How do I select the first adjacent sibling?

前端 未结 8 2158
北海茫月
北海茫月 2020-12-30 19:38

I have an HTML list like so:

  • Heading 1
相关标签:
8条回答
  • 2020-12-30 20:39

    try using JS to getElementsby Classname and if more than 1 then use CSS:

    ul li:first-child {display: none;}
    
    0 讨论(0)
  • 2020-12-30 20:43

    This is now possible

    li:first-of-type {
        display: none;
    }
    

    This will match the first li tag.

    li:first-of-type:not(:only-of-type) {
        margin: 10px;
    }
    

    If you want a bit more control - such as adding space between two items only when there are more items the above would work. Mixing pseudo selectors can be very powerful. https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes

    0 讨论(0)
提交回复
热议问题