【发布时间】:2014-04-07 08:13:29
【问题描述】:
我遇到了一个让我感到困惑的场景 - :last-child 的使用会影响父类的应用方式。
我有一个元素列表,目标是对最后一个元素应用一些样式。
但是,当我使用:last-child 时,样式的优先级发生了变化,并且其中一个父类停止工作,只有!important 解决了这个问题。
我在这里做了一个简单的演示: http://jsfiddle.net/wC2AX/1/
HTML:
<div class="hover">
<div class="focus_on_last_child" style="background-color:red; width:100px; height:100px">
<div class="attribution">
</div>
</div>
</div>
CSS:
/*this should be applied on hover always*/
.hover:hover .attribution{
background-color: black; /*try adding !important*/
bottom: 0px;
}
/*basic properties*/
.attribution {
height: 60px;
overflow: hidden;
position: absolute;
bottom: -60px;
width: 100px;
}
/*depending on a screen size styles are changed*/
.focus_on_last_child:last-child .attribution { /*try removing :last:child*/
background-color: pink;
bottom: -30px;
}
这个例子有点愚蠢,应该改变悬停样式的想法。但它仅在使用!important 或删除:last-child 时才有效。
感谢您的任何建议!
【问题讨论】: