![Get the default value of a CSS property](https://javascriptsource.com/wp-content/uploads/2021/10/Get-the-default-value-of-a-CSS-property-702x526.jpg)
The following function 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
document.body.appendChild(ele);
// Get the styles of new element
const styles = window.getComputedStyle(ele);
// Get the value of property
const value = styles.getPropertyValue(property);
// Remove the element
document.body.removeChild(ele);
// Return the value of property
return value;
};
We can use it, for example, to get the default font size of div
tag:
getDefaultProperty('div', 'font-size');
// Or
getDefaultProperty('div', 'fontSize');
Source
https://htmldom.dev/get-the-default-value-of-a-css-property/