• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / CSS / Get the default value of a CSS property with JavaScript

Get the default value of a CSS property with JavaScript

Get the default value of a CSS property

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/

CSS

Related Snippets:

  • Create a custom scrollbar
  • Get the Parent of an Element
  • Check if HTML element has class
  • Toggle a class for an HTML element

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers