Andrew Li
Andrew Li

Reputation: 1055

css when using multiple class for same css property

I don't know well about css rules. I've multiple classes with same property like "width".
When setting these classes to the Html tag, whose width property is set to that tag?
For example.

.main{
   width:600px;
   border:1px solid #000;
}

.rule1{
  width:400px;
  min-height:10px;
}

.rule2{
  width:300px;
  background:#aaa;
}
<div class="main rule1 rule2">
  Which rule's width property is set?
</div>

Upvotes: 6

Views: 6652

Answers (6)

Gaurav Panwar
Gaurav Panwar

Reputation: 41

.rule2{width:300px; background:#aaa;}

This is last css rule its override all css class rule, finally width:300px is compile...

Upvotes: 0

Talent Developer
Talent Developer

Reputation: 142

The last rule wins when setting the multiple rules. But if there is inline "style" , "style" wins.

A selector's specificity is calculated as follows:

    count 1 if the declaration is from is a 'style' attribute rather than a rule with a selector, 0 otherwise (= a) (In HTML, values of an element's "style" attribute are style sheet rules. These rules have no selectors, so a=1, b=0, c=0, and d=0.)
    count the number of ID attributes in the selector (= b)
    count the number of other attributes and pseudo-classes in the selector (= c)
    count the number of element names and pseudo-elements in the selector (= d)
    The specificity is based only on the form of the selector. In particular, a selector of the form "[id=p33]" is counted as an attribute selector (a=0, b=0, c=1, d=0), even if the id attribute is defined as an "ID" in the source document's DTD.

Concatenating the four numbers a-b-c-d (in a number system with a large base) gives the specificity.

If the specificities are equal, then CSS 2.1 Section 6.4.1 comes into play:

    Finally, sort by order specified: if two declarations have the same weight, origin and specificity, the latter specified wins. Declarations in imported style sheets are considered to be before any declarations in the style sheet itself.

Note that this is talking about when the style is defined, not when it is used. If classes .a and .b have equal specificity, whichever is defined last in the stylesheet(s) wins. ...

and ...

will be styled identically, based on the definition order of .a and .b.

Upvotes: 1

Dalin Huang
Dalin Huang

Reputation: 11342

Simple, later one will overwrite the previous one for the same attribute like width or background color.

Note in main you have a border, and never been overwritten by rule1 and rule2 so it keeps the 5px solid red border.

Note that width:200px; is applied since it is the last one define width, so it overwrite previous (unless you use !important in previous one, even with !important, later one will overwrite previous !important)

I think you get the idea now!

Just define them in one css rule:

.main{
  width:200px;
  background: yellow;
  min-height:10px;
  border:5px solid red;
}

.main{
   width:1000px;
   border:5px solid red;
   background: red;
}

.rule1{
  width:2000px;
  min-height:10px;
  background: green;
}

.rule2{
  width:200px;
  background: yellow;
}
<div class="main rule1 rule2">
  Which rule's width property is set?
</div>

Upvotes: 0

Chris Pierce
Chris Pierce

Reputation: 736

you can overwrite the default behavior by adding

.rule1 {
    width:400px !important;
    min-height:10px;
}

This will make sure that this rule's width is used.

Upvotes: 1

SimpleBeat
SimpleBeat

Reputation: 755

CSS stands for Cascading Style Sheets, which means that all the rules defined for an element apply, and if there are several identical properties the last defined wins.

So, your min-height from .rule1 is applied, as well as background from the second rule is applied. However, when it comes to width (which both rules have), the last one defined is applied: width:300px.

Try putting .rule2 before .rule1 in your CSS file to see what it does!

Upvotes: 1

Kiran Dash
Kiran Dash

Reputation: 4956

.rule2{
  width:300px;
  background:#aaa;
}

This one defines the width for the tag since it comes at the end. When you are having multiple classes for a tag, and a common style for them all then it is always the last rule which will overwrite all the previous rules.

Upvotes: 5

Related Questions