Style Helper
This is a helper for authoring conditional values for property styles.
Write complex conditional styles based on one or more conditions, such as viewport sizes and interactive states, in a concise and expressive way.
Anchor to stylehelperStyleHelper
Sets an optional default value to use when no other condition is met.
Adjusts the style based on different conditions. All conditions, expressed as a literal object, must be met for the associated value to be applied.
The when
method can be chained together to build more complex styles.
Import the Style helper
Anchor to conditionsConditions
The following conditions are supported for conditional styles.
Multiple conditions can be set on the same when
method.
Name | Type | Description |
---|---|---|
"hover" | true | This condition is met when an element is hovered on with the cursor (mouse pointer). |
"focus" | true | This condition is met when an element is clicked, tapped on or selected using the Tab key. |
viewportInlineSize | {min: "small" | "medium" | "large"} | This condition is met when the device matches the minimum width. |
Anchor to examplesExamples
This section provides examples of conditions.
Anchor to example-default-style-with-conditionsDefault Style With Conditions
Default styling can be combined with specific conditions. In this example, the Grid’s children will be stacked by default and side by side on viewports above the small
breakpoint.
Anchor to example-simple-conditionSimple Condition
Using simple conditional styling enables you to specify a styling change when a condition is met. In this example, the View’s padding will be loose
on hover.
Anchor to example-conditionally-hiding-contentConditionally hiding content
Using the display
property with conditional styles enables you to hide content for certain viewport sizes. In this example, the View will be hidden on small and above screen sizes.