Get the default value of a css property

The function below returns the default value of CSS property for given tagName:

const getDefaultProperty = function(tagName, property) {
    // Create new element
    const ele = document.createElement(tagName);

    // Append to the body

    // Get the styles of new element
    const styles = window.getComputedStyle(ele);

    // Get the value of property
    const value = styles.getPropertyValue(property);

    // Remove the element

    // Return the value of property
    return value;

Good to know

The getComputedStyle() method returns the live styles of given element. It means that it's updated automatically if the element styles are changed.

That's why we need to get the value of property before removing the element. The following code doesn't return the correct value:

const styles = window.getComputedStyle(ele);

// Always return "" because the element is already 
// removed from the document
return styles.getPropertyValue(property);

We can use it, for example, to get the default font size of div tag:

getDefaultProperty('div', 'font-size');

// Or
getDefaultProperty('div', 'fontSize');

Might be useful