简体   繁体   English

div中选择器前后的CSS

[英]CSS before and after selector inside div

I am trying to use after and before selectors to apply certain style to children of a div, my code is as follows : 我正在尝试使用选择器之后和之前将某些样式应用于div的子代,我的代码如下:

<div class="outer-container">
    <a href="#" class="links">Link 1</a>
    <a href="#" class="links">Link 2</a>
    <a href="#" class="links">Link 3</a>
    <span class="mid-divider">Some Text</span>
    <a href="#" class="links">Link 4</a>
    <a href="#" class="links">Link 5</a>
    <a href="#" class="links">Link 6</a>
    <span class="last-divider">Some Text</span>
    <a href="#" class="links">Link 7</a>
    <a href="#" class="links">Link 8</a>
    <a href="#" class="links">Link 9</a>
</div>

.outer-container .mid-divider::before{
    //some styles
}
.outer-container .mid-divider::after{
    //some styles
}

I want to apply styles to link1, link2 and link3 as well as to links7,8 and 9. But the selector is not working. 我想将样式应用于link1,link2和link3以及link7,8和9。但是选择器不起作用。

You don't need the 您不需要

::before ::之前

or 要么

:: after ::之后

tag in this case, just call the elements like this: 在这种情况下,只需调用以下元素:

.outer-container a{} .outer-container a {}

.mid-divider a{} .mid-divider a {}

.last-divider a{} .last-divider a {}

try using this 尝试使用这个

.outer-container a:nth-child(n+7), .outer-container a:nth-child(-n+3) { }

.outer-container a:nth-child(n+7) <-- this selects all children after the 7th child .outer-container a:nth-​​child(n + 7)<-选择第7个孩子之后的所有孩子

.outer-container a:nth-child(-n+3) <-- this selects all children before the 4th child. .outer-container a:nth-​​child(-n + 3)<-选择第四个孩子之前的所有孩子。

check this link out. 检查此链接。 Shows a variety of ways to target different child elements http://css-tricks.com/useful-nth-child-recipies/ 显示了针对不同子元素的多种方法http://css-tricks.com/useful-nth-child-recipies/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM