html – 如何使用CSS隐藏UL的前3个孩子

前端之家收集整理的这篇文章主要介绍了html – 如何使用CSS隐藏UL的前3个孩子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有以下结构:

    我想隐藏前三项.我写了下面的代码,但它只隐藏了第一个孩子而不是下两个.

    #test li:first-child
    {
        display:none;
    }
    

    我如何隐藏其他两个呢?

    最佳答案
    您可以使用nth-child选择器:

    #test li:nth-child(-n+3) {
        display: none;
    }
    

    链接的MDN文档:

    Matches if the element is one of the first three children of its
    parent

    原文链接:https://www.f2er.com/css/427613.html

    猜你在找的CSS相关文章