学习css 伪类:has

学习抖音: @渡一前端提薪课

首先我们看下:**has(selector)**是什么

匹配包含(相对于 selector 的 :scope)指定选择器的元素。可以认为 selector 的前面有一个看不见的 :scope 伪类。它的强大之处是,可以实现父选择器和前面兄弟选择器的功能。支持所有的 CSS 选择符。

我们举个例子:

css

css 复制代码
  .box1:has(p) {
    color: red;
}
        
.box1:has(span) {
    color: blue;
}
.box1:has(a, div) {
    color: greenyellow;
}

html

html 复制代码
<body>

    <div class="box1">
        <p>ppp</p>
    </div>
    <div class="box1">
        <span>span</span>
    </div>
    <div class="box1">
        <a>有p</a>
    </div>
    <div class="box1">
        <div>有div</div>
    </div>
</body>

结果,.box1:has(p) 就是包含 p 标签的.box1 元素。.box1:has(span)就是包含span的元素,.box1:has(a, div) 就是 包换 a 和包含div的元素

进阶

知道这个原理后我们在进阶一下,看看 子的伪类可不可以用。

css

css 复制代码
.box1:has(.red:hover) {
    color: red;
}
.box1:has(.blue:hover) {
    color: blue;
}
.box1:has(.yellow:hover) {
    color: yellow;
}

html

html 复制代码
<div class="box1">
    <div class="red">变红色</div>
    <div class="blue">变蓝色</div>
    <div class="yellow">变黄色</div>
</div>

结果:

基于这个特效 我们做一个案例

案例

直接上代码

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo-2</title>

    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .container {
            width: 400px;
            height: 400px;
            border: 2px solid #999;
            position: absolute;
            top: 100px;
            left: 50%;
            transform: translateX(-50%);
            display: grid;
            transition: 0.5s;
            grid-template-columns: 1fr 1fr 1fr;
            grid-template-rows: 1fr 1fr 1fr;
            gap: 5px
        }
        
        .item:nth-of-type(1) {
            background: red;
        }
        
        .item:nth-of-type(2) {
            background: rgb(255, 208, 0);
        }
        
        .item:nth-of-type(3) {
            background: rgb(0, 255, 0);
        }
        
        .item:nth-of-type(4) {
            background: rgb(166, 255, 0);
        }
        
        .item:nth-of-type(5) {
            background: rgb(170, 88, 224);
        }
        
        .item:nth-of-type(6) {
            background: rgba(0, 238, 255, 0.726);
        }
        
        .item:nth-of-type(7) {
            background: rgb(255, 115, 0);
        }
        
        .item:nth-of-type(8) {
            background: rgb(86, 75, 241);
        }
        
        .item:nth-of-type(9) {
            background: rgb(255, 0, 149);
        }
        
        .container:has(.item:nth-of-type(1):hover) {
            grid-template-columns: 2fr 1fr 1fr;
            grid-template-rows: 2fr 1fr 1fr;
        }
        
        .container:has(.item:nth-of-type(2):hover) {
            grid-template-columns: 1fr 2fr 1fr;
            grid-template-rows: 2fr 1fr 1fr;
        }
        
        .container:has(.item:nth-of-type(3):hover) {
            grid-template-columns: 1fr 1fr 2fr;
            grid-template-rows: 2fr 1fr 1fr;
        }
        
        .container:has(.item:nth-of-type(4):hover) {
            grid-template-columns: 2fr 1fr 1fr;
            grid-template-rows: 1fr 2fr 1fr;
        }
        
        .container:has(.item:nth-of-type(5):hover) {
            grid-template-columns: 1fr 2fr 1fr;
            grid-template-rows: 1fr 2fr 1fr;
        }
        
        .container:has(.item:nth-of-type(6):hover) {
            grid-template-columns: 1fr 1fr 2fr;
            grid-template-rows: 1fr 2fr 1fr;
        }
        
        .container:has(.item:nth-of-type(7):hover) {
            grid-template-columns: 2fr 1fr 1fr;
            grid-template-rows: 1fr 1fr 2fr;
        }
        
        .container:has(.item:nth-of-type(8):hover) {
            grid-template-columns: 1fr 2fr 1fr;
            grid-template-rows: 1fr 1fr 2fr;
        }
        
        .container:has(.item:nth-of-type(9):hover) {
            grid-template-columns: 1fr 1fr 2fr;
            grid-template-rows: 1fr 1fr 2fr;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
</body>
<script>
</script>

</html>

效果:

相关推荐
DoraBigHead12 分钟前
this 的前世今生:谁在叫我,我听谁的
前端·javascript·面试
蓝婷儿1 小时前
每天一个前端小知识 Day 28 - Web Workers / 多线程模型在前端中的应用实践
前端
琹箐1 小时前
Ant ASpin自定义 indicator 报错
前端·javascript·typescript
小小小小小惠1 小时前
Responsetype blob会把接口接收的二进制文件转换成blob格式
前端·javascript
爱电摇的小码农1 小时前
【深度探究系列(5)】:前端开发打怪升级指南:从踩坑到封神的解决方案手册
前端·javascript·css·vue.js·node.js·html5·xss
kymjs张涛1 小时前
零一开源|前沿技术周报 #7
android·前端·ios
爱编程的喵1 小时前
React入门实战:从静态渲染到动态状态管理
前端·javascript
Tttian6222 小时前
npm init vue@latestnpm error code ETIMEDOUT
前端·vue.js·npm
患得患失9492 小时前
【前端】【组件库开发】【原理】【无框架开发】现代网页弹窗开发指南:从基础到优化
前端
运维咖啡吧2 小时前
给朋友们分享个好消息 7天时间23.5k
前端·程序员·ai编程