惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
The Register - Security
The Register - Security
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
罗磊的独立博客
U
Unit 42
S
SegmentFault 最新的问题
Y
Y Combinator Blog
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
博客园 - 叶小钗
C
Cybersecurity and Infrastructure Security Agency CISA
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
S
Securelist
A
Arctic Wolf
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
Scott Helme
Scott Helme
博客园 - 聂微东
博客园 - 【当耐特】
T
Tenable Blog
I
Intezer
D
DataBreaches.Net
B
Blog RSS Feed
Security Latest
Security Latest
C
Cisco Blogs
T
Tor Project blog
N
Netflix TechBlog - Medium

博客园 - 饭特稠

浪浪山老前端的2025 useAttrs 是响应式的吗(by 豆包) 实时互动教育版的自定义 UI 如何开发 2024,在路上 当蓝牙键盘连不上电脑:一次意外的debug之旅 前端技术选型时有用的网站 好用的zsh插件,打造好用的命令行 2023年终总结:怀孕,装修,还贷和其他 分享一个URL正则 使用 scriptable 实现每日诗词小组件 如何使webpack编译 node_modules 中的 npm 包 小程序中实现图片旋转后保存 Custom Elements 和 Shadow DOM了解一下? 聊聊CSS 缓动函数的新成员linear() chatgpt: 在ts中如何声明一个全局类型 Workbox -- 为serviceWorker量身定做的工具 cache API简介 重新学车 魔幻2022
CSS 原生嵌套来了
饭特稠 · 2023-06-30 · via 博客园 - 饭特稠

前言

在web发展的过程中,有很多API都是浏览器吸收了开源社区的热门库后出现的,比如 document.querySelector 吸收了 jQuery 的精华,或者Promise,JSON的相关API都是先出现在社区,然后被浏览器原生支持。今天我们要谈的css原生嵌套也是借鉴了less,sass等社区方案而出现的。

css原生嵌套

在没有css原生嵌套之前,如果不借助css预处理器,我们可能经常要写这样的代码:

.nesting {
  color: hotpink;
}

.nesting > .is {
  color: rebeccapurple;
}

.nesting > .is > .awesome {
  color: deeppink;
}

有了css原生嵌套之后,我们可以这样写:

.nesting {
  color: hotpink;

  > .is {
    color: rebeccapurple;

    > .awesome {
      color: deeppink;
    }
  }
}

可以发现,使用css嵌套之后,我们的代码量更少,而且结构更清晰。

&符号

在less中, 我们可以用&来表示嵌套的父级选择器,在css原生嵌套中也是这个效果:

.card {
  .featured & {
    /* 表示 .featured .card */
  }

  & .featured  {
    /* 表示 .card  .featured */
  }
  &.featured  {
    /* 表示 .card.featured,注意没有空格 */
  }
}

与less 中 & 的区别

  1. 不支持字符串连接
    在less中,我们可以使用&来拼接选择器的字符串:
.card {
  &--header {
    /* 等于 .card--header/
  }
}

但是在css原生嵌套这么做是不生效的。具体可以看这个解释

  1. 不支持直接嵌套html标签选择器
    在less中,我们可以这么写:
.card {
 h1 {
    /* 等于 .card h1/
  }
}

但是在css嵌套中,这么写是无效的,如果要想达到这样的效果,我们可以这么写:

.card {
 & h1 {
    /* 等于 .card h1/
  }
/*或者*/
:is(h1) {
  /*OK*/
}
}

总结一下就是,被嵌套的选择器必须以下面的字符开头:

所以嵌套 div 之类的 HTML 标签是不生效的

本文完

参考文档:

https://developer.chrome.com/articles/css-nesting/#understanding-the-nesting-parser