406 words
2 minutes
CSS - if statements
if() 函数允许根据不同属性检查结果赋予不同的值,这些条件包括 style query、media query 以及 feature query 等
Intro
if() 是 2025 年引入的 CSS 新特性(Chrome 137 及以上版本支持),允许开发者在纯 CSS 中实现条件逻辑,而无需依赖 JavaScript、预处理器(如:Sass)或多个类名
在此之前,CSS 的条件主要通过 @media、@container、@supports 等规则实现
Example
Basics
.box { width: 300px; height: 300px; background-color: red;}
@media (width <= 1000px) { .box { background-color: blue; }}
@media (width <= 600px) { .box { background-color: green; }}.box { width: 300px; height: 300px; background-color: if( media(width <= 600px): green; media(width <= 1000px): blue; else: red; );}if() 局限在于无法复用属性逻辑
例如:对于同样的
color属性需要根据不同的媒体查询赋值,在每一个选择器中重复定义条件
.box { color: if( media(width <= 600px): red; media(width <= 1000px): blue; else: white; );}
a { color: if( media(width <= 600px): red; media(width <= 1000px): blue; else: white; );}if style checks
.box { --test: "hi";
width: 300px; height: 300px; background-color: if( style(--test: "hi"): green; else: red; );}这种 style() 检查也可以通过更早的 @container 实现
:root { --test: "hi";}
.box { width: 300px; height: 300px; background-color: red;}
@container style(--test: "hi") { .box { background-color: green; }}Example use case for if function
使用 if() 函数将媒体查询的结果抽象为断点变量
:root { --bp: if( media(width <= 600px): "sm"; media(width <= 1000px): "md"; else: "lg"; );}
.box { width: 300px; height: 300px; background-color: if( style(--bp: "sm"): green; style(--bp: "md"): blue; style(--bp: "lg"): red; else: black; );}if attr checks
attr() 函数允许在 CSS 中访问元素的属性值,例如:data-* 或其他标准属性
.box { --status: attr(data-status);
width: 300px; height: 300px; background-color: if( style(--status: "loading"): green; style(--status: "loaded"): blue; style(--status: "error"): red; else: black; );}References
CSS - if statements
https://www.waterwater.moe/posts/2025/css-if-statements/