Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:empty selector not working in css

Tags:

i used :empty selector in .error class. the problem is that even when there is no content inside the div with class of error, the error class is not removed completely. when i tested in firebug, i found that the div still has some whitespace and when i remove that extra spaces, the div then disappears.

.error{ border:solid 1px #ff0000; color:#ff0000;}
.error:empty{ display:none;}

the div shows like this on debugging:

<div class="error">     </div>

that extra spaces you see is the problem. Is there any way i can show &nbsp; in css to display:none ? pls help.

like image 444
gaurav oberoi Avatar asked Aug 19 '13 12:08

gaurav oberoi


People also ask

Can I use empty CSS?

The :empty CSS pseudo-class represents any element that has no children. Children can be either element nodes or text (including whitespace). Comments, processing instructions, and CSS content do not affect whether an element is considered empty.

How do you check if an element is empty in CSS?

An element with nothing between its tags is empty. That includes if an element contains a code comment. And if the CSS for the element has generated content — as from a pseudo-element like ::before or ::after — it is also still considered empty.

What is blank in CSS?

The :blank pseudo-class builds upon the :empty pseudo-class. Like :empty , :blank will select elements that contain nothing at all, or contain only an HTML comment. But, :blank will also select elements that include whitespace, which :empty will not. p:blank { display: none; }


2 Answers

it is because <div class="error"> </div>(demo) is not empty, it has a text node as the child.

:empty

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.

Try

<div class="error"></div>

Demo: Fiddle

like image 181
Arun P Johny Avatar answered Sep 19 '22 14:09

Arun P Johny


The :empty pseudo-class doesn't select elements, containing elements or text (and white space is text).

However, there is now the experimental :blank pseudo-class which selects elements, containing nothing or just white space, and .error:blank would select that <div> element. You should avoid using experimental features in your production code, though.

like image 26
Arthur Khazbs Avatar answered Sep 23 '22 14:09

Arthur Khazbs