Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the real style when multiple CSS style classes are applied to the same element?

Tags:

css

I have a html file as below:

<!DOCTYPE html>
<html>
    <head>
        <style>
        .social { color: red; border: 1px solid blue; display: inline-block;}
        .first { color: green; }
        </style>
    </head>
    <body>
        <p class="social">taco</p>
        <p class="first">burrito</p>
        <p class="first social">chimichanga</p>
    </body>
</html>

I uses classes 'first' and 'social' for the 3rd paragraph. However, the colour of the 3rd paragraph is always green ('first') no matter whether I put 'first' before 'social' or after.

So, how does the browser decide the css style when multiple style classes are applied to one element?

like image 375
injoy Avatar asked Mar 13 '23 07:03

injoy


1 Answers

From the CSS2.1 specifications:

Finally, sort by order specified: if two declarations have the same weight, origin and specificity, the latter specified wins.

In your case, the first class appears after the social class in your internal stylesheet. Since both have the same weight, origin, and specificity, the first rule's overlapping style (i.e., color property) wins.

like image 181
timolawl Avatar answered Mar 14 '23 21:03

timolawl