StylePropertyMapReadOnly: get() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Want more browser support for this feature? Tell us why.
Note: This feature is available in Web Workers.
The get() method of the StylePropertyMapReadOnly interface returns a CSSStyleValue-derived object for the first value of the specified property.
Use getAll() to get all the values of a CSS property that can have multiple values, such as background-image or transition.
Syntax
get(property)
Parameters
property-
The name of the property. Custom property names (starting with
--) are matched case-sensitively; standard property names are not.
Return value
A CSSStyleValue-derived object, or undefined if property has no value in the map.
The concrete type of the returned object depends on the property and its value.
For example, a property assigned a keyword might return a CSSKeywordValue, while a property assigned the result of a mathematical operation might return a CSSMathSum.
Exceptions
TypeError-
Thrown if
propertyis not a valid CSS property name.
Examples
>Basic usage
This example shows how to get a few properties and their values.
HTML
First we create a link inside a paragraph:
<p>
<a href="https://example.com">Link</a>
</p>
CSS
The CSS styles the font-weight of the paragraph, and defines and uses a custom property for the color of the link:
p {
font-weight: bold;
}
a {
--color: red;
color: var(--color);
}
JavaScript
First we call computedStyleMap() on the element to get its style map: a StylePropertyMapReadOnly object.
We create an array of properties of interest and use the get() method of StylePropertyMapReadOnly to log only those values.
// get the element and its style map
const myElement = document.querySelector("a");
const styleMap = myElement.computedStyleMap();
// array of properties we're interested in
const properties = ["font-weight", "border-left-color", "color", "--color"];
// log the value of each property of interest
for (const property of properties) {
log(`${property}: ${styleMap.get(property)}`);
}
Result
Getting different value types
This example demonstrates that get() can return objects of different types, depending on the property and its value.
First it applies a number of different properties to an element using CSS. It then reads them back via JavaScript and logs the constructor name and value of each property. The logging code itself isn't part of the demonstration, so it's hidden here.
HTML
First we define an <div> with id demoBox to which we'll add styles.
<div id="demoBox">Text</div>
CSS
#demoBox {
--my-custom-property: "some text";
opacity: 0.5;
width: calc(30% - 20px);
display: block;
transform: translate(10px, 10px);
}
JavaScript
Next we use computedStyleMap() to get the StylePropertyMapReadOnly object for the demo box, and iterate through each of the properties we added, logging their constructor name and value.
const styleMap = document.querySelector("#demoBox").computedStyleMap();
const properties = [
"opacity",
"display",
"width",
"transform",
"--my-custom-property",
];
for (const property of properties) {
const value = styleMap.get(property);
log(property);
log(` type: ${value.constructor.name}`);
log(` value: ${value}\n`);
}
Result
Specifications
| Specification |
|---|
| CSS Typed OM Level 1> # dom-stylepropertymapreadonly-get> |