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

推荐订阅源

博客园 - 【当耐特】
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
小众软件
小众软件
The Cloudflare Blog
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
美团技术团队
WordPress大学
WordPress大学
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI
月光博客
月光博客
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
Google Developers Blog
GbyAI
GbyAI
B
Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗

kshao-blog-前端知识记录

win or mac clash 无 TUN 让 Antigravity、Chrome 强制 proxy(解决 Antigravity 无法加载选择 model、自动更新无法登录、跳转) 【大杂烩】在 pnpm 中直接修改 node_modules(.pnpm) 中的依赖项,项目中持久化 - pnpm 中的依赖处理、幽灵依赖、寻址规则等 在 html 中直接使用 Esm、Jsx 脚本快速调试和使用 React@19 和 Vue@3 源码,解决 React19 UMD 构建等问题 一键在本地批量检测并升级更新 package.json 中的模块依赖,ncu(npm-check-updates)在 npm、pnpm 或 workspace 项目中的使用教程 解决 Mac Docker Desktop 中启动出现的问题合集 通过阿里云、腾讯云无服务器搭建自定义的企业域名邮箱,实现在 QQ邮箱 收发等功能(附腾讯 SMTP 和 IMAP) 解决使用代理(clash 等)进行 SSH 连接(如 Github ssh key clone/push)出现 kex_exchange_identification 错误 静态文件资源 cdnjs, jsdelivr 抖音字节国内快速 CDN 镜像推荐【2025】- 仍在使用 bootcdn 和 staticfile CDN 请注意验证资源的完整性(SRI) pnpm monorepo 中管理依赖的最佳实践,与 Catalogs(目录)协议的使用(monorepo 中统一版本管理) Web 安全中的 Secure Contexts(安全上下文)- 解决在本地中使用 clipboard 或 Crypto 等 API 限制或关闭上下文限制 使用 serve 配合 openssl 或 mkcert 创建本地自签名可信任的证书 - 创建本地 TLS\SSL https 协议服务 利用 Github Actions 和 Acme 自动申请、更新和部署至阿里云、腾讯云 CDN Lets Encrypt SSl\TLS ECC RSA 双证书 【CSS】解决在 flex 容器中使用 align-content 或 justify-content 属性 center 居中时的溢出滚动和截断问题 - 理解 safe 关键字 在线工具 - 一键获取下载抖音无水印视频、抖音去水印解析工具、下载抖音无水印高清图集【2025 最新】 【React Router】v6 data router 在非组件(或工具方法)中如何优雅的跳转路由 【React】为什么路由跳转时页面滚动高度不会被重置(保留上个页面高度)?理解 history scrollRestoration 的场景与使用,以及如何使用 React Router 重置和跳转前保留滚动高度 【React】在本地 Html 中快速 debug(调试)React 源码 【React】结合源码和 EventLoop 分析 - 为什么 useLayoutEffect 会阻止 DOM 重绘(而 useEffect 闪烁)?为什么其内部 useState 会“同步”执行? React Compiler - 解放在函数中编程时的性能焦虑(React Conf 2024)附 Next 在线演示 在 github actions 中获取时间,并转换为中国标准时间(中国时区) 【npm】npm ci - npm clean install,在 CI、CD 中保持构建的一致性和可重复性 eslint 9.x 升级或使用指南,eslint.config.js 配置,包含 react、typescript、prettier 等常用配置升级迁移 使用 Spicetify 自定义 Spotify - 歌词翻译、全屏展示、主题替换 在 node 中快速代理请求(Proxy),解决跨域或请求转发问题 - http-proxy-middleware 修改请求体和返回 在 Hexo 中使用 AI(Gemini、deepseek、Azure)生成文章摘要,支持自定义模板。hexo-ai-summaries 插件文档(默认适配主题 Butterfly) 解决在 webstorm 或 idea 等 jetbrains 工具中遇到 Git 无法 force push,或 force push 灰色禁用无法点击(protected branches) hexo-seo-submit,Hexo 博客 SEO 优化插件 - 每日定时自动或手动提交链接至百度、Bing、Google,支持 Github Actions 和 Coding Jenkins 等CI(Hexo 插件编写) 一文吃透 pnpm 如何使用 workspace 构建 monorepo,与 npm、yarn 的用法对比(pnpm 9.x 内部安装依赖问题 link-workspace-packages) 【Node】Corepack - 解决 pnpm 或 yarn 的多版本管理、解决本地版本与 packageManager 中的版本一致性问题 解决 npm、cnpm 或 pnpm install 遇到 certificate has expired (证书已过期)
【CSS】主流 UI 库都在用的逻辑伪类选择器 not、where、is、has
kshao · 2023-06-13 · via kshao-blog-前端知识记录

目前 Css 关于逻辑选择器::not:is:where:has,除了 :has 比较新以外,其他选择器在一些 UI 库中都有广泛的使用,浏览器基本都已经支持了,一起卷起来,使用更先进的选择器~

MDN: :not() CSS 伪类用来匹配不符合一组选择器的元素。由于它的作用是防止特定的元素被选中,它也被称为反选伪类(negation pseudo-class)。

<ul class="css-selectors selector-not">
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>
.selector-not {
  li:not(:first-child) {
    border: 1px solid red;
  }
}
  • 1
  • 2
  • 3

li:not(:first-child) 选择除 :first-child 第一个 li 以外的其他 li 元素。

:not 优先级

:not 不会提升优先级,它的优先级由将由其逗号分割的参数中优先级最高的选择器指定

:not 兼容性

can i use,还是可以放心使用滴~

:is

:is 选择器并没有什么新功能,它像一个语法糖,可以让我们更方便的书写选择器,它的作用是将一组选择器包裹起来,然后作为一个整体使用,这样就可以减少选择器的书写量,提高代码的可读性。如下示例

之前对有相同属性的子元素父元素不同时:

header p:hover,
main p:hover,
footer p:hover {
  color: red;
  cursor: pointer;
}

使用 :is 之后:

:is(header, main, footer) p:hover {
  color: red;
  cursor: pointer;
}

:is demo

<ul class="css-selectors css-selectors-is">
  <li>
    <div>div</div>
    <p>p</p>
  </li>

  <li>
    <div>div</div>
    <span>span</span>
  </li>

  <li>
    <div>div</div>
    <i> tag i </i>
  </li>

  <li>
    <section>
      <i>deep level i</i>
    </section>
  </li>
</ul>

若将 li 内的 div,span,p,i 元素的颜色设置颜色时,之前写法

.css-selectors-is > li > div,
.css-selectors-is > li > span,
.css-selectors-is > li > p,
.css-selectors-is > li > i {
  background-color: #0a9caf;
}

使用 :is 之后:

.css-selectors-is > li > :is(div, span, p, i) {
  background-color: #0a9caf;
}

支持层叠嵌套使用

.css-selectors-is :is(li) > :is(div, span, p, :is(i, section)) {
  background-color: #0a9caf;
}
  • div

    p

  • div

    span
  • div

    tag i
  • deep level i

来看 MDN 提供的复杂场景:

ol ol ul,     ol ul ul,     ol menu ul,     ol dir ul,
ol ol menu,   ol ul menu,   ol menu menu,   ol dir menu,
ol ol dir,    ol ul dir,    ol menu dir,    ol dir dir,
ul ol ul,     ul ul ul,     ul menu ul,     ul dir ul,
ul ol menu,   ul ul menu,   ul menu menu,   ul dir menu,
ul ol dir,    ul ul dir,    ul menu dir,    ul dir dir,
menu ol ul,   menu ul ul,   menu menu ul,   menu dir ul,
menu ol menu, menu ul menu, menu menu menu, menu dir menu,
menu ol dir,  menu ul dir,  menu menu dir,  menu dir dir,
dir ol ul,    dir ul ul,    dir menu ul,    dir dir ul,
dir ol menu,  dir ul menu,  dir menu menu,  dir dir menu,
dir ol dir,   dir ul dir,   dir menu dir,   dir dir dir {
  list-style-type: square;
}

使用 :is 之后:

:is(ol, ul, menu, dir) :is(ol, ul, menu, dir) :is(ul, menu, dir) {
  list-style-type: square;
}

:is() 不能选择伪元素

:is() 伪类不匹配伪元素(伪类是可以的,例如::visible:focus)。所以这样不行:

some-element:is(::before, ::after) {
  display: block;
}

:is 的优先级

先看示例

<ul class="css-selectors css-selectors-is-priority">
  <li>
    <div id="is-priority-id" class="is-priority-class">id div</div>
  </li>

  <li>
    <div class="is-priority-class">class div</div>
  </li>
</ul>
.css-selectors-is-priority {
  > li {
    & > :is(div, #is-priority-id) {
      background-color: #0a9caf;
    }

    & > .is-priority-class {
      background-color: red;
    }
  }
}
  • id div

  • class div

上述 css 按照正常理解的话,应该对应下述代码,理论 div 的背景颜色应该是 红色蓝色,分别是由 li > #is-priority-idli > .is-priority-class 两个选择器作用的,但是实际上 div 的背景颜色是 蓝色,这是为什么呢?

li > div {
    background-color: #0a9caf;
}
li > #is-priority-id {
    background-color: #0a9caf;
}
li > .is-priority-class {
    background-color: red;
}

:not 一样,:is 的优先级由 选择器 列表内最高的选择器决定的,需要把他们当做整体来看。
由于 is 选择器内的优先级由列表内优先级最高的决定。所以 标签选择器 div 拥有和 id 选择器相同的优先级,即也会覆盖下方的 class 选择器

:is 兼容性

can i use

:where

:where 选择器其实和 :is 几乎一样,除了优先级外。

<ul class="css-selectors css-selectors-where">
  <li>
    <div id="css-selectors-where-id">where</div>
  </li>
</ul>
// where 的优先级总为 0,其他与 is 一致
.css-selectors-where {
  li :is(div) {
    color: red;
  }

  li :where(#css-selectors-where-id) {
    color: green;
  }
}

正常理解下 :is(div):where(id div) 都可以转化为 li div,后面的甚至是 id,但 li 的颜色却是红色

  • where

由此可看出 :where 的优先级总为 0,其他与 :is 一致

:has

重量级选手 :has,css 里可以往上选择的选择器。

<ul class="css-selectors css-selectors-has">
  <li>
    <div>
      <p>has - p</p>
    </div>
  </li>

  <li>
    <div>
      <a>
        <span>has - span</span>
      </a>
    </div>
  </li>

  <li>
    <div>
      <span>
        <i>has - i</i>
      </span>
    </div>
  </li>
</ul>
/*找自己*/
.css-selectors-has li:has(>div >p) {
    border: 1px solid red;
}

/*找前面的兄弟*/
.css-selectors-has li:has(+ li >div >span >i) {
    border: 1px solid green;
}
  • has - p

  • has - i

li:has(>div >p) { border: 1px solid red; } 选择器,选择的是 li 里面有 div 里面有 pli,所以第一个 li 会有红色边框
li:has(+ li >div >span >i) { border: 1px solid green; } 通过兄弟选择器 + 选择的是相邻兄弟 li 里面有 div 里面有 span 里面有 ili,所以有 span 的符合规则。

:has 选择的是参数匹配的宿主元素而不是参数本身

:has 兼容性

can i use,早用早享受~