经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
15 分钟带你感受 CSS :has() 选择器的强大 - xingba-coder
来源:cnblogs  作者:xingba-coder  时间:2024/3/25 8:52:58  对本文有异议

最近看到了许多关于 :has() 选择器的知识点,在此总结下来。

MDN 对 :has() 选择器 的解释是这样的:

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

下面一起来感受下 :has() 选择器的强大之处吧。

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

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

  1. p + img

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

  1. p ~ img

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

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

  1. :has(p)

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

  1. :has(> p)

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

  1. div:has(> p)

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

  1. div:has(+ p)

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

  1. div:has(~ p)

:has() 选择器中的

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

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

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

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

现在有如下元素

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

要求选择第一行字体为绿色,最后一行字体为红色。需要注意的是,中间元素可以是任意的

cc.png

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

  1. /* 选择 h2 中间第一行 */
  2. h2 + :has(~ h2){
  3. color:green;
  4. }
  5. /* 选择 h2 中间最后一行 */
  6. h2 ~ :has(+ h2){
  7. color:red;
  8. }

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

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

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

dd.png

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

:has() 选择器的应用

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

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

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

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

star.webp

html 结构

  1. <div>
  2. <input type="radio" name="radio" id="radio1">
  3. <label for="radio1">
  4. <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>
  5. </label>
  6. <input type="radio" name="radio" id="radio2">
  7. <label for="radio2">
  8. <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>
  9. </label>
  10. <input type="radio" name="radio" id="radio3">
  11. <label for="radio3">
  12. <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>
  13. </label>
  14. <input type="radio" name="radio" id="radio4">
  15. <label for="radio4">
  16. <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>
  17. </label>
  18. <input type="radio" name="radio" id="radio5">
  19. <label for="radio5">
  20. <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>
  21. </label>
  22. </div>

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

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

  1. input:checked + label{
  2. color:gold;
  3. }

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

  1. label:hover{
  2. color:gold;
  3. & ~ label{
  4. color:#ccc!important;
  5. }
  6. }

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

  1. label:not(:hover,:hover ~ *){
  2. color:gold;
  3. }

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

  1. div:has(label:hover) label:not(:hover,:hover ~ *){
  2. color:gold;
  3. }

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

  1. div:has(input:checked) label:not(input:checked ~ label){
  2. color:gold;
  3. }

完整示例

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

有下面的 html 结构

  1. <div>
  2. <p>第一页</p>
  3. <p class="this">第二页</p>
  4. <p>第三页</p>
  5. <p>第四页</p>
  6. </div>

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

aa.png

如果现在有两个 div 元素块

  1. <div>
  2. <p>第一页</p>
  3. <p class="this">第二页</p>
  4. <p>第三页</p>
  5. <p>第四页</p>
  6. </div>
  7. <div>
  8. <p>第一页</p>
  9. <p class="this">第二页</p>
  10. <p class="this">第三页</p>
  11. <p>第四页</p>
  12. </div>

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

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

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

  1. //这种写法是无效的,无法判断元素有没有其他相同的类名。
  2. .this:only-of-type {
  3. color:red;
  4. }
  5. //这种写法是有效的,但判断的是没有相同的 p 的元素,显然无法满足上面的要求,但能匹配下面 ul 中的 p
  6. p:only-of-type {
  7. color:red;
  8. }
  9. <ul>
  10. <li>第一页</li>
  11. <li class="this">第二页</li>
  12. <li class="this">第三页</li>
  13. <p>第四页</p>
  14. </ul>

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

排除前面的 .this

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

排除后面的 .this,

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

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

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

bb.png

完整示例

3、CSS :has() 选择器之模仿 mac 电脑 dock 栏

利用 :has() 可以选择到前面的兄弟元素的特点,还能做出下面的动画效果

aa.gif

当鼠标滑入到一个元素时,该元素放大,该元素的前一个元素和后一个元素缩小,除了这三个元素之外的其他元素缩的更小并且有一定透明度;

html 结构如下

  1. <div class="box">
  2. <div class="son">乔丹</div>
  3. <div class="son">科比</div>
  4. <div class="son">詹姆斯</div>
  5. <div class="son">奥尼尔</div>
  6. <div class="son">邓肯</div>
  7. <div class="son">卡特</div>
  8. <div class="son">麦迪</div>
  9. <div class="son">艾弗森</div>
  10. <div class="son">库里</div>
  11. <div class="son">杜兰特</div>
  12. </div>

关键 css 代码

  1. .son{
  2. ...
  3. ...
  4. ...
  5. &:hover{
  6. background-color:#67c23a;
  7. transform:scale(1.4);
  8. & + .son{
  9. transform:scale(1.1); // 后一个相邻的兄弟元素
  10. }
  11. }
  12. }

让前一个元素也缩放为原来的 1.1

  1. // 选择存在 后一个相邻的被hover的兄弟元素 的元素
  2. .son:has( + .son:hover){
  3. transform:scale(1.2);
  4. }

然后对这三个元素之外的其他元素缩放为原来的 0.8

  1. .box:has(.son:hover) .son:not(:hover, :has(+ :hover), .son:hover + *) {
  2. transform:scale(0.8);
  3. opacity:0.7;
  4. }

.box:has(.son:hover) 表示选择子元素 sonhover 时的 .box

.son:not(:hover, :has(+ :hover), .son:hover + *) 表示排除 son 元素里面被 hover 的元素,被 hover 的元素的前一个邻接的兄弟元素,被 hover 的元素的后一个邻接的兄弟元素;

完整示例

4、CSS :has() 选择器之单选题

bb.gif

这是个有趣的应用,当选择的是错误的选项时,选择题的标题和当前选择项标红。并且会给正确的选项添加动画效果提示用户这才是正确选项。

这里用 data-correct="false" 表示错误的选项,data-correct="true" 表示正确的选项。

  1. <input type="radio" name="option" data-correct="false" id="option1" />
  2. <label for="option1">Responsive design</label>
  3. <input type="radio" name="option" data-correct="true" id="option2" />
  4. <label for="option2">Responsive design</label>
  5. <input type="radio" name="option" data-correct="false" id="option3" />
  6. <label for="option3">Responsive design</label>

选择错误选项时,标红当前选项。选择正确选项时标绿当前选项。

  1. .question{
  2. --correct: #5ed235; // 正确选项的颜色
  3. --wrong: #f83d56; // 错误选项的颜色
  4. --wrong-bg: rgba(248 ,61, 86,0.8);
  5. --correct-bg: rgb(94 ,210, 53,0.8);
  6. }
  7. input[data-correct="false"]:checked + label{
  8. color: #fff;
  9. background-color: var(--wrong);
  10. border-color: var(--wrong);
  11. }
  12. input[data-correct="true"]:checked + label{
  13. color: #fff;
  14. background-color: var(--correct);
  15. border-color: var(--correct);
  16. }

选择错误选项时,标红标题; 这里用 :has 选择器获取子元素中有错误选项选中时。

  1. .question:has(input[data-correct="false"]:checked) {
  2. .questionHeader {
  3. box-shadow: inset 0 7px 0 0 var(--wrong);
  4. background-color: var(--wrong-bg);
  5. }
  6. }

并且给正确选项增加提示动画

  1. .question:has(input[data-correct="false"]:checked) {
  2. input[data-correct="true"] + label {
  3. animation: flash 2s infinite;
  4. }
  5. }
  6. @keyframes flash {
  7. 0% {
  8. background-color: white;
  9. }
  10. 25% {
  11. background-color: #5ed235;
  12. }
  13. 50% {
  14. background-color: white;
  15. }
  16. 75% {
  17. background-color: #5ed235;
  18. }
  19. 100% {
  20. background-color: white;
  21. }
  22. }

选择正确选项时,标绿标题;

  1. .question:has(input[data-correct="true"]:checked) {
  2. .questionHeader {
  3. box-shadow: inset 0 7px 0 0 var(--correct);
  4. background-color: var(--correct-bg);
  5. }
  6. }

完整示例

总结

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

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

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

如果对本文感兴趣或对你有帮助,麻烦动动你们的发财手,点点赞~

原文链接:https://www.cnblogs.com/zsxblog/p/18093007

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号