Is there a CSS selector for the first child, taking text nodes into account?

后端 未结 4 710
刺人心
刺人心 2020-12-20 14:47

I\'m trying to find a CSS selector for an element that is the first child, taking any text nodes into account that might come before it (i.e. if any elements come before, po

相关标签:
4条回答
  • 2020-12-20 15:12

    In essence, you're asking if text can affect the styling of dom elements and the answer is - no, because text is not a dom element of it's own.

    We can prove this with a simple experiment. Just add a marker element at the beginning of the paragraph and then use a sibling selector to override color. You'll see that this works in both cases, because text has no effect on surrounding dom flow.

    For the record, I thought I was onto something by initially doing this marker experiment with ::before pseudo elements but they can't be used with sibling selectors either. Pseudo elements are not real elements and will have no effect on the relationships of actual dom tree.

    .red-if-not-first {
      color: red;
      font-weight: bold;
    }
    
    .red-if-not-first:first-child {
      color: green;
    }
    
    .marker + span{
      color: red;
    }
    <p>
      <i class="marker"></i>
      Lorem ipsum. <span class="red-if-not-first">This should be red, not green, because some content comes before it.</span> Eum natus culpa officia a molestias, sed beatae aut in autem architecto iure repellat quam placeat, expedita maxime laborum necessitatibus repudiandae. Corrupti!
    </p>
    
    <p>
      <i class="marker"></i>
      <span class="red-if-not-first">This is rightly green, not red, because it's first bit of content in this paragraph.</span> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum natus culpa officia a molestias, sed beatae aut in autem architecto iure repellat quam placeat, expedita maxime laborum necessitatibus repudiandae. Corrupti!
    </p>

    0 讨论(0)
  • 2020-12-20 15:13

    This is 2017. The answer is "No". There is no such CSS selector that can help you with this.

    0 讨论(0)
  • 2020-12-20 15:18

    One workaround could be to make use of the :empty pseudo class. You will need more markup though.

    p .red-if-not-first {
      color: red;
      font-weight: bold;
    }
    
    p > :empty + .red-if-not-first {
      color: green;
    }
    <p>
      <span>Lorem ipsum.</span> <span class="red-if-not-first">This should be red, not green, because some content comes before it.</span> Eum natus culpa officia a molestias, sed beatae aut in autem architecto iure repellat quam placeat, expedita maxime laborum necessitatibus repudiandae. Corrupti!
    </p>
    
    <p>
      <span></span> <span class="red-if-not-first">This is rightly green, not red, because it's first bit of content in this paragraph.</span> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum natus culpa officia a molestias, sed beatae aut in autem architecto iure repellat quam placeat, expedita maxime laborum necessitatibus repudiandae. Corrupti!
    </p>

    0 讨论(0)
  • 2020-12-20 15:25

    If, for some strange reason, you can make do with only supporting Gecko, you can use-moz-first-node selector to do this.

    https://developer.mozilla.org/en-US/docs/Web/CSS/:-moz-first-node

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