Get element's custom css properties (-mystyle) using JavaScript
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>
Polymer 1.x: How to imperatively (using JS) obtain the value of a custom CSS property?
The docs you linked actually mention it...
You just need to check this.customStyle['--my-property-name']
and it will have the value of the property
Is it possible to use a block declaration as a CSS Custom Property?
A custom property is just that: an individual CSS property, with exactly one value. This value just happens to be usable in other property values via the var()
function.
The value of a custom property cannot be a declaration block, because a declaration block is not a property value. By extension, a single custom property cannot substitute for multiple property declarations.
Whatever SCSS does with custom properties is non-standard and cannot be reproduced with a standard CSS stylesheet unless it's post-processed somehow (and even then, that's just pretending that custom properties can do more than what they're actually capable of by taking an additional dependency).
Related Topics
How Does This Object Method Definition Work Without the "Function" Keyword
How to Chain and Share Prior Results With Promises
Load Less.Js Rules Dynamically
How to Concatenate and Minify Multiple CSS and JavaScript Files with Grunt.Js (0.3.X)
Limit Labels Number on Chart.Js Line Chart
How to Preserve Aspect Ratio When Scaling Image Using One (Css) Dimension in IE6
Detecting Touch Screen Devices with JavaScript
Rails: Post 422 (Unprocessable Entity) in Rails? Due to the Routes or the Controller
Rotate the Elements in an Array in JavaScript
How to Use Backslashes (\) in a String
Is There a Case Insensitive Jquery :Contains Selector
Check Browser CSS Property Support with JavaScript
Visibility:Hidden VS Display:None VS Opacity:0
Bootstrap Modal: Background Jumps to Top on Toggle