How to use CSS first-child but excluding text content

后端 未结 4 1782
渐次进展
渐次进展 2021-01-19 01:38

This snippet doesn\'t work the way I expect:

4条回答
  •  广开言路
    2021-01-19 02:08

    Your code will apply the style to any span element that is the first child of its parent. In both cases, your span is the first child of its parent li.

    You don't want the first child span in this case, you want the first child li.

    div li:first-child span {
      font-weight: bold;
    }
    

    EDIT: I seem to have misunderstood your question, as it more relates to why the second span is bold, despite being preceded by text. As you presumed, text is not seen as a child element as the text node is technically part of its parent li. Your code will work in the case of defined DOM siblings like other

    s or s, however I am not sure there's a sufficient way to say "If preceded by any text" without implementing javascript.

提交回复
热议问题