CSS :empty Pseudo-class

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The :empty pseudo-class matches elements that have no children — no nested elements and no text nodes (spaces count as text).

Use it for placeholder boxes, empty slots, or hiding blank containers. Comments alone do not break the match. Write truly empty tags like <div></div> with nothing between them.

Baseline · Widely available.

Not deprecated. CSS3 form. Safe across modern browsers.

Selectors Level 4 note.

The Level 4 spec would treat whitespace-only nodes as empty, but no browser ships that yet. Study and use the strict “no whitespace” behavior.

CSS Syntax

syntax.css
:empty { /* declarations */ }

Often typed as div:empty, p:empty, or .box:empty.

Selector Forms

PatternMatchesTry it
div:emptyTruly empty div elementsTry it
.slot:emptyEmpty placeholder slots by classTry it
p:emptyEmpty paragraphs (hide or mark)Try it
Whitespace pitfallSpaces/newlines prevent a matchTry it

How to Use :empty

Keep the markup blank, then style the empty state.

  1. Write an element with nothing between the tags (no spaces).
  2. Select it with :empty or div:empty.
  3. Add outline, background, or min-size so empty slots stay visible.
  4. Avoid pretty-printing whitespace inside tags you need to match.
Inside the elementMatches :empty?
Nothing at allYes
HTML comment onlyYes
Space / newline / textNo
Nested element (even empty)No

Live Preview

Toggle which box is highlighted. Only truly empty boxes (and comment-only) match :empty today.

Lime outline = matches :empty. Whitespace and nested children do not.

Boxes
empty
comment
space
child

:empty — empty + comment boxes match

CSS Examples

Each snippet focuses on :empty. Use Try It Yourself for a live editor.

Example 1 — Outline Empty Boxes

Make blank containers visible for layout debugging.

CSS
<style>
  div:empty {
    outline: 2px solid #db2777;
    height: 2rem;
  }
</style>

<div></div>
<div>Has text</div>
Try It Yourself

How It Works

  1. First div. Nothing between tags — matches div:empty.
  2. Outline + height. Empty boxes get a visible size and border.
  3. Second div. Text creates a child text node — no match.
  4. Use case. Spot missing content during layout work.

Example 2 — Style Empty Slots

Show a muted placeholder when a slot has no content yet.

CSS
<style>
  .slot:empty {
    background: #f1f5f9;
    border: 1px dashed #94a3b8;
    min-height: 2.5rem;
  }
</style>

<div class="slot"></div>
<div class="slot">Filled</div>
Try It Yourself

How It Works

  1. Scoped selector. .slot:empty only targets empty slots.
  2. Dashed look. Signals “waiting for content.”
  3. Filled slot. Has text, so styles do not apply.
  4. Tip. Keep the empty markup free of spaces.

Example 3 — Hide Empty Paragraphs

Remove blank p tags from the layout flow.

CSS
<style>
  p:empty {
    display: none;
  }
</style>

<p>Visible paragraph</p>
<p></p>
<p>Another visible paragraph</p>
Try It Yourself

How It Works

  1. Rule. p:empty { display: none; } drops blank paragraphs.
  2. Middle p. Truly empty — removed from layout.
  3. Others stay. They contain text.
  4. Caution. Do not hide interactive controls that need an accessible name.

Edge Cases & Pitfalls

If :empty seems broken, check these first.

space

Whitespace is content

<div> </div> or a newline between tags does not match. Use <div></div> with nothing in between.

nested

Nested empty child blocks the parent

<div><p></p></div> — the outer div is not empty because it has an element child.

comment

Comments are OK

An element that contains only an HTML comment still matches :empty.

a11y

Do not leave interactive controls empty

Buttons and links need an accessible name. Hide decorative empties, not unlabeled controls.

Level 4

Whitespace-as-empty is not shipping

Selectors Level 4 would change whitespace behavior. Until browsers catch up, assume spaces break the match.

Browser Support

The :empty pseudo-class is a browser CSS selector. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

:empty

Supported in all modern browsers (CSS3). Safe for teaching and production. Level 4 whitespace change is not implemented yet.

100% Core support
Google Chrome 4+
Full support
Mozilla Firefox 2+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 10+
Full support
Samsung Internet 4+
Full support
Internet Explorer 9+
Full support
:empty Universal

Bottom line: No prefixes. No polyfills. It is not a runtime API.

Key Takeaways

  • Role: match elements with no element or text children.
  • Whitespace: spaces and newlines prevent a match today.
  • Comments: comment-only content still matches :empty.
  • Tip: keep empty tags flush: <div></div>.

One line: :empty styles elements that truly contain nothing — not even a space.

Frequently Asked Questions

It matches elements that have no children — no element nodes and no text nodes (including whitespace).
Yes, in every current browser. A space, tab, or line break between the tags creates a text node, so the element no longer matches :empty.
No. Comments and processing instructions do not prevent :empty from matching. CSS generated content also does not count.
The Level 4 spec would let whitespace-only nodes still match :empty (like :-moz-only-whitespace), but no browser implements that yet. Teach the strict CSS3 behavior.
Yes. It is Baseline Widely available (CSS3). It is a browser selector, not a Node.js or runtime API.
:blank is a newer idea that can treat whitespace-only as empty. Support is limited; prefer :empty for production teaching and code today.

Next: :enabled

Learn how to style interactive form controls with :enabled.

:enabled tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful