CSS Basic
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:empty
selector and given a dashed red border and a light red background. - Non-empty elements are unaffected by the
:empty
selector.
💬 Usage Tips
- The
:empty
selector 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
:empty
selector.For example:
HTMLCopied<p> </p> <!-- Not empty because of whitespace -->
This
<p>
tag won’t be selected by:empty
because 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:empty
selector.
🎉 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.