Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I select a div with class "A" but NOT with class "B"?

I have some divs:

<div class="A">"Target"</div> <div class="A B">"NotMyTarget"</div> <div class="A C">"NotMyTarget"</div> <div class="A D">"NotMyTarget"</div> <div class="A E">"NotMyTarget"</div> 

Is there a CSS selector that will get me the div containing Target but not the divs containing NotMyTarget?

Solution must work on IE7, IE8, Safari, Chrome, and Firefox

Edit: So far Nick is the closest. It's clumsy and I don't like the solution, but at least it works:

.A {    /* style that all divs will take */ } div.B  {   /* style that will override style .A */ } 
like image 581
MedicineMan Avatar asked Mar 20 '10 00:03

MedicineMan


People also ask

Can I use two classes in a div?

Absolutely, divs can have more than one class and with some Bootstrap components you'll often need to have multiple classes for them to function as you want them to. Applying multiple classes of course is possible outside of bootstrap as well. All you have to do is separate each class with a space.

How do you select element with class A in CSS?

class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class.

How do you select an element with an attribute class?

The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".


2 Answers

You can use the attribute selector to match the div that has only one class:

div[class=A] {   background: 1px solid #0f0; } 

If you want to select another div that has multiple classes, use quotes:

div[class="A C"] {   background: 1px solid #00f; } 

Some browsers do not support the attribute selector syntax. As usual, "some browsers" is a euphemism for IE 6 and older.

See also: http://www.quirksmode.org/css/selector_attribute.html

Full example:

<!DOCTYPE html> <html> <head>   <style>     .A { font-size:22px; }     .B { font-weight: bold; border: 1px solid blue; }     .C { color: green; }      div[class="A"] {       border: 1px solid red;     }     div[class="A B"] {       border: 3px solid green;     }   </style> </head> <body>   <div class="A">"Target"</div>    <div class="A B">"NotMyTarget"</div>    <div class="A C">"NotMyTarget"</div>    <div class="A D">"NotMyTarget"</div>    <div class="A E">"NotMyTarget"</div>  </body> </html> 

EDIT 2014-02-21: Four years later, :not is now widely available, though verbose in this specific case:

.A:not(.B):not(.C):not(.D):not(.E) {   border: 1px solid red; } 

Unfortunately, this doesn't work in IE 7–8 as specified in the question: http://caniuse.com/#search=:not

like image 134
Ron DeVera Avatar answered Sep 23 '22 14:09

Ron DeVera


.A:not(.B) {} 

But guess who doesn't support that... Indeed, IE<=8.

like image 36
Ms2ger Avatar answered Sep 25 '22 14:09

Ms2ger