CSS: Styling When Element Has Two Classes

CSS: styling when element has two classes

You just answered yourself. Be wary of the IE6 bug.

CSS rule to apply only if element has BOTH classes

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

... or simply:

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

CSS Selector that applies to elements with two classes

Chain both class selectors (without a space in between):

.foo.bar {
/* Styles for element(s) with foo AND bar classes */
}

If you still have to deal with ancient browsers like Internet Explorer 6, be aware that it doesn't read chained class selectors correctly: it'll only read the last class selector (.bar in this case) instead, regardless of what other classes you list.

To illustrate how other browsers and IE6 interpret this, consider this snippet:

* {
color: black;
}

.foo.bar {
color: red;
}
<div class="foo">1. Hello Foo</div>
<div class="foo bar">2. Hello World</div>
<div class="bar">3. Hello Bar</div>

How to select an element with 2 classes

You can chain class selectors without a space between them:

.a.b {
color: #666;
}

Note that, if it matters to you, IE6 treats .a.b as .b, so in that browser both div.a.b and div.b will have gray text. See this answer for a comparison between proper browsers and IE6.

Using two CSS classes on one element

If you want two classes on one element, do it this way:

<div class="social first"></div>

Reference it in css like so:

.social.first {}

Example:

https://jsfiddle.net/tybro0103/covbtpaq/

How can I apply styles to multiple classes at once?

.abc, .xyz { margin-left: 20px; }

is what you are looking for.

class overrule when two classes assigned to one div

Multiple classes can be assigned to a div. Just separate them in the class name with spaces like this:

<div class="rule1 rule2 rule3">Content</div>

This div will then match any style rules for three different class selectors: .rule1, .rule2 and .rule3.

CSS rules are applied to objects in the page that match their selectors in the order they are encountered in the style sheet and if there is a conflict between two rules (more than one rule trying to set the same attribute), then CSS specificity determines which rule takes precedence.

If the CSS specificity is the same for the conflicting rules, then the later one (the one defined later in the stylesheet or in the later stylesheet) takes precedence. The order of the class names on the object itself does not matter. It is the order of the style rules in the style sheet that matters if the CSS specificity is the same.

So, if you had styles like this:

.rule1 {
background-color: green;
}

.rule2 {
background-color: red;
}

Then, since both rules match the div and have exactly the same CSS specificity, then the second rule comes later so it would have precedence and the background would be red.


If one rule had a higher CSS specificity (div.rule1 scores higher than .rule2):

div.rule1 {
background-color: green;
}

.rule2 {
background-color: red;
}

Then, it would take precedence and the background color here would be green.


If the two rules don't conflict:

.rule1 {
background-color: green;
}

.rule2 {
margin-top: 50px;
}

Then, both rules will be applied.

Apply CSS Rules Only If 2 Classes are Set to an Element

Sure:

.one.two { border: 1px red solid }

override a CSS class if two other classes are present

Just go

.class1.class2.class3 {
/* these definitions are more specific and don't need !important; */
}

Remember this requires you to put class3 on the element as well:

<element class="class1 class2 class3"></element>

If the only reason you wanted to add class3 is to specify styles that should be applied when both classes class1 class2 are present, you don't even need class3, just go

.class1.class2 {
/* these definitions are more specific than those for .class1 or .class2
and don't need !important;
These rules apply to any element that has both class1 and class2 */
}


Related Topics



Leave a reply



Submit