Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector only works if <tag>has contents</tag>

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.

like image 619
Mr Foo Bar Avatar asked Jun 24 '10 08:06

Mr Foo Bar


People also ask

Can I use contains in CSS selector?

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).

How does Selector work in CSS?

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.

Is there a CSS selector for elements containing certain text?

CSS [attribute~=value] Selector The [attribute~=value] selector is used to select elements with an attribute value containing a specified word.

Which selector applies a CSS rule to all elements?

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").


2 Answers

CSS3 has an :empty pseudo -class.

I can't see any way of doing this in pure CSS2.1.

like image 170
Skilldrick Avatar answered Oct 01 '22 17:10

Skilldrick


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; }
like image 42
Andy E Avatar answered Oct 01 '22 18:10

Andy E