John David

John David


CSS :empty Selector

Often, we want to style elements that contain content. How about when an element has no children or text at all? Easy, you can use the :empty selector 🤩

<p> </p><!-- NOT empty: note the blank space -->
<p></p><!-- YES empty: nothing inbetween -->

p::before {
 font-family: "FontAwesome";
 content: "\f240";

p:empty::before {
 content: “\f244”;
p {
 color: silver;

p:empty {
 color: red;

What’s considered empty?

When I first encounter this, there was a few confusion about what this property considers as empty. Let’s stick with MDN’s definition here:

The :empty CSS pseudo-class represents any element that has no children. Children can be either element nodes or text (including whitespace). Comments, processing instructions, and CSS content do not affect whether an element is considered empty.

Is empty

As long as there is no whitespace, it’s an empty element.


Comment in between is also considered an empty element. As long as there is no whitespace.

<p><!-- comment --></p>

Not empty

Whitespace is considered not empty. Even in a new line, there is whitespace, so not empty! Emphasizing on this, cause I made the same mistake 😅

<p> </p>

<!-- comment -->

Having children element also counts as not empty


Whitespace in Future Spec

The good news is – in Selectors Level 4, whitespace would be considered empty! This will make it similar to act like :-moz-only-whitespace. In other words, this would considered empty:

<!-- Considered Empty in CSS Selectors Level 4 -->
<p> </p>

⚠️ BUT, don’t do this yet. Currently no browser supports this.

Examples using :empty

Okay, let’s take a look at some real-life examples of using :empty.

Using :empty in Form Error Message

This is the example that made me first discovered :empty. So I wanted to prepend a ❌ icon on my error message. But the problem is the icon appeared even when I had no error message. But then no problem, I can just use the :empty to only append the icon when there IS an error message 👍


.error:before {
 color: red;
 content: "\0274c "; /* ❌ icon */


<!-- No error message -->
<div class=“error”></div>

<!-- Yes error message -->
<div class=“error”>Missing Email</div>


Without Empty

❌ Missing Email

With :empty

❌ Missing Email

Using :empty in Alerts

Here’s another example using :empty to hide empty state.

.alert {
 background: pink;
 padding: 10px;
.alert:empty {
 display: none;


<div class=“alert”></div>
<div class=“alert”>Alert Message</div>


Without empty
Alert Message
With :empty
Alert Message

Browser Support

Support on this is actually really good. It supports all the way to Internet Explorer 9 🙌

Community Examples

I discovered :empty when I was trying to style empty error messages in a form. <div class=“error”></div>. No JS required anymore, I can use pure CSS 👍

What other use cases do you know?

  • @delusioninabox: A fix to remove janky spacing of empty paragraph tags, generally from user-created content. 😅
  • @hkfoster: I’ve used it to squash any number of randomly generated selectors that I can’t weed out on the templating side. 👍
  • @sumurai8: Removing padding from empty paragraphs
  • @_ottenga: Nice for notification dot (if empty is not visible, if has some number - for example - is red with the number inside)
  • @stephenjbell: li:empty { visibility:hidden; } Let an empty list item act as kind of a paragraph break (no bullet) in the middle of a list.
  • @jlabs: I’ve used this recently within ul that show’s a message when the ul has no children - the list was populated via JS.

Community Input

  • @bourhaouta: One more thing about :empty it doesn’t select elements that contain whitespace, in the other side we have :blank but it’s not well supported yet 😔
  • @link2twenty: I love seeing little known features like this getting some spotlight! It’s probably worth noting when the level 4 selectors roll out white space will be included as empty 🙂

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Learn More

The Web Developer Bootcamp

Build Responsive Real World Websites with HTML5 and CSS3

Advanced CSS and Sass: Flexbox, Grid, Animations and More!

Web Design for Beginners: Real World Coding in HTML & CSS

HTML5 and CSS3 Fundamentals: Development for Absolute Beginners

How to build and send a HTML Email 2019

Using Web Workers to Real-time Processing

How to build Desktop Apps with HTML, CSS and Javascript ?

34 Free HTML And CSS Books

Building a Simple URL Shortener With HTML and Javascript


What is GEEK

Buddha Community

CSS :empty Selector

Hire CSS Developer

Want to develop a website or re-design using CSS Development?

We build a website and we implemented CSS successfully if you are planning to Hire CSS Developer from, We can fill your Page with creative colors and attractive Designs. We provide services in Web Designing, Website Redesigning and etc.

For more details…!!
Consult with our experts:-

#hire css developer #css development company #css development services #css development #css developer #css

Alayna  Rippin

Alayna Rippin


Creating a CSS Visual Cheatsheet

The other day one of our students asked about possibility of having a CSS cheatsheet to help to decide on the best suited approach when doing this or that layout.

This evolved into the idea of making a visual CSS cheatsheet with all (most) of the common patterns we see everyday and one of the best possible conceptual implementation for them.

In the end any layout could and should be split into parts/blocks and we see every block separately.

Here is our first take on that and we would be happy to keep extending it to help us all.

Please, send you suggestions in the comments in community or via gitlab for the repeated CSS patterns with your favourite implementation for that so that we will all together make this as useful as it can be.

#css #css3 #cascading-style-sheets #web-development #html-css #css-grids #learning-css #html-css-basics

Aisu  Joesph

Aisu Joesph


CSS Alignment Made Simple

CSS is seen as an impediment in web development for many of us. Most of the time it looks like even when you follow the rules and everything seems clear, it still doesn’t work the way you want it to.

Therefore, the purpose of this article is to make some features of CSS much easier to understand.

The thing I want to address now is the alignment of the elements.

Without further ado, here are some of the most common scenarios one might encounter when it comes to this topic and how they can be approached.

#css-center #css-position #css-flexbox #css-center-image-in-a-div #css

This CSS Cut Out Effect is Guaranteed to Blow Your Mind 🤯

This effect is so cool and just fun to see. What it comes down to is having a background image show through the text.

How it works is that we will have a div that will have the image as a background. On that, we put our text element, using blend-mode it will show through the image.

The result you can see and touch on this Codepen.

#css #css3 #html-css #css-grids #learning-css #html-css-basics

CSS Animation: translate3d, backdrop-filter, and Custom Tags

In this tutorial, we are going to learn:

  • how to create a smooth animation using the CSS transform translate3d prop.
  • why we’d want to use the cubic-bezier transition timing function and this function’s benefits.
  • how and why we use custom tags.
  • if you watch the video to the end, I also provide a bonus tip on using backdrop-filter to style some frost/blur style on background.

#css #css animation #css / style sheets #css animations #css background