The :empty pseudo-class targets elements that contain no child elements and no text. It is useful for highlighting blank containers, handling dynamic content gaps, and building cleaner layouts.
01
No children
Zero content.
02
No text
Includes spaces.
03
Any element
div, p, li…
04
Placeholders
Empty slots.
05
Layout gaps
Spot blanks.
06
Pitfalls
Whitespace.
Fundamentals
Introduction
The :empty selector in CSS is a pseudo-class used to match elements that have no children at all — no element nodes and no text nodes.
It lets you apply styles specifically to completely blank containers, which is helpful when managing layout gaps, styling placeholders, or debugging dynamically generated content.
Definition and Usage
Append :empty to any element selector with no space before the colon: div:empty, p:empty, or .card:empty. Styles apply only while the element remains truly empty.
💡
Beginner Tip
Even a single space between opening and closing tags creates a text node, so <p> </p> is not empty. Write <p></p> with nothing inside for :empty to match.
Empty list items are removed from the visual layout. This keeps dynamically generated lists tidy without extra JavaScript checks.
Watch Out
⚠️ Common Pitfalls
Whitespace matters — <p> </p> contains a space text node and does not match :empty. Remove all characters between tags.
Line breaks count — Pretty-printed HTML with a newline inside a tag also creates a text node.
Hidden elements still count — A child with display: none or zero size is still a child; the parent is not empty.
Self-closing tags — <img>, <br>, and <input> inside a container prevent :empty from matching.
Not the same as :blank — Do not expect :empty to match whitespace-only elements; use :blank where supported instead.
Whitespace Example
empty-whitespace.html
<!-- Does NOT match :empty (space inside) --><p></p><!-- DOES match :empty --><p></p>
A11y
♿ Accessibility
Do not hide meaningful empty regions — If an empty container conveys structure, ensure assistive technology users still understand the layout.
Placeholder text via CSS — Content added with ::before may not always be announced by screen readers; prefer real text when the message is important.
Loading states — Pair skeleton loaders with aria-busy="true" on the container so users know content is loading.
Do not rely on color alone — Combine dashed borders with labels or text when empty states need to be understood clearly.
🧠 How :empty Works
1
Browser inspects the DOM
For each candidate element, the browser checks its child nodes.
DOM
2
Counts element and text nodes
Any element child or text node (even one space) means the element is not empty.
Match test
3
:empty styles apply
If zero children are found, your :empty rules take effect.
Render
=
🔍
Blank containers styled
Empty slots are highlighted, hidden, or given placeholders automatically.
Compatibility
🖥 Browser Compatibility
The :empty pseudo-class is supported in all modern browsers. It has been part of CSS since Selectors Level 3 and works reliably across Chrome, Firefox, Safari, Edge, and Opera.
✓ Baseline · Universal support
Structural empty detection everywhere
:empty works in every major browser for layout, placeholders, and debugging blank containers.
99%Universal support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
:empty pseudo-class99% supported
Bottom line::empty is safe for production. For whitespace-only elements, consider :blank where you can accept limited support.
Wrap Up
🎉 Conclusion
The :empty pseudo-class gives you a simple, CSS-only way to target elements with no content. It is ideal for placeholders, layout debugging, and cleaning up dynamically generated markup.
Remember that whitespace counts as content, scope your selectors when possible, and pair :empty with ::before for helpful placeholder messages.
Use min-height to reserve space for loading content
Combine with ::before for placeholder text
Test with dynamically inserted content
❌ Don’t
Assume pretty-printed HTML will stay empty
Hide empty regions that convey important structure
Rely on :empty for whitespace-only elements
Forget that child elements prevent a match
Use global :empty on large pages without scoping
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :empty
Use these points when styling blank containers.
5
Core concepts
∅01
Zero content
No children or text.
Purpose
␣02
Whitespace
Breaks the match.
Pitfall
💬03
Placeholders
With ::before.
Pattern
📄04
Dynamic lists
Hide blank items.
Use case
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The :empty pseudo-class matches elements that have no child elements and no text nodes. It lets you style containers that are completely blank.
Yes. A space, tab, or line break inside an element creates a text node, so the element no longer matches :empty. Write truly empty tags like <p></p> with nothing between them.
No. Comment nodes are not element or text nodes, so an element that contains only <!-- comments --> still matches :empty in modern browsers.
:empty matches only elements with zero children and zero text. :blank (newer, limited support) also matches elements that contain only whitespace.
Yes. All modern browsers support :empty. It has been available since CSS3 and works reliably for layout and placeholder styling.