Cascading and specificity are used together to determine the final value of a CSS styling property. They also define the mechanisms for resolving conflicts in CSS rule sets.
Styles are read from the following sources, in this order:
.css
files<style>
element of the HTML documentstyle
attribute on an HTML element)The browser will lookup the corresponding style(s) when rendering an element.
When only one CSS rule set is trying to set a style for an element, then there is no conflict, and that rule set is used.
When multiple rule sets are found with conflicting settings, first the Specificty rules, and then the Cascading rules are used to determine what style to use.
.mystyle { color: blue; } /* specificity: 0, 0, 1, 0 */
div { color: red; } /* specificity: 0, 0, 0, 1 */
<div class="mystyle">Hello World</div>
What color will the text be? (hover to see the answer)
! blue
First the specificity rules are applied, and the one with the highest specificity “wins”.