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

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Forum
  • 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:

  • Get all of an element’s parent elements up the DOM tree
  • Check If an Element is Visible in the Viewport
  • Find all matching elements on a page with document.querySelectorAll()
  • Get the Children of an Element

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2022 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us