Reputation: 24788
I have this:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<title>My Social Network</title>
</head>
<body>
<div id="best_friend" class="friend" ></div>
<div class="family"></div>
<div class="enemy" id="archnemesis"></div>
</body>
</html>
div {
display: inline-block;
margin-left: 5px;
height: 100px;
width: 100px;
border-radius:100%;
border: 2px solid black;
}
#best_friend {
border: 4px solid #00C957;
}
.friend {
border: 2px dashed #008000;
}
.family {
border: 2px dashed #0000FF;
}
.enemy {
border: 2px dashed #FF0000;
}
#archnemesis {
border: 4px solid #CC0000;
}
My question is: Notice how I define border
for both class
and id
. The border
that is applied is the one coming from the id
. Why so? Why is the border
specification in id
overriding the one in class
.
Upvotes: 3
Views: 619
Reputation: 8681
The browser determines which styles to apply in what order based on Specificity, the higher number determines which will be applied.
Universal selectors have a specificity of 0,0,0,0.
*
= 0
HTML selectors have a specificity of 1.
p
, div
, etc.. = 1 each
So each HTML selector adds to the specificity.
div p
= 2, div p span
= 3
Class selectors have a specificity of 10.
.class
= 10
Class selectors combined with HTML selectors.
div p.class
= 12
ID selectors have a specificity value of 100.
#id
= 100
ID selectors combined with HTML selectors.
#id div
= 101
!important
overrides all other styles unless combined with another selector.
table td {height: 50px !important;}
Would override any height
style applied to only a td
within a table
.
Inline styles have the highest specificity of 1000.
style=
= 1000
CSS Specificity: Things You Should Know
Specificity | HTML Dog
Upvotes: 2
Reputation: 23670
Without including exceptions (e.g. :not
, !important
), the following list of selector types is by increasing specificity:
Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
The border that is applied is the one coming from the id. Why so? Why is the border specification in id overriding the one in class.
From this list, you can see that id
is higher than class
, so the border set in id
will be applied.
Upvotes: 2
Reputation: 1111
Specificity hierarchy
Every selector has its place in the specificity hierarchy. There are four distinct categories which define the specificity level of a given selector:
<h1 style="color: #fff;">
#div
..classes, [attributes]
and pseudo-classes such as :hover, :focus
etc.:before
and :after
.Ref:http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
and here : http://fourword.fourkitchens.com/article/css-specificity-id-overrides
Upvotes: 1
Reputation: 3449
Because id
has a higher precedence than class
you can verify that in this official documentation.
Upvotes: 1
Reputation: 19341
Following rules are applied for css
1.) When more than 1 overlapping styles are applied to the same element, only the last style is visible
2.) When the !important
attribute is used, it has the highest priority
3.) The style that has the highest CSS specificity is applied. The specificity of different elements is defined as follows:
ID attribute = 100
Class attribute = 10
Element = 1
Refer this link
Upvotes: 1