MathMLElement: style property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨January 2023⁩.

The read-only style property of the MathMLElement returns the inline style of an element in the form of a live CSSStyleProperties object. This object can be used to get and set the inline styles of an element.

Value

A live CSSStyleProperties object.

Note: Earlier versions of the specification returned a CSSStyleDeclaration (from which CSSStyleProperties is derived). See the browser compatibility table for browser support information.

Description

The values of the inline styles set in the element's style attribute are reflected by corresponding properties of the returned CSSStyleProperties object.

Note: CSSStyleProperties has dash-named and corresponding camel-case named properties for all CSS properties supported by the browser (not just those with inline styles). Properties that don't have a corresponding inline style are set to "".

Shorthand CSS properties of the element are expanded to their corresponding long-form properties. For example, an element with style "border-top: 1px solid black" would be represented in the returned object by properties with the names border-top and borderTop, and the corresponding longhand properties border-top-color and borderTopColor, border-top-style and borderTopStyle, and border-top-width and borderTopWidth.

The style property is read-only, meaning it is not possible to assign a CSSStyleProperties object to it. Nevertheless, it is possible to set an inline style by assigning a string directly to the property. In this case the string can be read from cssText. Using style in this manner will completely overwrite all inline styles on the element.

To add specific styles to an element without altering other style values, it is generally preferable to set individual properties on the CSSStyleProperties object. For example, you can write element.style.backgroundColor = "red". A style declaration is reset by setting it to null or an empty string, e.g., element.style.color = null.

The style property has the same priority in the CSS cascade as an inline style declaration set via the style attribute.

Examples

Enumerating style information

This example demonstrates how we can enumerate the dash-named properties of CSSStyleProperties.

HTML

html
<math>
  <mrow>
    <mi>f</mi>
    <mo stretchy="false">(</mo>
    <mi class="parameter" style="color: red; border-bottom: 1px solid">x</mi>
    <mo stretchy="false">)</mo>
    <mo>=</mo>
    <mi>x</mi>
  </mrow>
</math>
<pre id="log"></pre>

JavaScript

The following code iterates the enumerable properties of the CSSStyleProperties and logs the result.

js
const element = document.querySelector(".parameter");
const elementStyle = element.style;

// Loop through all the element's styles using `for...in`
for (const prop in elementStyle) {
  // Check the property belongs to the CSSStyleProperties instance
  // Ensure the property is a numeric index (indicating a dash-named/inline style)
  if (
    Object.hasOwn(elementStyle, prop) &&
    !Number.isNaN(Number.parseInt(prop, 10))
  ) {
    log(
      `${
        elementStyle[prop]
      } = '${elementStyle.getPropertyValue(elementStyle[prop])}'`,
    );
  }
}

Results

The result is shown below. Note that only the element's longhand CSS properties are enumerated values (the inline shorthand property is not enumerated).

Specifications

Specification
CSS Object Model (CSSOM)
# dom-elementcssinlinestyle-style

Browser compatibility

See also