Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between `.class1.class2` and `.class1 .class2` CSS rule?

Tags:

css

I have a table with some rows:

<table>
    <tr class="even"><td>tr0</td></tr>
    <tr><td>tr1</td></tr>
    <tr class="even"><td>tr2</td></tr>
</table>

I have a CSS rule (rule1) for even rows:

.even{
    background-color: blue;
}

I have another rule (rule2) for override the bgcolor of any row:

.override, .override.even{
    background-color: green;
}

The weird thing is in IE9 all even rows (with no override class) are green!
Developer tools shows this for even rows:

enter image description here

In these two conditions IE do the job correctly:

If I rewrite rule2 like this:

.override, .override .even{ ... }

If I move rule2 above rule1:

.override, .override.even{ ... }
.even { ... }

Question is what's the difference between .override.even and .override .even?

EDIT:

Thanks for replies. Another question which I forgot to ask is why IE shows the even rows green?

like image 310
Pedram Behroozi Avatar asked Oct 21 '11 19:10

Pedram Behroozi


People also ask

What is. class1. class2 in CSS?

class1. class2 selects an element that has both classes. . class1 . class2 selects an element with a class of .

What is CSS class period?

To match a specific class attribute, we always start the selector with a period, to signify that we are looking for a class value. The period is followed by the class attribute value we want to match.


2 Answers

Spacing in between class specifiers means a ascendant -> descendant relationship.

The rule:

.test .heading { font-weight: bold; }

Would apply to the <p> element here:

<span class="test"><p class="heading">Something</p></span>

The lack of space means that the element must have both classes for the rule to apply.

The rule:

.heading.major { color: blue; }

Would apply to the <p> element here:

<p class="heading major">Major heading</p>
like image 81
Andre Avatar answered Sep 29 '22 14:09

Andre


Both answers are right, but they don't explain, why IE shows both rows green.

It's because IE has "standard" and "quirks" mode. To make multiple classes selectors work, you need to use proper DOCTYPE at the beginning of the file.

You are in "quirks" mode now and IE don't support multiple selectors, it sees only latest class. So it sees this and rows are green:

.even {
    background-color: blue;
}
.override, .even {
    background-color: green;
}

Put

<!DOCTYPE html>

(or another DOCTYPE) at the beginning of the file and both rows are going to be blue as expected.

like image 41
Petr Avatar answered Sep 29 '22 13:09

Petr