当前位置:网站首页>结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
2022-07-28 00:36:00 【小琳爱分享】
① 结构伪类选择器—查找单个
常用于查找父级选择器中的子元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* 1、找到第一个子元素,并且为li标签 */
li:first-child {
/* background-color: blue; */
}
/* 2、找到最后一个子元素,并且为li标签 */
li:last-child {
/* background-color: orange; */
}
/* 3、找到第3个子元素,并且为li标签 */
li:nth-child(3) {
/* background-color: pink; */
}
/* 4、找到倒数第3个子元素,并且为li标签 */
li:nth-last-child(3) {
background-color: red;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</style>
</head>
<body>
<!-- ul>li{我是第$个li}*10 -->
<ul>
<!-- <div>私生子</div> -->
<li>我是第1个li</li>
<li>我是第2个li</li>
<li>我是第3个li</li>
<li>我是第4个li</li>
<li>我是第5个li</li>
<li>我是第6个li</li>
<li>我是第7个li</li>
<li>我是第8个li</li>
<li>我是第9个li</li>
<li>我是第10个li</li>
</ul>
</body>
</html>
② 结构伪类选择器—查找多个

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* 1、找到偶数个li----- */
li:nth-child(2n) {
/* background-color: orange; */
}
li:nth-child(even) {
/* background-color: blue; */
}
/* 2、找到奇数个li 以下三个都是----- */
li:nth-child(2n+1) {
/* background-color: orange; */
}
li:nth-child(2n-1) {
/* background-color: blue; */
}
li:nth-child(odd) {
/* background-color: pink; */
}
/* 3、找到前5个------------------------ */
li:nth-child(-n+6) {
/* background-color: orange; */
}
/* 4、找到从第5个往后------------------------ */
li:nth-child(n+5) {
background-color: red;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</style>
</head>
<body>
<!-- ul>li{我是第$个li}*10 -->
<ul>
<li>我是第1个li</li>
<li>我是第2个li</li>
<li>我是第3个li</li>
<li>我是第4个li</li>
<li>我是第5个li</li>
<li>我是第6个li</li>
<li>我是第7个li</li>
<li>我是第8个li</li>
<li>我是第9个li</li>
<li>我是第10个li</li>
</ul>
</body>
</html>
易错点

<!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>
/* 错误的写法 */
li a:first-child {
/* color: red; */
}
/* 正确的写法 */
li:first-child a {
color: red;
}
</style>
</head>
<body>
<ul>
<li><a href="#">我是第1个a标签</a></li>
<li><a href="#">我是第2个a标签</a></li>
<li><a href="#">我是第3个a标签</a></li>
<li><a href="#">我是第4个a标签</a></li>
<li><a href="#">我是第5个a标签</a></li>
<li><a href="#">我是第6个a标签</a></li>
<li><a href="#">我是第7个a标签</a></li>
<li><a href="#">我是第8个a标签</a></li>
</ul>
</body>
</html>
③nth-of-type结构伪类选择器

type用于不是一类标签的情况,可根据类和第几个来控制要改变的标签。
④伪元素

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father {
width: 300px;
height: 300px;
background-color: skyblue;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.father::before {
/* 必加的属性,一般可以设置为空,即' ' */
content: '我是一个伪元素';
/* 伪元素是行内元素,不会显示高,转换显示方式,需要设置为块元素或者行内块元素 */
display: block;
/*或者为行内块元素*/
display:inline-block;
width: 100px;
height: 100px;
background-color: orange;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</style>
</head>
<body>
<div class="father">
我是father内部的内容
</div>
</body>
</html>
边栏推荐
猜你喜欢

JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method

Lambda expressions and stream streams

go 学习02 基础知识

Uniapp summary (applet)

Solution of digital commerce cloud supply chain centralized purchase management system: centralized purchase system management mode, digital control of enterprise materials

【面试:并发篇28:volatile】有序性

Forget the root password

In the era of great changes in material enterprises, SRM supplier procurement system helps enterprises build a digital benchmark for property procurement

Five basic data structures of redis

Netease cloud copywriting
随机推荐
软件测试面试题:为什要在一个团队中开展测试工作?
交叉熵原理及实现
go 学习02 基础知识
软件测试面试题:请详细介绍一下各种测试类型的含义?
【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例
Game 302 of leetcode
数据输出-图片注释、标注
云原生爱好者周刊:Prometheus 架构演进之路
GBase 8c 备份控制函数(一)
Small bulk quantitative stock trading record | data is the source in the quantitative system, which teaches you to build a universal data source framework
GBase 8c 事务ID和快照(六)
软件测试面试题:你认为做好测试用例设计工作的关键是什么?
记录一次生产死锁
After learning the loop, I came across the problem of writing factorial of N, which caused a series of problems, including some common pitfalls for beginners, and how to simplify the code
每条你收藏的资讯背后,都离不开TA
样本不均衡-入门0
损失函数-交叉熵的原理及实现
Uniapp summary (applet)
【网站搭建】使用acme.sh更新ssl证书:将zerossl改为letsencrypt
Leetcode's 83rd biweekly match