There is a webpage containing this fragment of HTML: <div class="a b"></div><div class="a"></div>
. How can I hide the second div with CSS, leaving the first one visible? Please note, that I cannot add any other classes, as well as the visibility of the first div changes (sometimes relative, sometimes absolute) and it does not depend on me.

- 2,576
- 7
- 29
- 37
-
What have you tried so far? I can't see why you'd be stuck on this. `.b{display:none}` is not what you want? – Wesley Murch Mar 09 '12 at 20:44
-
possible duplicate of [Can you target an element with CSS only if 2 classes are present?](http://stackoverflow.com/questions/640996/can-you-target-an-element-with-css-only-if-2-classes-are-present) – Wesley Murch Mar 09 '12 at 20:46
-
sorry for mistake. I wanted to hide the second one. Corrected the question – burtek Mar 09 '12 at 20:46
-
1So you've tried nothing and you're already out of ideas? Hmmm... – Wesley Murch Mar 09 '12 at 20:48
-
The problem is, that the first div never has the same visibility - sometimes it's absolute, sometimes relative and it does not depend on me... – burtek Mar 09 '12 at 20:50
-
1Is there some reason why you can't add a class 'c' to the second DIV and hide it via that? – jmbertucci Mar 09 '12 at 21:51
6 Answers
You can hide both then show the one that has both classes a
and b
.a {display: none;}
.a.b {display: block;}
If the mark-up won't change you can hide the second div with the following:
.a.b + .a {display:none;}
This says anything with the class a
that directly follows anything with both classes a
and b
should be hidden.

- 23,604
- 6
- 30
- 47
-
As written in comment to the question, the first div never has the same visibility - sometimes it's absolute, sometimes relative and it does not depend on me. Is there maybe any 'not'-like statement in css? – burtek Mar 09 '12 at 20:52
-
@burtek: note that questions (and answers) on SO should be standalone. It shouldn't be necessary to read comments in order to understand either. Please put all necessary information in the question itself. – outis Mar 10 '12 at 14:51
div.a.b{display:none} /*take note, no spaces between .a and .b to signify that the div has both*/
or, since the first contains b
div.b{display:none}

- 117,725
- 30
- 181
- 234
Just create a selector for class b:
.b { display: none; }
If you require that only items with class a should be hidden when they also have class b, you can use the multiple class selector by joining the two class selector (no blank):
.a.b { display: none; }

- 30,396
- 9
- 75
- 81
What do you mean sometimes the visibility is absolute or relative? visibility can only be hidden or visible on a div.
Here is what you need for your updated Question
.a { display: hidden; }
.b { display: inline !important; }

- 1,963
- 3
- 25
- 35
CSS3 defines a negation pseudo-class:
.a:not(.b) {
display: none;
}
However, it only gained support in IE 9, so the current typical cross-browser solution is to set the property for all elements (including the ones that you don't want to match), then create another rule with a more specific selector that restores the original styling. This still has problems in IE 6 and earlier, as they only consider the last class when multiple classes for a single element are used.
.a {
display: none;
}
.a.b { /* IE6 will treat selector as '.b' */
display: block;
}
Note that if there are already rules with a more specific selector that match the elements you don't want to touch, or if the style is set inline (which shouldn't be done outside of a script manipulating the page), you don't need to add the second rule, since .a
has such a low specificity.