CSS选择器has()使用


记录一下关于这个选择器的一些用法,CSS 函数式伪类 :has() 表示一个元素,如果作为参数传递的任何相对选择器在锚定到该元素时,至少匹配一个元素。这个伪类通过把可容错相对选择器列表作为参数,提供了一种针对引用元素选择父元素或者先前的兄弟元素的方法。

以上是MDN对 :has()选择器的解释

:has() 选择器选择父元素和前面的兄弟元素

邻接兄弟选择器(+)用来选中恰好处于另一个在继承关系上同级的元素旁边的物件。例如,选中所有紧随<p>元素之后的<img>元素:

p + img

通用兄弟关系选择器(~)用来选中一个元素后面的所有兄弟元素。例如,选中<p>元素之后的所有的<img>元素:

p ~ img

css 并没有提供直接选择父元素或者前面的兄弟元素的选择器,但 :has() 可以做到这点。

1、比如选择所有包含 <p>元素的父元素:

:has(p)

2、选择直接后代元素包含 <p>元素的父元素:

:has(> p)

3、选择直接后代元素包含 <p>元素的父级标签名是 div 父元素:

div:has(> p)

4、选择 <p>元素的相邻的前一个标签名是 div 的兄弟元素:

div:has(+ p)

5、选择 <p>元素的前面所有标签名是 div 的兄弟元素:

div:has(~ p)

:has() 选择器中的

:has() 选择器中表示 很简单,例如:

p:has(.a):has(.b)` 表示选择同时包含子元素 `a` 和 子元素 `b` 的 元素 `p
p:has(.a, .b)` 表示选择包含子元素 `a` 或者包含子元素 `b` 的 元素 `p

:has() 选择器选择一个范围内的元素

现在有如下元素

<div>
  <h2>标题开始(选择第一行字体为绿色,最后一行字体为红色)</h2>
  <p>h2中间第一行</p>
  <h4>h2中间第二行</h4>
  <h5>h2中间最后一行</h5>
  <h2>标题结束</h2>
</div>

使用 :has() 实现上面效果,可以这么做

/* 选择 h2 中间第一行 */
h2 + :has(~ h2){
  color:green;
}
/* 选择 h2 中间最后一行 */
h2 ~ :has(+ h2){
  color:red;
}

h2 + :has(~ h2) 表示选择紧跟着 h2 的并且后面还有 h2 元素的兄弟元素。也就选择到了 h2 范围内的第一个元素。

h2 ~ :has(+ h2) 表示选择 h2 后面的兄弟元素,并且该兄弟元素的下一个兄弟元素是 h2,也就选择到了 h2 范围内最后一个元素

那如果要选择中间所有元素呢,可以这样做

/* 选择 hr 中间所有行 */
hr ~ :has(~ hr){
  color:blue;
}

:has() 选择器的应用

1、CSS :has() 选择器之星级评分

关于星级评分,之前写过一篇文章分享过 三种方式使用纯 CSS 实现星级评分

这里介绍下使用 :has() 选择器 + :not() 选择器 实现星级评分的方式。

星级评分效果包括鼠标滑入和点击,滑入或点击到第几颗星的位置,该位置之前的星高亮,之后的星不高亮或者有高亮的则取消高亮;

html 结构

<div>
  <input type="radio" name="radio" id="radio1">
  <label for="radio1">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
  <input type="radio" name="radio" id="radio2">
  <label for="radio2">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
  <input type="radio" name="radio" id="radio3">
  <label for="radio3">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
  <input type="radio" name="radio" id="radio4">
  <label for="radio4">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
  <input type="radio" name="radio" id="radio5">
  <label for="radio5">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
</div>

为了使星星有点击效果,利用 radio + label 的方式实现点击效果;label 代表星星。

当点击星星时,高亮当前星星


input:checked + label{
    color:gold;
}

当鼠标移入星星时,高亮当前星星,并且该位置之后的星星取消高亮;


label:hover{
  color:gold;
  & ~ label{
    color:#ccc!important;
  }
}

让当前位置之前的所有星星也高亮,可以利用 :not ,排除掉当前位置和当前位置之后的星星。

css
label:not(:hover,:hover ~ *){
  color:gold;
}

并且只有鼠标滑入时添加这些效果。

css
div:has(label:hover) label:not(:hover,:hover ~ *){
  color:gold;
}

同样,当点击星星时,点亮当前选择的之前所有的星星也如此

css
div:has(input:checked) label:not(input:checked ~ label){
  color:gold;
}

2、CSS :not 和 :has() 模拟 :only-of-type

有下面的 html 结构

<div>
  <p>第一页</p>
  <p class="this">第二页</p>
  <p>第三页</p>
  <p>第四页</p>
</div>

要选择类名为 this 的元素,并设置颜色为红色,使用 .this{color:red;} 可以轻松做到。

如果现在有两个 div 元素块

<div>
  <p>第一页</p>
  <p class="this">第二页</p>
  <p>第三页</p>
  <p>第四页</p>
</div>

<div>
  <p>第一页</p>
  <p class="this">第二页</p>
  <p class="this">第三页</p>
  <p>第四页</p>
</div>

现要求选择 div 的子元素中只有含有一个类名为 this 的元素(也就是第一个 div 元素块),并且设置其颜色为红色,该怎么做呢?

:only-of-type 代表了任意一个元素,这个元素没有其他相同类型的兄弟元素。

但 :only-of-type 判断是否有相同类型的依据是标签名,而不是类名。所以并不能达到想要的效果。

css
//这种写法是无效的,无法判断元素有没有其他相同的类名。
.this:only-of-type {
    color:red;
}

//这种写法是有效的,但判断的是没有相同的 p 的元素,显然无法满足上面的要求,但能匹配下面 ul 中的 p
p:only-of-type {
    color:red;
}

<ul>
  <li>第一页</li>
  <li class="this">第二页</li>
  <li class="this">第三页</li>
  <p>第四页</p>
</ul>

而 :has 能做到,要选择前后没有相同类名的元素 ,也就是排除前后的 .this 。

排除前面的 .this

// 表示选择前面没有 .this 的 .this
.this:not(.this ~)

排除后面的 .this,

// 表示排除后面有 .this 的 .this
.this:not(:has(~ .this))

两个做并集,也就选择到了唯一的 .this

.this:not(:has(~ .this)):not(.this ~ *){
  color:red;
}

总结

本文介绍了 :has() 选择器的基本用法以及四个实际应用;

  • 选择父元素和前面的兄弟元素
  • :has() 选择器中的
  • 选择一个范围内的元素

:has() 选择器出来之前,使用 CSS 是无法直接选择到父级元素和前面的兄弟元素的,但 :has() 选择器的出现使这个变成了可能;


文章作者: feico
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 feico !
评论
  目录