2

I want a CSS selector that will only give me the first 3 <li>s below that has no dom children. :empty doesn't work because it text are considered children.

 <ul>
  <li>I want this one</li>
  <li>I want this one</li>
  <li>I want this one</li>
  <li><a href="...">Not this one</a></li>
  <li><a href="...">Not this one</a></li>
</ul>
stackjlei
  • 7,747
  • 12
  • 42
  • 95

1 Answers1

1

You can't do that as a general rule, as you would need a parent selector, but...

...as you want to target text only, you can use the :empty selector in combination with an attribute and a pseudo element

ul li:empty::after {
  content: attr(data-text);
  color: red;
  background: lightgray;
}
<ul>
  <li data-text="I want this one"></li>
  <li data-text="I want this one"></li>
  <li data-text="I want this one"></li>
  <li><a href="...">Not this one</a></li>
  <li><a href="...">Not this one</a></li>
</ul>
Ason
  • 79,264
  • 11
  • 79
  • 127