【问题标题】:CSS HTML last child not working?CSS HTML 最后一个孩子不工作?
【发布时间】:2014-02-06 20:26:16
【问题描述】:

您好,我希望类中的所有第 thirth div : highLightBoxSmall 具有 margin-right: 0;

这是我的html:

        <a href="assortimentoverzicht.html"><div class="highLightBoxSmall"> <img src="images/style/HighLightAssortiment1.png" class="highLightsImg mid" /> <span class="highLightHeader">Monitoren</span></div></a>
        <a href="assortimentoverzicht.html"><div class="highLightBoxSmall"> <img src="images/style/HighLightAssortiment2.png" class="highLightsImg mid" /> <span class="highLightHeader">Monitoren</span></div></a>
        <a href="assortimentoverzicht.html"><div class="highLightBoxSmall"> <img src="images/style/HighLightAssortiment2.png" class="highLightsImg mid" /> <span class="highLightHeader">Monitoren</span></div></a>
        <a href="assortimentoverzicht.html"><div class="highLightBoxSmall"> <img src="images/style/HighLightAssortiment1.png" class="highLightsImg mid" /> <span class="highLightHeader">Monitoren</span></div></a>
        <a href="assortimentoverzicht.html"><div class="highLightBoxSmall"> <img src="images/style/HighLightAssortiment2.png" class="highLightsImg mid" /> <span class="highLightHeader">Monitoren</span></div></a>
        <a href="assortimentoverzicht.html"><div class="highLightBoxSmall"> <img src="images/style/HighLightAssortiment2.png" class="highLightsImg mid" /> <span class="highLightHeader">Monitoren</span></div></a>
        <a href="assortimentoverzicht.html"><div class="highLightBoxSmall"> <img src="images/style/HighLightAssortiment1.png" class="highLightsImg mid" /> <span class="highLightHeader">Monitoren</span></div></a>
        <a href="assortimentoverzicht.html"><div class="highLightBoxSmall"> <img src="images/style/HighLightAssortiment2.png" class="highLightsImg mid" /> <span class="highLightHeader">Monitoren</span></div></a>
        <a href="assortimentoverzicht.html"><div class="highLightBoxSmall"> <img src="images/style/HighLightAssortiment2.png" class="highLightsImg mid" /> <span class="highLightHeader">Monitoren</span></div></a>

这是我的 CSS:

.highLightBoxSmall:nth-child(3n+3) { 
margin-right: 0; 
}

当我删除它时它确实有效,但它不起作用。

当我尝试这个时:

.highLightBoxSmall a:nth-child(3n+3) { 
margin-right: 0; 
}

或者这个

.highLightBoxSmall a:link:nth-child(3n+3) { 
margin-right: 0; 

}

一切都不起作用。

提前致谢。

【问题讨论】:

    标签: javascript jquery html css css-selectors


    【解决方案1】:

    问题是您使用了 无效标记 (如果不使用 HTML5),您无法使用a标签..

    所以考虑改变标记,并使用下面的选择器..

    div.highLightBoxSmall:nth-of-type(3n) {
        background: #f00;
    }
    

    Demo

    如果你想定位a,而不仅仅是在选择器末尾添加a

    div.highLightBoxSmall:nth-of-type(3n) a {
        /* Styles goes here */
    }
    

    如果你想坚持你的标记

    (我会考虑改变)
    a:nth-of-type(3n) div.highLightBoxSmall {
        background: #f00;
    }
    

    Demo

    注意:确保将a 标记包装在具有idclass 的容器元素中,以唯一定位a 元素。因此,假设如果您要使用上述解决方案,请将内容包装在 div 中,例如使用 classselect_threes,因此请使用下面的选择器..

    .select_threes a:nth-of-type(3n) div.highLightBoxSmall {
        background: #f00;
    }
    

    如果您只定位a 标记,则可以去掉div.highLightBoxSmall


    另外,想告诉您,您所有的选择器都不正确,在每个选择器中,您选择的是 a 标签,嵌套在具有 .highLightBoxSmall class 的元素下,但没有一个。

    这里最好使用nth-of-type()而不是nth-child()

    【讨论】:

    【解决方案2】:

    您的代码有很多问题。

    • .highLightBoxSmall里面没有anchor,所以选择器错误。
    • 您正在请求一个元素中的第 n 个子元素,该元素没有多个相同元素。

    您需要一个围绕一组锚点的容器:

    <div id="containerId">
        <a class="anchorClass"></a>
        <a class="anchorClass"></a>
        <a class="anchorClass"></a>
        <a class="anchorClass"></a>
        <a class="anchorClass"></a>
        <a class="anchorClass"></a>
    </div>
    

    然后你可以创建一个选择器如下:

    #containerId a.anchorClass:nth-of-type(3n){
        margin-right: 0; 
    }
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签