Let's consider the following code
<html>
<head>
<style>
*{
color: gray;
}
[[title]]{
color: red;
}
.h1{
color: blue;
}
h1{
color: pink;
}
</style>
</head>
<body>
<h1 id="title" class="h1" style="color:purple">CodeWithHarry</h1>
</body>
</html>
!important
rule typeLets look into all these one by one.
<link>
tag, a <style>
tag, or style
attribute. <link>
tag at the top of your page and another at the bottom, the styles from the bottom one will be used. The same goes for <style>
tags; the ones lower down on the page take priority.style
attribute with CSS declared in it will override all other CSS, regardless of its position, unless a declaration has !important
defined.CSS specificity determines which style rules get applied to an element when there are conflicts.
Higher specificity means the style will be used. It's calculated based on a point system involving inline styles, IDs, classes, and tag names.
Inline styles have the highest specificity and will always override styles if other selector(s) are also defined.
<html>
<head>
<style>
*{
color: gray;
}
[[title]]{
color: red;
}
.h1{
color: blue;
}
h1{
color: pink;
}
</style>
</head>
<body>
<h1 id="title" class="h1" style="color:purple">CodeWithHarry</h1>
</body>
</html>