A numeric CSS value and its unit.
dom::StyleValue pairs a number with a CSS unit for styling page elements from native code. It lets you write numeric values directly to an element's inline style without formatting strings, and read numeric properties back from inline or computed styles.
You can update a health bar with computed values:
A handle to an element on a page.
Definition Element.h:142
detail::StyleProxy style
The element's inline style (style).
Definition Element.h:179
static constexpr StyleValue Pct(double v)
Create a percentage (%).
Definition StyleValue.h:240
static constexpr StyleValue Px(double v)
Create a length in CSS pixels (px).
Definition StyleValue.h:233
The library still parses the value as CSS when applying it to the element.
Reading Styles and Value States
Calling AsStyleValue() on an inline or computed style parses the property into a dom::StyleValue:
if (width)
PlaceMinimap(width.
value);
ComputedStyle getComputedStyle(const Element &element)
Get an element's computed style (getComputedStyle).
Definition ComputedStyle.h:232
A numeric CSS value and its unit.
Definition StyleValue.h:147
double value
The number (only meaningful when this holds one).
Definition StyleValue.h:148
A dom::StyleValue is always in one of these states:
- A number holds a finite value and a StyleUnit. Writing it sets the property.
- Empty holds no value. Writing it removes the inline property.
- Invalid represents text that doesn't convert to a number (eg, auto) or a number that isn't finite. Writing it does nothing and keeps the existing style.
Checking a dom::StyleValue with if (value) evaluates to true only when it holds a number.
You can copy an inline style directly between elements by assigning what you read:
icon.style.width = badge.style.width.AsStyleValue();
Unsupported Units and Custom Properties
- Properties ignore unsupported units. Assigning a unit that a property doesn't accept (such as pixels for opacity or a unitless number for width) does nothing and keeps the previous value. When DOM diagnostics are on, the library logs a warning for each ignored write.
- Custom properties require text. Assigning a dom::StyleValue to a custom property (--name) does nothing, so assign text like "8px" instead.
- See also
- dom::Element::style, dom::getComputedStyle(), dom::StyleUnit, dom::CssValue
|
| static constexpr StyleValue | Parse (std::string_view text) |
| | Parse CSS text into a StyleValue.
|
| static constexpr StyleValue | Number (double v) |
| | Create a unitless number (CSS <number>).
|
| static constexpr StyleValue | Px (double v) |
| | Create a length in CSS pixels (px).
|
| static constexpr StyleValue | Pct (double v) |
| | Create a percentage (%).
|
| static constexpr StyleValue | Em (double v) |
| | Create a multiple of the element's font size (em).
|
| static constexpr StyleValue | Rem (double v) |
| | Create a multiple of the root element's font size (rem).
|
| static constexpr StyleValue | Vw (double v) |
| | Create a percentage of the viewport width (vw).
|
| static constexpr StyleValue | Vh (double v) |
| | Create a percentage of the viewport height (vh).
|
| static constexpr StyleValue | Deg (double v) |
| | Create an angle in degrees (deg).
|
| static constexpr StyleValue | Ms (double v) |
| | Create a time in milliseconds (ms).
|
| static constexpr StyleValue | S (double v) |
| | Create a time in seconds (s).
|