How do I select the first adjacent sibling?

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

I have an HTML list like so:

  • Heading 1
8条回答
  •  一生所求
    2020-12-30 20:35

    It is possible to target first sibling with CSS, with some limitations.

    For the example in the question it could be done with something like this

    li.heading { display: none; }                   /* apply to all elements */
    li.heading + li.heading { display: list-item }  /* override for all but first sibling */
    

    This works, but requires that you can explicitly set the styles on the siblings to override the setting for first child.

提交回复
热议问题