当前位置:网站首页>结构伪类选择器—查找单个—查找多个—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>
边栏推荐
- Digital economy is the core of future economic development
- 软件测试面试题:为什要在一个团队中开展测试工作?
- GBase 8c 配置设置函数
- Gbase 8C backup control function (IV)
- 学习了循环碰到了编写计算n的阶乘的题目,由此引发了一系列问题,包括一些初学者常见的坑,以及如何简化代码
- Dpdk plug-in of VPP
- 【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例
- Solution of digital commerce cloud supply chain centralized purchase management system: centralized purchase system management mode, digital control of enterprise materials
- Mark's story
- 损失函数-交叉熵的原理及实现
猜你喜欢

样本不均衡-入门0

Interviewer: are you sure redis is a single threaded process?

hypermesh 圆周阵列-插件

Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步

C# 使用Abp仓储访问数据库时报错记录集

What is method and methodology: understand the underlying logic of self-improvement

Codeforces Round #807 (Div. 2) A-C题解

记录一次生产死锁

The petrochemical industry is facing the tide of rising prices, and the digital dealer distribution system platform enables dealers and stores

学习了循环碰到了编写计算n的阶乘的题目,由此引发了一系列问题,包括一些初学者常见的坑,以及如何简化代码
随机推荐
Zkrollup learning materials summary
Traversal and properties of binary trees
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
Domain Driven Design -- Terminology
53:第五章:开发admin管理服务:6:开发【admin管理员退出登录,接口】;(一个点:我们想要修改一个采用了某种编码方式的值时,新的值最好也按照这种编码方式编码后,再去修改;)
Interviewer: are you sure redis is a single threaded process?
自定义类型:结构体,枚举,联合
忘记root密码
Typescript中类的使用
Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses
sftp文件/文件夹上传服务器
Five basic data structures of redis
一种比读写锁更快的锁,还不赶紧认识一下
In the era of great changes in material enterprises, SRM supplier procurement system helps enterprises build a digital benchmark for property procurement
都在说DevOps,你真正了解它吗?
Gbase 8C database object size function (I)
The story of amen
Gbase 8C backup control function (II)
FreeRTOS kernel summary
Cloud native enthusiast weekly: the evolution of Prometheus architecture