Styling Elements with the CSS :empty Pseudo-Class

Learn how to target and style empty elements using CSS. From debugging layout issues to creating conditional layouts--discover the power of :empty.

Every web developer encounters empty elements at some point--containers that render without content, debugging artifacts, or dynamically generated markup with no inner HTML. The CSS :empty pseudo-class provides an elegant, native solution for targeting and styling these empty elements without JavaScript. This powerful selector has been part of CSS since IE9, making it one of the most widely supported pseudo-classes available. Whether you're debugging layout issues, hiding placeholder containers, or creating conditional layouts based on content presence, :empty offers a clean approach that aligns with modern web development's performance-first philosophy.

Basic :empty Usage
1/* Target all empty elements */2*:empty {3 outline: 2px solid red;4}5 6/* Target empty div elements */7div:empty {8 display: none;9}10 11/* Target empty elements with a specific class */12.card:empty {13 background: #f5f5f5;14 min-height: 200px;15}

What is the :empty Pseudo-Class?

The :empty pseudo-class represents any element that contains no children. Children can be either element nodes (other HTML elements) or text nodes, including whitespace. This selector operates at the DOM level, examining the actual content structure rather than visual appearance. When applied, :empty matches elements that have nothing between their opening and closing tags--no nested elements, no visible text, no whitespace characters, and no other content.

An element is considered empty when it has nothing between its opening and closing tags:

  • <div></div> -- truly empty
  • <div><!-- comment --></div> -- empty (comments don't count)
  • <div> </div> -- NOT empty (whitespace counts)

CSS-generated content via the content property on pseudo-elements like ::before and ::after does not affect whether the parent element is considered empty. As documented in MDN's CSS Reference, this selector follows standard CSS pseudo-class syntax and can be combined with other selectors for more specific targeting. Understanding how :empty works is fundamental to building robust CSS architectures for modern web applications.

The selector is part of the CSS Selectors Level 3 specification, making it one of the most widely supported pseudo-classes available.

Practical Applications

Real-world scenarios where :empty proves valuable

Debugging Layout Issues

Quickly identify empty containers causing layout problems. Apply temporary highlighting to all empty elements, then narrow down to specific containers.

Conditional Content Styling

Create CSS-only solutions for empty versus populated states. Style card components, content blocks, and list items based on content presence.

Hiding Empty Containers

Prevent visual clutter from empty containers. Hide elements with no content to avoid unnecessary whitespace and layout gaps.

Placeholder Styling

Apply placeholder graphics and default styling to empty components. Create consistent empty-state experiences without JavaScript.

The Whitespace Consideration

One of the most important nuances of :empty is its treatment of whitespace. In the current CSS Selectors specification (Level 3), an element containing only whitespace is NOT considered empty.

<!-- These are NOT empty due to whitespace -->
<div> </div>
<div>
</div>
<div>
	
</div>

<!-- Only this is truly empty -->
<div></div>

Selectors Level 4 introduced a change to make :empty match elements containing only whitespace, but no browser has implemented this yet. Developers must account for the current whitespace-sensitive behavior.

As explained in CSS-Tricks' pseudo-class reference, this behavior stems from how browsers parse HTML and represent the DOM, where whitespace between elements typically creates text nodes.

Solutions:

  • Remove all whitespace between tags
  • Use HTML comments strategically
  • Accept that truly empty markup is required
Practical :empty Examples
1/* Debugging: Highlight all empty elements */2*:empty {3 outline: 2px solid tomato;4 background: rgba(255, 99, 71, 0.1);5}6 7/* Card with empty state styling */8.card:empty {9 background: linear-gradient(10 45deg, #f0f0f0 25%,11 #e0e0e0 25%, #e0e0e0 50%,12 #f0f0f0 50%, #f0f0f0 75%,13 #e0e0e0 75%, #e0e0e014 );15 background-size: 20px 20px;16 border: 2px dashed #ccc;17 display: flex;18 align-items: center;19 justify-content: center;20}21 22.card:empty::before {23 content: "No content available";24 color: #666;25 font-size: 14px;26}27 28/* Hide empty elements completely */29.container > *:empty {30 display: none;31}

Combining with Other Pseudo-Classes

The :empty pseudo-class can be combined with other pseudo-classes for enhanced functionality:

/* Empty elements only when hovered */
.card:empty:hover {
 background: #ffe0e0;
}

/* Non-empty cards get different treatment */
.card:not(:empty):hover {
 box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
 transform: translateY(-2px);
}

/* Focus styling for empty focusable elements */
[tabindex]:empty:focus {
 outline: 2px solid blue;
 background: #e6f0ff;
}

Combining :empty with :not() creates powerful inverse logic patterns. For example, .card:not(:empty) selects cards that contain content, enabling different visual treatments for empty versus populated states of the same component. This approach integrates well with modern CSS techniques like CSS custom properties for maintainable component styling.

Browser Support

IE9+

Minimum Browser Support

99.5%

Global Support

4

Selectors Level

0

Prefixed Required

Performance and Best Practices

Selector Performance

The :empty pseudo-class is a structural pseudo-class that browsers evaluate efficiently. It examines DOM tree structure rather than computed styles, making it one of the faster selectors available. Modern browser engines optimize structural selectors heavily, making :empty suitable for use in production stylesheets without significant performance concerns.

Best Practices:

  • Be specific with your selectors to reduce unnecessary matching
  • Combine :empty with class or ID selectors for targeted styling
  • Avoid overuse in stylesheets with very large DOM trees

Modern CSS Integration

In contemporary CSS architecture, :empty integrates naturally with:

  • CSS Custom Properties -- Define empty state tokens separately
  • Container Queries -- Create adaptive layouts based on content presence
  • Cascade Layers -- Manage :empty styles with proper layer control

Our web development services team applies these best practices to build scalable, maintainable CSS architectures for projects of all sizes.

Common Pitfalls

  1. Whitespace -- Remember that whitespace creates text nodes
  2. Form Inputs -- Never use :empty for form validation
  3. Comments -- HTML comments don't prevent :empty from matching
  4. Pseudo-element Content -- CSS content property doesn't affect emptiness

Frequently Asked Questions

Need Help with CSS Development?

Our team of web development experts can help you implement modern CSS techniques and build performant, maintainable stylesheets. From responsive layouts to advanced selectors, we deliver clean, efficient code.

Sources

  1. MDN Web Docs - :empty - Comprehensive official documentation covering syntax, browser support, and accessibility considerations
  2. CSS-Tricks - :empty - Practical examples, demos, and use cases with interactive CodePen embeds
  3. Go Make Things - The :empty pseudo-class - Developer-focused tutorial with implementation examples
  4. Can I Use - CSS :empty - Browser support matrix