Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Getting a custom variable by dot notation

Is there any way to access to a custom variable through dot notation (or bracket) instead of doing the getComputedStyle and getPropertyValue methods? In example (I know it's not the best way):

let darkColor = getComputedStyle(document.documentElement).getPropertyValue('--dark-color'); 
console.log(darkColor)
:root {
  --dark-color: black;
}

Is there any way to dont use those methods and write something like document.documentElement.style['--dark-color']? (this one doesn't work, but just for getting the idea). I am curious at it because you can access to regular attributes like color or background-color through the dot notation but not for custom variables.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can get the applied custom properties only using getComputedStyle and getPropertyValue.

element.style[someProperty] and getComputedStyle(element)[someProperty] are not equivalent.

element.style gives you the property directly assigned to the element (not what is currently applied, which can be different).

And your :root { --dark-color: black; } is applied through the stylesheet and not at the element directly.

getComputedStyle returns a CSSStyleDeclaration where the values of the properties reflect the result of all applied rules. To get the correct custom property you need getComputedStyle, so you can't get rid of that.

For getComputedStyle(element).getPropertyValue(name) compared to getComputedStyle(element)[name] part.

The dot notation or array access is not possible for custom properties. That access is given by this part of the specification:

For each CSS property property that is a supported CSS property, the following partial interface applies where camel-cased attribute is obtained by running the CSS property to IDL attribute algorithm for property.

partial interface CSSStyleDeclaration {  
  [CEReactions] attribute [LegacyNullToEmptyString] CSSOMString 
  _camel_cased_attribute;
};

The camel-cased attribute attribute, on getting, must return the result of invoking getPropertyValue() with the argument being the result of running the IDL attribute to CSS property algorithm for camel-cased attribute.

And supported CSS property is specified as:

[…] The term supported CSS property* refers to a CSS property that the user agent implements, including any vendor-prefixed properties, but excluding custom properties. […]

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!