Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7075665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:12:16+00:00 2026-05-28T06:12:16+00:00

In an application where certain elements have custom CSS properties, is there any way

  • 0

In an application where certain elements have custom CSS properties, is there any way to retrieve such a value via JavaScript?

e.g.

<div id="myDiv" style="color:#f00;-my-custom-property:upsidedown;" />

I can access the color attribute via these two methods:

document.getElementById('myDiv').style.getPropertyValue("color")
document.getElementById('myDiv').style.color

But these do not work for custom properties. Is this supported at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:12:17+00:00Added an answer on May 28, 2026 at 6:12 am

    CSS values not understood by the browser are discarded, which explains why -my-custom-property was unavailable via .style.

    In the past, you would have had to rely on storing the data with data attributes and dealing with inheritance yourself via JavaScript.

    However, “custom properties”, aka “CSS variables”, have since been introduced into the standard and implemented by browsers, with ~92% support globally as of 2019-05-09. At a quick glance, Edge seems to have been the last major browser to implement, with version 16 on October 16, 2017.

    Essentially, you need to set a custom property (eg, --my-custom-property: 'foobar';) on an element, and it can be accessed with something like getComputedStyle(your_el).getPropertyValue("--my-custom-property") which would return 'foobar' (with a leading space). Note the leading space and quotation marks. It will return the value exactly as it was provided.

    Example:

    console.log(getComputedStyle(document.getElementById("a")).getPropertyValue("--my-custom-property-1"))
    console.log(getComputedStyle(document.getElementById("b")).getPropertyValue("--my-custom-property-2"))
    #b-div { --my-custom-property-2: 'world' }
    <div style="--my-custom-property-1: 'hello'"><h1 id="a">#a 'hello'</h1></div>
    <div id="b-div"><h1 id="b">#b 'world'</h1></div>

    Here’s some testing using one and two leading hyphens, inheritance, and different methods of retrieving the value:

    function log(computed, selector, prop, value) {
      let method = computed ? "getComputedStyle(el)" : "el.style"
      let method_id = computed ? "computed" : "raw"
    
      // Build first level of list (tag name)
      let first = document.querySelector("#" + selector)
      if (!first) {
        first = document.createElement("li")
        first.appendChild(document.createTextNode(selector))
        first.setAttribute("id", selector)
        first.appendChild(document.createElement("ul"))
        document.querySelector("ul").appendChild(first)
      }
    
      // Build second level of list (method of style retrieval)
      let second = document.querySelector("#" + selector + "-" + method_id)
      if (!second) {
        second = document.createElement("li")
        second.appendChild(document.createTextNode(method))
        second.setAttribute("id", selector + "-" + method_id)
        second.appendChild(document.createElement("ul"))
        first.querySelector("ul").appendChild(second)
      }
    
      // Build third level of list (property accessed)
      let third = document.querySelector("#" + selector + "-prop" + prop)
      if (!third) {
        third = document.createElement("li")
        third.appendChild(document.createTextNode(prop + ": `" + value + "`"))
        third.setAttribute("id", "prop" + prop)
        second.querySelector("ul").appendChild(third)
        if (value === "") {
          third.classList.add("bad")
        } else {
          third.classList.add("good")
        }
      }
    }
    
    // Uses .style
    function getStyleAttr(selector, prop) {
      let value = document.querySelector(selector).style.getPropertyValue(prop)
      log(false, selector, prop, value)
    }
    
    // Uses getComputedStyle()
    function getStyleComputed(selector, prop) {
      let value = getComputedStyle(document.querySelector(selector)).getPropertyValue(prop)
      log(true, selector, prop, value)
    }
    
    // Loop through each property for each element and output the value
    let selectors = ["article", "h1", "p"]
    let props = ["--my-custom-property", "-my-custom-property"]
    selectors.forEach(function(selector) {
      props.forEach(function(prop) {
        getStyleAttr(selector, prop)
        getStyleComputed(selector, prop)
      })
    })
    code {
      background: #eee;
      padding: .2em;
    }
    
    .bad {
      color: #800;
    }
    
    .good {
      color: #080;
    }
    <article class="custom-prop-inheritance" style="--my-custom-property: 'foobar'; -my-custom-property: 'foobar'">
      <h1>Title</h1>
      <p>Custom properties require two leading hyphens (<code>-my-custom-property</code> <em>never</em> works). Using <code>el.style</code> does not support inheritance. To support both inheritance and custom properties, you must use <code>getComputedStyle(<b>el</b>)</code> along with two leading hyphens on the custom property (eg, <code>--my-custom-property</code>).</p>
    </article>
    <ul></ul>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have a desktop vb application take control of an
In my application I have a form, which elements are named using a certain
I have a certain application in MSVC6.0 all C code. I have it as
I have a Java application which requires certain software (one of them being Perl)
I have a certain web application that makes upwards of ~100 updates to an
I'm having an issue running my application when certain elements exist in the layout
I currently have a Rails Apps that lets users drag and drop certain elements
I want to create an application that drops certain elements on other elements, using
I have a java selenium application which needs to be aware of certain events.
I have the following code which detects all the elements in a Silverlight application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.