Skip to content

Commit e8a7432

Browse files
tursunovasvgeesus
authored andcommitted
Applied review comments
1 parent 4745978 commit e8a7432

File tree

1 file changed

+77
-32
lines changed

1 file changed

+77
-32
lines changed

css-values-5/if-explainer.md

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,48 @@ This document is an explainer for the `if()` function proposed for CSS Values an
44

55
## Introduction
66

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.
813

914
## Motivation
1015

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:
1220

1321
```css
1422
.my-element {
15-
color: blue;
23+
color: blue; /* default */
1624
}
1725

18-
@media (min-width: 600px) {
19-
.my-element {
20-
color: red;
21-
}
26+
.theme-dark .my-element {
27+
color: red; /* theme override */
2228
}
2329
```
2430

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:
2636

2737
```css
38+
/* Assuming a parent has --theme: dark set */
2839
.my-element {
29-
color: if(media(min-width: 600px): red; else: blue);
40+
color: if(style(--theme: dark): red; else: blue);
3041
}
3142
```
3243

33-
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.
3449

3550
## Syntax
3651

@@ -46,21 +61,17 @@ The `if()` function's syntax is formally defined as:
4661
style( <style-query> )
4762
```
4863

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.
5069

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.
5273

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
6475

6576
### 1. Simple Media Query
6677

@@ -74,17 +85,33 @@ A common use case is to switch a value based on a media query.
7485

7586
### 2. Feature Support
7687

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.
7896

7997
```css
80-
.card {
81-
display: if(supports(display: grid): grid; else: block);
98+
.highlight {
99+
background-image: linear-gradient(
100+
to right,
101+
hsl(50 100% 50%),
102+
if(
103+
supports(color: color(display-p3 0 0 0)): color(display-p3 1 0.5 0);
104+
else: hsl(30 100% 50%)
105+
)
106+
);
82107
}
83108
```
84109

85110
### 3. Theming with Custom Properties
86111

87-
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.
88115

89116
```css
90117
/* 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 `
108135

109136
### 4. Combining Conditions with `and`
110137

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.
112141

113142
```css
114143
.widget {
115144
--base-size: 2rem;
116145
--enhanced-size: 3rem;
117146

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 */
119148
font-size: if(
120-
media(width >= 1024px) and supports(display: subgrid): var(--enhanced-size);
149+
media(width >= 1024px) and supports(display: flex): var(--enhanced-size);
121150
else: var(--base-size)
122151
);
123152
}
124153
```
125154

126155
## Fallback Behavior
127156

128-
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`).
129162

130163
```css
131164
.box {
@@ -136,6 +169,18 @@ If no condition in an `if()` function evaluates to true and no `else` branch is
136169

137170
To ensure predictable behavior, it is recommended to always provide an `else` branch.
138171

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+
139179
## Security and Privacy Considerations
140180

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

Comments
 (0)