来吧,登录吧,往日欢迎你!
您需要 登录 才可以下载或查看,没有账号?立即注册
×
01 :is() —— 选择器合并神器,冗余直接砍半
把重复前缀一组合,代码瞬间清爽。
- /* 旧写法 */
- header p, main p, footer p { line-height:1.6; }
- /* 新写法 */
- :is(header, main, footer) p { line-height:1.6; }
复制代码
多层嵌套也能叠:
- :is(header, footer) a:is(:hover, :focus) { color:[color=#576b95 !important][url=]#2563eb[/url]; }
复制代码
02 :where() —— 零权重福音,组件样式随便覆
功能和:is()一模一样,但特异性=0,写基础样式、组件默认样式神器。
- /* 权重高,难覆盖 */
- article :is(h2,h3) { color:[color=#576b95 !important][url=]#222[/url]; }
- /* 权重0,轻松覆盖 */
- article :where(h2,h3) { color:[color=#576B95 !important][url=]#222[/url]; }
复制代码
适用场景:UI库、全局重置、基础排版。
03 :has() —— 父选择器降临,干掉80%冗余JS
终于能根据子元素/兄弟关系选父级,逻辑直接写进CSS。
- /* 包含图片的卡片 */
- .card:has(img) { padding-top:0; border-radius:12px 12px 0 0; }
- /* 表单校验失败 */
- .form-group:has(input:invalid) { border-color:[color=#576B95 !important][url=]#ef4444[/url]; }
- /* 有下拉菜单的导航项 */
- nav li:has(ul) > a::after { content:"▼"; margin-left:6px; }
复制代码
真正的“CSS-if逻辑”,现代浏览器全支持。
04 容器查询 —— 组件级响应式,告别视口绑架
比媒体查询强10倍:组件看容器宽度,不是看屏幕。
- /* 定义容器 */
- .sidebar { container-type:inline-size; }
- /* 容器够宽就变横版 */
- @container (min-width:380px) {
- .card { display:flex; gap:16px; }
- }
复制代码
一套组件,适配侧边栏、弹窗、主内容区。
05 @layer 层叠规则 —— 终结权重战争
用“层优先级”代替权重内卷,结构清晰到离谱。
- @layer reset, base, components, utilities;
- @layer base {
- body { line-height:1.5; }
- }
- @layer components {
- .btn { padding:8px 16px; }
- }
复制代码
层越靠后优先级越高,再也不用!important。
06 :not() 多条件排除 —— 精准过滤,一行顶三行
新版支持多选择器并列,不用重复写:not()+:not()。
- /* 排除三类元素 */
- div:not(.draft, .hidden, [data-loading="true"]) {
- background:[color=#576B95 !important][url=]#fff[/url];
- }
复制代码
07 nth-child 高级公式 —— 批量排版不用加class
不用JS、不用循环,纯CSS控制间距、高亮。
- /* 第4个及以后 */
- li:nth-child(n+4) { margin-top:12px; }
- /* 前5个加粗 */
- li:nth-child(-n+5) { font-weight:600; }
- /* 奇偶行 */
- tr:nth-child(even) { background:[color=#576B95 !important][url=]#f9fafb[/url]; }
复制代码
08 属性选择器通配符 —— 自动识别链接/文件/状态
像正则一样匹配,自动给链接加图标、标状态。
- /* PDF链接 */
- a[href$=".pdf"]::after { content:" 📄"; }
- /* 外部链接 */
- a[href^="http"]:not([href*="你的域名"])::after { content:" 🔗"; }
- /* 数据状态 */
- .card[data-status="error"] { border-color:[color=#576B95 !important][url=]#ef4444[/url]; }
复制代码
09 :focus-visible —— 优雅焦点,不丑且可访问
只在键盘导航时显示轮廓,鼠标点击不干扰。
- button:focus-visible {
- outline:2px solid [color=#576B95 !important][url=]#2563eb[/url];
- outline-offset:2px;
- }
复制代码
10 :empty —— 空元素自动隐藏,布局不崩
没有文字、没有子元素,直接隐藏,不用JS判断。
- section:empty, div:empty { display:none; }
复制代码
11 相邻兄弟组合 —— 自动间距、排版逻辑
不用给每个元素加class,靠关系自动排版。
- /* 列表项之间自动间距 */
- li + li { margin-top:8px; }
- /* 标题后所有段落缩进 */
- h2 ~ p { padding-left:1em; }
复制代码
12 color-mix() —— 原生混色,告别SASS变量
CSS自带颜色混合,主题色、渐变、透明度一键生成。
- .btn {
- background:color-mix(in srgb, #2563eb 80%, white 20%);
- }
复制代码
13 accent-color —— 表单原生着色,一行统一风格
单选、复选、滑块一键换色,不用覆盖伪元素。
- input[type="checkbox"],
- input[type="radio"] {
- accent-color:[color=#576B95 !important][url=]#2563eb[/url];
- }
复制代码
14 subgrid —— 嵌套网格对齐,强迫症狂喜
子网格直接继承父网格轨道,卡片高度、标题完美对齐。
- .container {
- display:grid;
- grid-template-columns:repeat(3,1fr);
- }
- .card {
- display:grid;
- grid-template-rows:subgrid;
- grid-row:span 3;
- }
复制代码
2026前端必背:4组黄金组合
复杂选择器::is() + :where() 精简代码
关系判断::has() 替代大量JS逻辑
响应式:容器查询 + 媒体查询 双剑合璧
工程化:@layer 管理优先级,告别权重地狱
最后
现代CSS早已不是“写样式”,而是用声明式逻辑控制UI。 这14个技巧,能让你:
代码量减少60%–70%
维护成本直线下降
页面更流畅、扩展性更强
建议立刻收藏,下次写CSS直接照着抄。
|