Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Basic

CSS :empty Selector

Posted in CSS Tutorial
Updated on Sep 20, 2024
By Mari Selvan
👁️ 3 - Views
⏳ 4 mins
💬 0
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:

Syntax
Copied
Copy To Clipboard
: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

HTML
Copied
Copy To Clipboard
<!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

CSS
Copied
Copy To Clipboard
/* 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

  1. Whitespace matters

    An element with only whitespace (such as spaces or line breaks) is not considered empty by the :empty selector.

    For example:

    HTML
    Copied
    Copy To Clipboard
    <p> </p> <!-- Not empty because of whitespace -->

    This <p> tag won’t be selected by :empty because of the whitespace.

  2. 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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy