CSS Portal

:empty CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :empty pseudo-class in CSS is used to select elements that have no children at all, including text nodes. This means that an element targeted by :empty must not contain any text, whitespace, or other elements. It is a structural pseudo-class, which allows developers to style elements dynamically based on their content state. This can be particularly useful when designing layouts where the presence or absence of content affects the visual presentation.

One key characteristic of :empty is that even invisible characters or spaces within an element will prevent it from being considered empty. For example, an element with a single space character is not considered empty, which is an important detail when working with dynamic content generated by JavaScript or server-side scripts. This makes :empty different from selectors that simply check for visible content, like :first-child or :nth-child.

Developers often pair :empty with other CSS properties to improve user experience. For instance, you might apply a different background-color or display style to elements that contain no content. This is useful for highlighting missing content areas in a layout, providing placeholder styling, or even hiding elements entirely when they are empty.

Here is a simple example of using :empty:

HTML:

<div class="box"></div>
<div class="box">Content here</div>

CSS:

.box:empty {
  background-color: #f0f0f0;
  border: 2px dashed #ccc;
  min-height: 50px;
}

In this example, the first <div> will be styled with a light gray background and a dashed border because it is empty, while the second <div> with content remains unaffected. This demonstrates how :empty can be leveraged to target elements dynamically based on whether they contain content or not, offering a flexible approach to content-aware styling.

Another practical use of :empty is in conjunction with pseudo-elements such as ::before or ::after, to display placeholder text or icons in empty containers without altering the actual HTML content.

Syntax

:empty {
  /* ... */
}

Example

<div class="box">I have text inside.</div>

<div class="box"></div>

<div class="box"> </div>

<div class="box"><span></span></div>
.box {
width: 200px;
height: 50px;
border: 1px solid #ccc;
margin: 10px;
background-color: #f9f9f9;
display: flex;
align-items: center;
justify-content: center;
}

/* Target only the element with absolutely nothing inside */
.box:empty {
background-color: #ffcccc;
border: 2px dashed #ff0000;
}

/* Optional: Adding a message using generated content */
.box:empty::before {
content: "This element is empty!";
color: #cc0000;
font-size: 12px;
}

Browser Support

The following information will show you the current browser support for the CSS :empty pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.

This psuedo class is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!