Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precedence of multiple classes defining color property being set by declaration order rather than specification order

Tags:

Given two classes of equal specificity defining the color property I thought the last class listed in the element class attribute would take precedence.

From http://htmlhelp.com/reference/css/structure.html:

Order of Specification To make it easy, when two rules have the same weight, the last rule specified wins.

In the following vacuum code example the order in which the class rule set is defined determines precedence. Here the last, or most recent, class rule set definition takes precedence.

<style>
    .makeBlue {color: blue;}
    .makeGreen {color:green;}
</style>
<div>
    <p class="makeGreen makeBlue">makeGreen makeBlue</p>
    <p class="makeBlue makeGreen">makeBlue makeGreen</p>
</div>

The output text is green.

If I swap the order of class declaration, and declare .makeGreen first

<style>
    .makeGreen {color:green;}
    .makeBlue {color: blue;}        
</style>
<div>
    <p class="makeGreen makeBlue">makeGreen makeBlue</p>
    <p class="makeBlue makeGreen">makeBlue makeGreen</p>
</div>

The output text is blue.

I've never noticed this before. edit I thought edit the last class listed in the element class attribute takes precedence.

edit To clarify --> I sometimes think of an element as a pet, let's say a dog. I see adding a class to the element's class attribute as issuing the dog a command. If I tell it to sit, and later tell it lie down, I expect the dog to lie down. I do not expect the dog to remain sitting simply because I taught it how to sit after (more recently than) I taught it how to lie down.

So... two questions.

  1. Is this how it is supposed to be? answered
  2. If so... why? I am unable to see the advantage of having to dig through class declarations in order to determine which was declared before the other.

Much thanks!

like image 751
IdusOrtus Avatar asked Jul 18 '13 15:07

IdusOrtus


People also ask

What is the correct order of precedence in selectors?

So final Order is: Value defined as Important > Inline >id nesting > id > class nesting > class > tag nesting > tag.

Which type of CSS selection will have the highest precedence?

Note: Inline style gets a specificity value of 1000, and is always given the highest priority! Note 2: There is one exception to this rule: if you use the ! important rule, it will even override inline styles! The selector with the highest specificity value will win and take effect!

How can you override the precedence or order of CSS styling?

The only way to override inline styles is by using !important . Many JavaScript frameworks and libraries add inline styles. Using !important with a very targeted selector, such as an attribute selector using the inline style, is one way to override these inline styles.


1 Answers

The order of the classes as you specify them on the elements is irrelevant. It's the order that you define them in your style declarations that matters. The quote you posted means in the style declarations, not the order the classes are listed on the elements.

like image 86
j08691 Avatar answered Oct 02 '22 02:10

j08691