1

I'm looking for a way to add a :content pseudo-element to an empty element and ran into the :empty CSS pseudo-selector which looks like it would serve the purpose I'm looking for. Basically, I want to add a generic message like "Nothing found" :after an empty element like a list.

For whatever reason my attempts to style empty elements aren't working, here's an example: https://codepen.io/kylegill/pen/ZVLEBg

This is the Markup/HTML:

<ul class="list">
</ul>
<!-- ^ Not getting selected by :empty:after  -->
<ul class="list">
  <li>This list is not empty.</li>
</ul>

And the CSS:

.list:empty:after {
  content: "Nothing found";
}
L. Alejandro M.
  • 619
  • 6
  • 14
kylegill
  • 314
  • 1
  • 12

1 Answers1

1

I found this article that helped answer my question.

The :empty class has some weird behavior and won't select elements with a space or a return/line break in them.

Ex: <div> </div> won't be selected, but <div></div> will.

The CSS4 spec includes :blank in this use case, which will select elements with whitespace, where :empty will not.

Here's an updated example:

https://codepen.io/kylegill/pen/vvgYZe

kylegill
  • 314
  • 1
  • 12