Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :empty pseudo-class

Reference for the CSS :empty pseudo-class: match elements that have no child elements or text, how whitespace affects it, and when to use it, with examples.

The :empty pseudo-class matches an element that has no children at all: no child elements and no text. A <div></div> is empty; a <div>Hi</div> is not. Comments and processing instructions do not count as children for this purpose.

It is useful for hiding placeholder boxes that a template or script leaves behind, such as an empty message area or an empty list wrapper.

CSS
.notice:empty {
  display: none;
}

Try it

Hide empty boxes

Only the second box is marked empty. The third contains an element, and the fourth contains whitespace (a newline), which browsers still treat as content.

Syntax

CSS
:empty {
  /* declarations */
}
  • Arguments: none.
  • Specificity: (0, 1, 0).
  • Matches: an element with zero child nodes, ignoring comments.

Values

Syntax and arguments Description
:empty No arguments. Matches elements with no child elements and no text nodes.
:not(:empty) The reverse: elements that have some content.

Usage notes

  • Whitespace breaks it. A newline or space between the tags counts as a text node, so <p> </p> does not match in current browsers. Newer Selectors drafts would let whitespace-only elements match, but you should not rely on that. Generate truly empty elements, or add a class.
  • Pseudo-elements are fine. ::before and ::after content is not part of the DOM tree, so an element with only generated content can still be :empty.
  • Replaced and void elements. Elements that cannot have children, like <img> or <input>, count as empty, so img:empty matches every image.
  • It does not see styled-away content. A child with display: none still counts as a child.
  • Accessibility. Hiding an empty live region with display: none can stop screen readers from announcing it later. Keep status regions visible to assistive technology, for example with a visually hidden style.
  • Do not use it to hide errors. An empty element is often a bug in the data, so fix the source when possible.

Browser support

Supported in every browser.