Firefox Tomorrow

web api instance method

CSSPrimitiveValue: getFloatValue() method

View on MDN ↗

The getFloatValue() method of the CSSPrimitiveValue interface is used to get a float value in a specified unit. If this CSS value doesn’t contain a float value or can’t be converted into the specified unit, a DOMException is raised.

[!NOTE] This method was part of an attempt to create a typed CSS Object Model. This attempt has been abandoned, and most browsers do not implement it.

To achieve your purpose, you can use:

Syntax

getFloatValue(unit)

Parameters

Return value

A float value in the specified unit.

Exceptions

Type Description
DOMException An INVALID_ACCESS_ERR is raised if the CSS value doesn't contain a float value or if the float value can't be converted into the specified unit.

Examples

const cs = window.getComputedStyle(document.body);
const cssValue = cs.getPropertyCSSValue("margin-top");
console.log(cssValue.getFloatValue(CSSPrimitiveValue.CSS_CM));

Specifications

This feature was originally defined in the DOM Style Level 2 specification, but has been dropped from any standardization effort since then.

It has been superseded by a modern, but incompatible, CSS Typed Object Model API that is now on the standard track.

Browser compatibility

Browser compatibilityCompatibility data is available on the canonical MDN page.