Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

:not(:empty) with space in CSS

:not(:empty) with space in CSS


I would like to apply a CSS rule on a div when it is Empty. Problem: the application which generate the html puts a space:

So I tried:

.my-div:not(:empty) {...}

But it doesn't work because of the space.

How can I do that in CSS ?

Thank you

Problem courtesy of: skurty


You cant


The :empty selector is the only way in CSS of detecting content, and also proves positive for whitespace:

The :empty pseudo-class represents any element that has no children at all. Only element nodes and text (including whitespace) are considered. Comments or processing instructions do not affect whether an element is considered empty or not.

Dont forget about the separation of concerns, HTML for content, CSS for styling and JS for functionality - as such, you either need to remove the whitespace from your HTML (content) or apply a class depending on its presence, or strip it using JS (functionality).

With that said, elements only containing whitespace should collapse by default, so long as the space in question isnt a non-breaking space

Demo Fiddle

Solution courtesy of: SW4


View additional discussion.

This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

:not(:empty) with space in CSS


Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription