CSS 中功能相似伪类间的区别
导读:
CSS3 中有许多伪类选择器,其中一些伪类选择器的作用近似却又不完全一样,下面我们就来看一看到底有什么不一样。
1、:only-child 与 :only-of-type
- 测试的代码:
<div>
<p>something</p>
</div>
<div>
<div>other something</div>
<p>something</p>
</div>
:only-child是指匹配属于父元素中唯一子元素的元素。设置p:only-child { background-color:#00ff00; }后表现为:something
other somethingsomething
:only-of-type是指匹配指定元素的父元素中唯一相同类型的子元素(该父元素可以有很多子元素,而与指定匹配的元素属于同一种类型的子元素是唯一的,则为其设定样式)。设置p:only-of-type { background-color:#00ff00; }后表现为:something
other somethingsomething
2、:first-child 与 :first-of-type
- 测试的代码:
<div>
<p>something</p>
<div>other something</div>
</div>
<div>
<div>other something</div>
<p>something</p>
</div>
:first-child是指匹配的对象是其所在的父元素的第一个元素。设置p:first-child { background-color:#00ff00; }后表现为:something
other somethingother somethingsomething
:first-of-type是指匹配的对象是其所在的父素的第一个相同类型元素。设置p:first-of-type { background-color:#00ff00; }后表现为:something
other somethingother somethingsomething
3、:last-child 与 :last-of-type 可以参考 :first-child 与 :first-of-type 的思路
4、:nth-child(n) 与 :nth-of-type(n)
- 测试的代码:
<div>
<div>other something</div>
<p>something</p>
<p>something</p>
<p>something</p>
</div>
nth-child(n)是指匹配的对象是其所在的父元素的第 n 个元素。设置p:nth-child(n) { background-color:#00ff00; }后表现为:- 当 n 为 1(数字) 时:没有被选中的元素。
- 当 n 为 2(数字) 时:
other something
something
something
something
- 当 n 为 n+3(公式) 时:
other something
something
something
something
- 当 n 为 -n+3(公式) 时:
other something
something
something
something
- 当 n 为 2n(公式) 时:
other something
something
something
something
- 当 n 为 2n+1(公式) 时:
other something
something
something
something
- tips:上面中的 n 的取值可以是数字,一个关键字,一个公式(此时 n 是一个从0开始的计数器)。当 n 取值
-n+2表示小于等于2;当 n 取值n+2表示大于等于2;当 n 取值2n表示取偶数,也可以用even代替;当 n 取值2n-1表示去奇数,也可以用odd代替。 nth-of-type(n)是指匹配的对象是其所在的父素的第 n 个相同类型元素。设置p:nth-of-type(n) { background-color:#00ff00; }后表现为:- 当 n 为 1(数字) 时:
other something
something
something
something
- 当 n 为 2(数字) 时:
other something
something
something
something
- 当 n 为 n+3(公式) 时:
other something
something
something
something
- 当 n 为 -n+3(公式) 时:
other something
something
something
something
- 当 n 为 2n(公式) 时:
other something
something
something
something
- 当 n 为 2n+1(公式) 时:
other something
something
something
something
- 当 n 为 1(数字) 时:
5、:nth-last-child(n) 与 :nth-last-of-type(n) 可以参考 :nth-child(n) 与 :nth-of-type(n) 的思路
若是文中有什么错误,欢迎大家指正批评,愿与大家在问题的交流之中共同进步。愈激烈,愈深刻。
CSS 中功能相似伪类间的区别的更多相关文章
- CSS中的一些伪类
一.:nth-child 和 :nth-of-type (1):nth-child() :nth-child(n) 选择器选取某任意一父元素的第 n 个子元素( p:nth-child(n) 即选中任 ...
- 学习css常用基本层级伪类属性选择器
常见的css选择器包含:常用选择器.基本选择器.层级选择器.伪类选择器.属性选择器,其中常用选择器分为:1.html选择符*{}//给页面上所有的标签设置模式:2.类选择符.hcls{}//给clas ...
- 妙味css3课程---1-2、css3中新增的伪类和伪元素有哪些
妙味css3课程---1-2.css3中新增的伪类和伪元素有哪些 一.总结 一句话总结: 1.div:target{}是什么意思? 比如a标签的锚点链接到div,div:target{}就可以找到这个 ...
- css中px,em和rem的区别
css中px,em和rem的区别 今天,突然间发现一个特别有意思的问题,就是无意间看到一个网站中的em并不是16px,下面展开了对于px和em以及rem的探究. 首先,px是绝对长度单位,是相对于显示 ...
- css中word-break、word-wrap和white-space的区别
css中word-break.word-wrap和white-space的区别 :https://baijiahao.baidu.com/s?id=1578623236521030997&wf ...
- 前端笔记之HTML5&CSS3(中)选择器&伪类伪元素&CSS3效果&渐变背景&过渡
一.CSS3选择器 CSS3是CSS的第三代版本,新增了很多功能,例如:强大的选择器.盒模型.圆角.渐变.动画.2D/3D转换.文字特效等. CSS3和HTML5没有任何关系!HTML5骨架中,可以用 ...
- CSS设计指南之伪类
伪类这个叫法源自它们与类相似,但实际上并没有类会附加到标记中的标签上.伪类分两种. UI伪类会在HTML元素处于某个状态时(比如鼠标指针位于链接上),为该元素应用CSS样式. 结构化伪类会在标记中存在 ...
- Html学习之十(CSS选择器的使用--伪类选择器)
伪类选择器 一.基于a标签. 1.:hover 选择鼠标滑过的超链接元素 2.:active 选择鼠标单击中的超链接元素 3.:link 选择未被访问的超链接元素 4.:visited 选择已经被访问 ...
- css 04-CSS选择器:伪类
04-CSS选择器:伪类 #伪类(伪类选择器) 伪类:同一个标签,根据其不同的种状态,有不同的样式.这就叫做"伪类".伪类用冒号来表示. 比如div是属于box类,这一点很明确,就 ...
随机推荐
- 在Python中处理大型文件的最快方法
我们需要处理的各种目录中有大约500GB的图像.每个图像的大小约为4MB,我们有一个python脚本,一次处理一个图像(它读取元数据并将其存储在数据库中).每个目录可能需要1-4小时才能处理,具体取决 ...
- HDU 3622 Bomb Game(2-sat)
Bomb Game Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- window.location.search 为何在url 带# 号时获取不到 ?
我们在获取url参数时,会常常用到截取参数 getUrlParam(name) { const reg = new RegExp('(^|&)' + name + '=([^&]*)( ...
- 数据存入hive数据表之前对.csv(数据集)文件的预处理以及数据的上传
对于数据集文件,在将其中的数据存入hive之前,需要将数据进行预处理. 1.删除文件第一行记录,即字段名称 sed -i '1d' raw_user //1d表示删除第1行,同理,3d表示删除第3行, ...
- mapreduce实验
代码: public class WordCount { public static void main(String[] args) throws IOException, ClassNotFoun ...
- LNMP环境安装
一.LNMP 1.使用LNMP的优点: 资源占用少 更多并发 代理服务器 热启动 稳定高效 负载均衡 邮件服务器 2.安装前准备: 2.1查看防火墙(默认开启): [root@localhost ~] ...
- 【记录】Idea "Cannot resolve symbol 'SpringBootApplication'" 错误&“找不到主类”错误
初学,有一个Spring Boot的demo需要用Idea打开.我选择导入(Import Project选项)文件,出现如题错误,且yml文件格式也不正确显示(叶子). 后面细看目录,发现一个main ...
- JMS Activemq实战例子demo
上一篇已经讲了JMS的基本概念,这一篇来上手练一练,如果对JMS基本概念还不熟悉,欢迎参靠JMS基本概. 这篇文章所使用的代码已经不是我刚入手时的代码,已经经过我重构过的代码,便于理解,并且加了很多中 ...
- vs code常用插件(python)
1.chinese 作用:vscode设置为中文. 使用方法:Ctrl+Shift+P:输入 "config":选择zh 2.python 作用:调试 3.autoDocstrin ...
- Linux下github的使用
在linux下搭建git环境 1.创建Github账号,https://github.com 2.Linux创建SSH密钥: ssh-keygen ##一直默认就可以了 3.将公钥加入到Github账 ...