头闻号

深圳市正海虹涂料有限公司

室内涂料|室外涂料|精细化学品代理加盟|防水涂料|特种涂料|艺术涂料

首页 > 新闻中心 > 科技常识:CSS3结构性伪类选择器九种写法
科技常识:CSS3结构性伪类选择器九种写法
发布时间:2023-02-01 10:17:31        浏览次数:1        返回列表

今天小编跟大家讲解下有关CSS3结构性伪类选择器九种写法 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关CSS3结构性伪类选择器九种写法 的相关资料,希望小伙伴们看了有所帮助。

一、X:nth-child(n)

Example Source Code :li:nth-child(3) {color: red;}   接下来的几个伪类选择器使用上非常类似 功能也比较接近。  :nth-child(n) 用于匹配索引值为n的子元素。索引值从1开始。  X:nth-child()用法实际上有三种变化 demo的用法是最简单的 X:nth-child()更强大的用处在于奇偶匹配 明河不展开讲 有兴趣的请看《Understanding :nth-child Pseudo-class expressions》,《CSS3 :nth-child()伪类选择器》

二、X:nth-last-child(n)

Example Source Code :li:nth-last-child(2) {color: red;}   :nth-child(n) 是从第一个开始算索引 而X:nth-last-child(n)是从最后一个开始算索引。

三、X:nth-of-type(n)

Example Source Code:ul:nth-of-type(3) {border: 1px solid black;}   nth-of-type与nth-child的效果是惊人的相似 想要更多的了解nth-of-type请看《Alternative for :nth-of-type() and :nth-child()》 :nth-of-type(N)

四、X:nth-last-of-type(n)

Example Source Code :ul:nth-last-of-type(3) {border: 1px solid black;}   :nth-last-child效果相似。

五、X:first-child Example Source Code:ul li:first-child {border-top: none;}   匹配子集的第一个元素。IE7就可以支持了 这个伪类还是非常有用的。

六、X:last-child

Example Source Code :ul > li:last-child {color: green;}   与:first-child效果相反  留意IE8支持:first-child,但不支持:last-child。

七、X:only-child

Example Source Code:div p:only-child {color: red;}    这个伪类一般用的比较少 比如上述代码匹配的是div下的有且仅有一个的p 也就是说 如果div内有多个p 将不匹配。

八、X:only-of-type

Example Source Code:li:only-of-type {font-weight: bold;}   与:only-child类似。

九、X:first-of-type Example Source Code :ul:first-of-type{font-weight: bold;}

来源:爱蒂网