You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: css-values-5/if-explainer.md
+77-32Lines changed: 77 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,33 +4,48 @@ This document is an explainer for the `if()` function proposed for CSS Values an
4
4
5
5
## Introduction
6
6
7
-
The `if()` function introduces powerful, inline conditional logic to CSS properties. It allows authors to select a value for a property based on a set of ordered conditions, similar to `if/else` constructs in many programming languages. This provides a more dynamic and streamlined way to write CSS without relying on verbose selectors or JavaScript.
7
+
The `if()` function introduces powerful,
8
+
inline conditional logic to CSS properties.
9
+
It allows authors to select a value for a property based on a set of ordered conditions,
10
+
similar to `if/else` constructs in many programming languages.
11
+
This provides a more dynamic and streamlined way to write CSS
12
+
without relying on verbose selectors or JavaScript.
8
13
9
14
## Motivation
10
15
11
-
Currently, CSS authors often need to repeat selectors to apply different styles under different conditions. For example, to change a property based on a media query, one would write:
16
+
Currently, CSS authors often need to repeat selectors
17
+
to apply different styles under different conditions.
18
+
For example, to change a property based on a custom property for theming,
19
+
one would write:
12
20
13
21
```css
14
22
.my-element {
15
-
color: blue;
23
+
color: blue;/* default */
16
24
}
17
25
18
-
@media (min-width: 600px) {
19
-
.my-element {
20
-
color: red;
21
-
}
26
+
.theme-dark.my-element {
27
+
color: red; /* theme override */
22
28
}
23
29
```
24
30
25
-
While functional, this pattern can lead to verbose and fragmented code, especially when multiple conditions are involved. The `if()` function aims to simplify this by allowing conditional logic to be expressed inline within a single declaration:
31
+
While functional, this pattern can lead to verbose and fragmented code,
32
+
especially when multiple conditions are involved.
33
+
The `if()` function aims to simplify this
34
+
by allowing conditional logic to be expressed inline within a single declaration,
35
+
using `style()` query to check custom properties values:
This approach improves readability and co-locates related logic, making stylesheets easier to maintain. This is especially powerful when combined with CSS Custom Properties, allowing for the creation of dynamic, themeable components with logic encapsulated directly in the CSS.
44
+
This approach improves readability and co-locates related logic,
45
+
making stylesheets easier to maintain.
46
+
This is especially powerful when combined with CSS Custom Properties,
47
+
allowing for the creation of dynamic,
48
+
themeable components with logic encapsulated directly in the CSS.
34
49
35
50
## Syntax
36
51
@@ -46,21 +61,17 @@ The `if()` function's syntax is formally defined as:
46
61
style( <style-query> )
47
62
```
48
63
49
-
In essence, the function is a list of conditional branches separated by semicolons. Each branch contains a condition followed by a colon (`:`) and a value. The conditions are evaluated in order, and the value from the first branch with a true condition is used.
64
+
In essence,
65
+
the function is a list of conditional branches separated by semicolons.
66
+
Each branch contains a condition followed by a colon (`:`) and a value.
67
+
The conditions are evaluated in order,
68
+
and the value from the first branch with a true condition is used.
50
69
51
-
The `else` keyword can be used as a condition that is always true. This makes it useful for providing a final fallback value, as any branches after an `else` branch will be ignored.
70
+
The `else` keyword can be used as a condition that is always true.
71
+
This makes it useful for providing a final fallback value,
72
+
as any branches after an `else` branch will be ignored.
52
73
53
-
## Core Use Cases
54
-
55
-
The `if()` function is well-suited for a variety of scenarios where a property's value needs to change based on context.
56
-
57
-
***Responsive Design:** Instead of defining multiple rules in separate `@media` blocks, `if()` allows responsive logic to be encapsulated within a single property. This is ideal for component-based architectures, as it co-locates the logic and makes it easier to manage fluid typography, spacing, or layout adjustments directly where they are applied.
58
-
59
-
***Theming:**`if()` enables dynamic and sophisticated theming systems. By checking the value of a custom property with `style()`, a component can adjust multiple aspects of its appearance (e.g., background, text color, border) based on a single theme flag (like `--theme: dark`). This centralizes theme logic and allows for the creation of derived styles that automatically respond to theme changes.
60
-
61
-
***Feature Detection & Progressive Enhancement:** Similar to the `@supports` rule, `if()` can check for browser feature support. Its advantage is providing an inline fallback within a single declaration. This is perfect for progressively enhancing a component. For example, you can use a modern layout mode like `subgrid` if it's supported, and fall back to a simpler, more widely-supported value like `grid` or `block` if it's not, all within the same `display` property.
62
-
63
-
## Examples
74
+
## Use Cases
64
75
65
76
### 1. Simple Media Query
66
77
@@ -74,17 +85,33 @@ A common use case is to switch a value based on a media query.
74
85
75
86
### 2. Feature Support
76
87
77
-
Conditionally apply a value based on whether the browser supports a particular CSS feature.
88
+
Conditionally apply a value based on whether the browser supports a particular CSS feature,
89
+
allowing for progressive enhancement.
90
+
For example, using a modern,
91
+
more vibrant color from a wider-gamut color space like Display P3,
92
+
inside of a gradient,
93
+
and falling back to a standard sRGB color if P3 is not supported.
94
+
Without `if()`,
95
+
the author would have to repeat the entire `linear-gradient()` expression.
The `if()` function is particularly powerful for creating themes. Here, we use `style()` to check the value of a `--theme` custom property inherited from an ancestor and adjust colors accordingly.
112
+
The `if()` function is particularly powerful for creating themes.
113
+
Here, we use `style()` to check the value of a `--theme` custom property inherited from an ancestor
114
+
and adjust colors accordingly.
88
115
89
116
```css
90
117
/* On a parent element, you might have: --theme: dark; */
@@ -108,24 +135,30 @@ The `if()` function is particularly powerful for creating themes. Here, we use `
108
135
109
136
### 4. Combining Conditions with `and`
110
137
111
-
Conditions can be combined with boolean keywords like `and`, `or`, and `not`. This allows for more complex logic, such as applying a style only when multiple conditions are met.
138
+
Conditions can be combined with boolean keywords like `and`, `or`, and `not`.
139
+
This allows for more complex logic,
140
+
such as applying a style only when multiple conditions are met.
112
141
113
142
```css
114
143
.widget {
115
144
--base-size: 2rem;
116
145
--enhanced-size: 3rem;
117
146
118
-
/* Use the enhanced size only on wide screens that also support subgrid*/
147
+
/* Use the enhanced size only on wide screens that also support flexbox*/
If no condition in an `if()` function evaluates to true and no `else` branch is provided, the function resolves to an empty token stream. This makes the declaration invalid at computed-value time, causing the property to fall back to its inherited or initial value (behaving like `unset`).
157
+
If no condition in an `if()` function evaluates to true
158
+
and no `else` branch is provided,
159
+
the function resolves to a `<guaranteed-invalid>` value.
160
+
This makes the declaration invalid at computed-value time,
161
+
causing the property to fall back to its inherited or initial value (behaving like `unset`).
129
162
130
163
```css
131
164
.box {
@@ -136,6 +169,18 @@ If no condition in an `if()` function evaluates to true and no `else` branch is
136
169
137
170
To ensure predictable behavior, it is recommended to always provide an `else` branch.
138
171
172
+
## Stakeholder Feedback
173
+
174
+
This proposal has been discussed in the CSS Working Group,
175
+
and feedback from browser vendors and web developers has been positive.
176
+
Currently, `media()`, `supports()`, and `style()` queries are supported,
177
+
but there are possibilities for expansion in the future.
178
+
139
179
## Security and Privacy Considerations
140
180
141
-
The `if()` function itself does not introduce new security or privacy concerns. It provides a new syntax for expressing conditional logic but relies on existing mechanisms (`media()`, `supports()`, `style()`) that have their own, already-defined security profiles. It does not introduce any new capabilities that could be used to fingerprint users or exfiltrate data.
181
+
The `if()` function itself does not introduce new security or privacy concerns.
182
+
It provides a new syntax for expressing conditional logic
183
+
but relies on existing mechanisms (`media()`, `supports()`, `style()`)
184
+
that have their own, already-defined security profiles.
185
+
It does not introduce any new capabilities
186
+
that could be used to fingerprint users or exfiltrate data.
0 commit comments