模板下载吧 > 快速入门 > 详解CSS中:nth-child的用法

详解CSS中:nth-child的用法

阿里云

前端的哥们想必都接触过css中一个神奇的玩意,可以轻松选取你想要的标签并给与修改添加样式,是不是很给力,它就是“:nth-child”。

下面我将用几个典型的实例来给大家讲解:nth-child的实际用途:

Tips:还用低版本的IE浏览器的哥们请绕过!

:nth-child(2)选取第几个标签,“2可以是你想要的数字”

<span class="pun">.</span><span class="pln">demo01 li</span><span class="pun">:</span><span class="pln">nth</span><span class="pun">-</span><span class="pln">child</span><span class="pun">(</span><span class="lit">2</span><span class="pun">){</span><span class="pln">background</span><span class="pun">:</span><span class="com">#090}</span>

:nth-child(n+4)选取大于等于4标签,“n”表示从整数,下同

点此查看实例展示

<span class="pun">.</span><span class="pln">demo01 li</span><span class="pun">:</span><span class="pln">nth</span><span class="pun">-</span><span class="pln">child</span><span class="pun">(</span><span class="pln">n</span><span class="pun">+</span><span class="lit">4</span><span class="pun">){</span><span class="pln">background</span><span class="pun">:</span><span class="com">#090}</span>

:nth-child(-n+4)选取小于等于4标签

点此查看实例展示

<span class="pun">.</span><span class="pln">demo01 li</span><span class="pun">:</span><span class="pln">nth</span><span class="pun">-</span><span class="pln">child</span><span class="pun">(-</span><span class="pln">n</span><span class="pun">+</span><span class="lit">4</span><span class="pun">){</span><span class="pln">background</span><span class="pun">:</span><span class="com">#090}</span>

:nth-child(2n)选取偶数标签,2n也可以是even

点此查看实例展示

<span class="pun">.</span><span class="pln">demo01 li</span><span class="pun">:</span><span class="pln">nth</span><span class="pun">-</span><span class="pln">child</span><span class="pun">(</span><span class="lit">2n</span><span class="pun">){</span><span class="pln">background</span><span class="pun">:</span><span class="com">#090}</span>

:nth-child(2n-1)选取奇数标签,2n-1可以是odd

点此查看实例展示

<span class="pun">.</span><span class="pln">demo01 li</span><span class="pun">:</span><span class="pln">nth</span><span class="pun">-</span><span class="pln">child</span><span class="pun">(</span><span class="lit">2n</span><span class="pun">-</span><span class="lit">1</span><span class="pun">){</span><span class="pln">background</span><span class="pun">:</span><span class="com">#090}</span>

:nth-child(3n+1)自定义选取标签,3n+1表示“隔二取一”

点此查看实例展示

<span class="pun">.</span><span class="pln">demo01 li</span><span class="pun">:</span><span class="pln">nth</span><span class="pun">-</span><span class="pln">child</span><span class="pun">(</span><span class="lit">3n</span><span class="pun">+</span><span class="lit">1</span><span class="pun">){</span><span class="pln">background</span><span class="pun">:</span><span class="com">#090}</span>

:last-child选取最后一个标签

点此查看实例展示

<span class="pun">.</span><span class="pln">demo01 li</span><span class="pun">:</span><span class="kwd">last</span><span class="pun">-</span><span class="pln">child</span><span class="pun">{</span><span class="pln">background</span><span class="pun">:</span><span class="com">#090}</span>

:nth-last-child(3)选取倒数第几个标签,3表示选取第3个

点此查看实例展示

<span class="pun">.</span><span class="pln">demo01 li</span><span class="pun">:</span><span class="pln">nth</span><span class="pun">-</span><span class="kwd">last</span><span class="pun">-</span><span class="pln">child</span><span class="pun">(</span><span class="lit">3</span><span class="pun">){</span><span class="pln">background</span><span class="pun">:</span><span class="com">#090}</span>

ok,:nth-child的这些用法在实际中很用得着,不用单独给需要选取的标签加上ID或Class,是不是很拉轰!





上一篇:

下一篇:

详解CSS中:nth-child的用法:等您坐沙发呢!

大牛,别默默的看了,快来点评一下吧!:)。

您必须登录后才能发表评论哦!:)

QQ登录 微博登录
wordpress自适应高级图片shejigh主题

Hi, 欢迎加入模板下载吧技术交流群,带你装逼带你飞!

我要入群

模板下载吧,累计帮助1000+用户成功建站,为草根创业提供助力!

立刻开启你的建站之旅
现在加入模板下载吧,注册一个账号