I'd like to assign a style to a HTML element - but only in the event that the tag has some contents set.
Is this possible using pure CSS? Ideally I would like to avoid JS or server-side changes to the structure of the HTML itself.
A custom built CSS selector for more advanced users can be built using the "contains" selector. The "contains" selector is useful in cases where we are looking for an element that "contains" some text (case sensitive).
A CSS selector is the first part of a CSS Rule. It is a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them.
CSS [attribute~=value] Selector The [attribute~=value] selector is used to select elements with an attribute value containing a specified word.
The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").
CSS3 has an :empty
pseudo -class.
I can't see any way of doing this in pure CSS2.1.
You can do this with CSS3 by combining the :not
and :empty
pseudo-classes. For browsers that don't support both you'll need to use JS or a server-side solution.
div:not(:empty) { background-color:blue; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With