tags:

views:

92

answers:

2

I'm a little confused about CSS and the class-attribut. I always thought, the order in which I specify multiple classes in the attribut's value has a meaning. The later class could/should overwrite definitions of the previous, but this doesn't seem to work. Here's an example:

<html>
<head>
<style type="text/css">
 .extra {
     color: #00529B;
     border:1px solid #00529B; /* Blue */
     background-color: #BDE5F8;
 }

 .basic {
           border: 1px solid #ABABAB;
 }
</style>
</head>
<body>
    <input type="text" value="basic" class="basic"/>
    <input type="text" value="extra" class="extra"/>
    <input type="text" value="basic extra" class="basic extra"/>
    <input type="text" value="extra basic" class="extra basic"/>
</body>
</html>

I would expect, the third example with class="basic extra" should have a blue border, since the in extra specified border would overwrite the border from basic.

I'm using FF 3 on ubuntu 9.04

+2  A: 

The order of classes in the attribute is irrelevant. All the classes in the class attribute are applied equally to the element.

The question is: in what order do the style rules appear in your .css file. In your example, .basic comes after .extra so the .basic rules will take precedence wherever possible.

If you want to provide a third possibility (e.g., that it's .basic but that the .extra rules should still apply), you'll need to invent another class, .basic-extra perhaps, which explicitly provides for that.

VoteyDisciple
+4  A: 

The order in which a class' attributes are overwritten is not specified by the order the classes are defined in the class attribute, but instead where they appear in the css

.myClass1 {font-size:10pt;color:#ff0000}
.myClass2 {color:#00ff00;}

HTML

<div class="myClass2 myClass1">Text goes here</div>

The text in the div will appear green and not red because myClass2 is futher down in the CSS definition than my class1. If I were to swap the ordering of the class names in the class attribute, nothing would change.

Zoidberg
Wow, I really got that wrong. Amazing how long I could make it without realizing this :-)
Tim Büthe
Yeah, its not obvious and I did the same thing you did. Took me hours to figure it out.
Zoidberg