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

后端 未结 4 729
刺人心
刺人心 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: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;
    }

    Lorem ipsum. This should be red, not green, because some content comes before it. Eum natus culpa officia a molestias, sed beatae aut in autem architecto iure repellat quam placeat, expedita maxime laborum necessitatibus repudiandae. Corrupti!

    This is rightly green, not red, because it's first bit of content in this paragraph. 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!

提交回复
热议问题