Reset CSS display property to default value

前端 未结 6 2035
粉色の甜心
粉色の甜心 2020-11-22 08:46

Is it possible to override the display property with its default value? For example if I have set it to none in one style, and I want to override it in a different with its

6条回答
  •  攒了一身酷
    2020-11-22 09:21

    If you have access to JavaScript, you can create an element and read its computed style.

    function defaultValueOfCssPropertyForElement(cssPropertyName, elementTagName, opt_pseudoElement) {
        var pseudoElement = opt_pseudoElement || null;
        var element = document.createElement(elementTagName);
        document.body.appendChild(element);
        var computedStyle = getComputedStyle(element, pseudoElement)[cssPropertyName];
        element.remove();
        return computedStyle;
    }
    
    // Usage:
    defaultValueOfCssPropertyForElement('display', 'div'); // Output: 'block'
    defaultValueOfCssPropertyForElement('content', 'div', ':after'); // Output: 'none'
    

提交回复
热议问题