20
loading...
This website collects cookies to deliver better user experience
<a class="container">Some text</a>
.container {
color: blue;
}
a {
color: red;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<style type="text/css">
h1 {
background-color: black;
color: red;
}
h2 {
color: goldenrod;
}
</style>
</head>
<body>
<h1>
The specifity of internal CSS written on HTML style block is more than
that of external stylesheet
</h1>
<h2 style="color: pink;">
Inline styles overides all other css styles
</h2>
</body>
</html>
Inline style : Connected directly to the element to be styled.
Example: <h1 style=”color: #ffffff;”>
IDs: ID is a unique identifier for an element.
Example: #navbar
Classes, attributes and pseudo-classes: Includes .classes, [attributes] and pseudo-classes like :hover, :focus etc.
Elements and pseudo-elements: Includes element names and pseudo-elements like h1, div, :before and :after.
h1
#content h1
<div id="content"><h1 style="color: #ffffff">Heading</h1></div>
h1
is 1 (one element)#content h1
is 101 (one ID reference and one element)<div id="content"><h1 style="color: #ffffff">Heading</h1></div>
is 1000 (inline styling)p {
} /* 0 0 0 1 */
span {
} /* 0 0 0 1 */
p span {
} /* 0 0 0 2 */
p > span {
} /* 0 0 0 2 */
div p > span {
} /* 0 0 0 3 */
.name {
} /* 0 0 1 0 */
.users .name {
} /* 0 0 2 0 */
[href$='.pdf'] {
} /* 0 0 1 0 */
:hover {
} /* 0 0 1 0 */
#name {
} /* 0 1 0 0 */
.user #name {
} /* 0 1 1 0 */
#name span {
} /* 0 1 0 1 */
<p style="color: red">Test</p> /* 1 0 0 0 */
p {
font-size: 20px !important;
}
The universal selector (*) has no specificity value (0,0,0,0)
Pseudo-elements (e.g. :first-line) get 0,0,0,1 unlike their psuedo-class brethren which get 0,0,1,0
There’s a highly debatable topic related to using !important
it overrides all the CSS values. It is also a good practice not to use any id selectors and !important but class selectors.
The pseudo-class : not() adds no specificity by itself, only what’s inside its parentheses.