Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS rule to apply only if element has BOTH classes [duplicate]

Let's say we have this markup:

<div class="abc"> ... </div> <div class="xyz"> ... </div> <div class="abc xyz" style="width: 100px"> ... </div> 

Is there a way to select only the <div> which has BOTH abc and xyz classes (the last one) AND override its inline width to make the effective width be 200px?

Something like this:

[selector] {   width: 200px !important; } 
like image 757
Majid Fouladpour Avatar asked Apr 26 '11 21:04

Majid Fouladpour


People also ask

How do you reference multiple classes in CSS?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element. Naming rules: Must begin with a letter A-Z or a-z.

Can an element have two classes CSS?

An element is usually only assigned one class. The corresponding CSS for that particular class defines the appearance properties for that class. However, we can also assign multiple classes to the same element in CSS. In some cases, assigning multiple classes makes page styling easier and much more flexible.

How do you select an element with multiple classes?

Use the getElementsByClassName method to get elements by multiple class names, e.g. document. getElementsByClassName('box green') . The method returns an array-like object containing all the elements that have all of the given class names.


2 Answers

div.abc.xyz {     /* rules go here */ } 

... or simply:

.abc.xyz {     /* rules go here */ } 
like image 125
esqew Avatar answered Sep 28 '22 08:09

esqew


Below applies to all tags with the following two classes

.abc.xyz {     width: 200px !important; } 

applies to div tags with the following two classes

div.abc.xyz {     width: 200px !important; } 

If you wanted to modify this using jQuery

$(document).ready(function() {   $("div.abc.xyz").width("200px"); }); 
like image 40
John Hartsock Avatar answered Sep 28 '22 07:09

John Hartsock