定位 CSS 类的第二个实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13193224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Target the 2nd instance of a CSS Class
提问by thatuxguy
Due to the solution i am using, i only have limited CSS classes. I would like to know if there is a way i can target the 2nd class, when there are 2 classes of the same name.
由于我使用的解决方案,我只有有限的 CSS 类。我想知道当有 2 个同名的班级时,是否有一种方法可以定位第二个班级。
I have 2 instances of media-link contained withing a DIV called item. I need to basically add additional styles to the 2nd instance of the class. However this is where the difficulty comes in... I can not add any additional code, or classes to the code! I know, not ideal but its down to the solution being used, not personal choice.
我有 2 个媒体链接实例,其中包含一个名为 item 的 DIV。我基本上需要向类的第二个实例添加其他样式。然而,这就是困难所在......我无法向代码添加任何额外的代码或类!我知道,这并不理想,但这取决于所使用的解决方案,而不是个人选择。
Example code:
示例代码:
<a href="#" class="media-link" style="display: block">
<img src="images/ph-a.png" alt="">
</a>
<a href="#" class="media-link">
<img src="images/auth-2-100px.png" alt="">
<p class="author">Author: John Smith</p>
</a>
Thanks in advance for any suggestions, or solutions.
在此先感谢您的任何建议或解决方案。
回答by Rohit Azad
Hi now used to this
嗨,现在习惯了
.media-link:nth-child(2) {
// here style
}
or
或者
.media-link:nth-of-type(2){
// here style
}
回答by Shailender Arora
you can use nth-child pseudo classes
for your requirement....
您可以nth-child pseudo classes
根据您的要求使用....
.media-link:nth-child(2) {
color:red;
}