Conditionally set style if sibling element exists in PURE CSS

This question already has an answer here:

  • CSS: Select element only if a later sibling exists 9 answers

  • In general, no. CSS reads forwards/down the DOM - it won't read backwards/up. But with this markup, you could use :not(:last-child)

    .firstElement:not(:last-child) {
      color: red
    }
    <div class="wrapper">
        <div class="firstElement">target this</div>
        <div class="secondElement"></div>
    </div>
    
    <div class="wrapper">
        <div class="firstElement">not this</div>
    </div>
    链接地址: http://www.djcxy.com/p/22854.html

    上一篇: 如何在div悬停时影响其他元素

    下一篇: 如果兄弟元素存在于PURE CSS中,则有条件地设置样式