CSS last-child(-1) [英] CSS last-child(-1)

查看:300
本文介绍了CSS last-child(-1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个css选择器,让我选择列表中最前面的子项。

I am looking for a css selector that lets me select the pre-last child of list.

<ul>
     <li>1</li>
     <li>2</li>
     <li>3</li>
     <li>4</li>
     <li>5</li> <!-- select the pre last item dynamically no matter how long this list is -->
     <li>6</li>
</ul>

静态方法

ul li:nth-child(5)

动态方法:

ul li:last-child(-1)

这当然不验证,也是nth-last-child似乎不提供一个动态的方式。可以回退到javascript,但我想知道是否有一个css的方式我忽略了

which of course doesn't validate, also nth-last-child doesn't seem to provide a dynamic way.. I can fallback to javascript but I'm wondering if there is a css way I overlooked

推荐答案

>使用 :nth-​​last-child() ;事实上,除了 :nth-​​last-of-type( ) 我不知道你还能用什么。我不知道你是什么意思是动态的,但如果你的意思是样式是否适用于新的第二个孩子,当更多的孩子被添加到列表,是的,它会的。 互动式小提琴

ul li:nth-last-child(2)

这篇关于CSS last-child(-1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆