
CSS Topics
- CSS Intro
- CSS How To
- CSS Editors
- CSS Properties
- CSS Selectors
- .class
- .class1.class2
- .class1 .class2
- #id
- * (all)
- element
- element.class
- element,element
- element element
- element>element
- element+element
- element1~element2
- [attribute]
- [attribute=value]
- [attribute~=value]
- [attribute|=value]
- [attribute^=value]
- [attribute$=value]
- [attribute*=value]
- :active
- ::after
- ::before
- :checked
- :default
- :disabled
- :empty
- :enabled
- :first-child
- ::first-letter
- ::first-line
- :first-of-type
- :focus
- :fullscreen
- :has()
- :hover
- :in-range
- :indeterminate
- :invalid
- :lang()
- :last-child
- :last-of-type
- :link
- ::marker
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :only-of-type
- :only-child
- :optional
- :out-of-range
- ::placeholder
- :read-only
- :read-write
- :required
- :root
- ::selection
- :target
- :valid
- :visited
- CSS Comments
- CSS Length
- CSS Image Sprites
- CSS Grid Layout
- CSS Grid Flexbox
- CSS @charset Rule
- CSS @font-face Rule
- CSS @import Rule
- CSS @keyframes Rule
- CSS @media Rule
CSS :empty Selector
Photo Credit to CodeToFun
🙋 Introduction
The :empty selector in CSS is used to select elements that do not have any children, including text nodes.
It allows you to apply styles specifically to elements that are completely empty, making it useful for managing layout gaps or handling special cases in dynamic content.
💡 Syntax
The signature of the :empty Selector is as follows:
:empty {
/* CSS properties */
}The :empty selector applies to any HTML element that contains no child elements or text, including whitespace.
📝 Example
Here is an example demonstrating the use of the :empty selector:
☠️ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :empty Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<p>This paragraph has content.</p>
<p></p> <!-- This paragraph is empty -->
<div class="box"></div> <!-- This div is empty -->
</div>
</body>
</html>🎨 CSS
/* Style for empty elements */
:empty {
border: 2px dashed red;
background-color: #ffe0e0;
height: 50px;
width: 50px;
}
/* Optional styling for elements with content */
.container p {
padding: 10px;
background-color: #e0ffe0;
}In this example:
- The empty
<p>and<div>elements are selected by the:emptyselector and given a dashed red border and a light red background. - Non-empty elements are unaffected by the
:emptyselector.
💬 Usage Tips
- The
:emptyselector works only when the element contains no child nodes, including text nodes. Even a single space or a comment inside the element will cause it to no longer be considered "empty." - This selector is useful for identifying and styling elements that have no content, such as gaps in layout or placeholder elements in dynamic content.
⚠️ Common Pitfalls
Whitespace matters
An element with only whitespace (such as spaces or line breaks) is not considered empty by the
:emptyselector.For example:
HTMLCopied<p> </p> <!-- Not empty because of whitespace -->This
<p>tag won’t be selected by:emptybecause of the whitespace.Comments inside elements:
If an element contains only an HTML comment (e.g.,
<!-- comment -->), it will not be considered empty by the:emptyselector.
🎉 Conclusion
The :empty selector provides a simple way to target elements that contain no children, which can be highly useful for handling empty containers, ensuring proper layout, or debugging.
By using the :empty selector effectively, you can enhance the styling and behavior of your web pages when dealing with dynamically generated or user-generated content.
👨💻 Join our Community:
Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee












If you have any doubts regarding this article (CSS :empty Selector), please comment here. I will help you immediately.