brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: CSS :empty hides elements that have no content

Css
~/tips/tip-css-empty-pseudo-class-hide

.notice:empty {

Matches elements with no children, so empty wrappers can be hidden or styled differently.

Templates and plugins love to print an empty wrapper with a border and padding around nothing. A one-line CSS rule can hide it.

.notice:empty {
  display: none;
}

:empty matches an element that has no children. That covers element nodes and text, including whitespace. Comments don’t count, so <div><!-- note --></div> still matches. Neither does CSS-generated content. It has worked in all browsers since July 2015.

MDN’s own example makes empty boxes lime and leaves the others pink:

.box { background: pink; }
.box:empty { background: lime; }

The caveat is whitespace. A line break or space inside the element is a text node, so <div>
</div>
is not empty. That is what makes the rule fail on HTML that is formatted nicely by a template. A nested empty element, like <div><p></p></div>, does not match either, because the <p> is a child.

Selectors Level 4 would let :empty match whitespace-only elements, but MDN says no browser supports that yet. So if you control the markup, print the tag with nothing between the brackets. If you don’t, you’ll need another route, such as hiding the wrapper where the template creates it.

Further reading: MDN: :empty

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .toast { article { counter-reset: h2; } .toolbar { @font-face { All Css tips (66) All topics

Link