css父元素选择器

木头的喵喵拖孩

今天发现一个css父选择器写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.wrapper {
width: 100px;
height: 100px;
border: 2px solid green;
}
.inner {
width: 80px;
height: 80px;
border: 2px solid blue;
}
.heart {
width: 60px;
height: 60px;
border: 2px solid red;
}

/* 父选择器,选择了.inner元素的父元素.wrapper */
.wrapper:has(.inner) {
border-top-left-radius: 50%;
}

/* 链式调用,实现祖先选择器 */
.wrapper:has(.inner):has(.heart) {
border-bottom-right-radius: 50%;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="inner">
<div class="heart"></div>
</div>
</div>
</body>
</html>
  • 标题: css父元素选择器
  • 作者: 木头的喵喵拖孩
  • 创建于: 2023-05-18 16:00:19
  • 更新于: 2024-05-21 10:56:15
  • 链接: https://blog.xx-xx.top/2023/05/18/css父元素选择器/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
css父元素选择器