Hide empty HTML Element

CSS

Sometimes, you may want to hide an HTML element when it has no content inside. This can be useful for cleaning up the UI and avoiding empty elements taking up space.

Here’s a simple CSS snippet to achieve this:

.my-component:empty {
  display: none;
}

Explanation

  • :empty: This CSS pseudo-class selects elements that have no children (including text nodes).
  • display: none;: This hides the selected element from the layout.

Usage

To use this CSS rule, simply add the class my-component to the elements you want to hide when they are empty:

<div class="my-component"></div>

This will hide the element if it has no content inside.