p { color: blue; }
#main p { color: green; }
.content p { color: red; }
<div id="main" class="content">
<p>Hello world!</p>
</div>The #main p selector has higher specificity because it uses an ID selector, which beats class selectors like .content p and element selectors like p. So the paragraph text will be green, not red or blue.
ul#nav li.active a:hover.Specificity is counted as (inline, IDs, classes/pseudo-classes, elements). Here, there is 1 ID (#nav), 2 classes/pseudo-classes (.active and :hover), and 3 elements (ul, li, a), so the specificity is 0,1,2,3.
<style>
button { color: black !important; }
.btn-primary { color: blue; }
#submit-btn { color: red; }
</style>
<button id="submit-btn" class="btn-primary">Submit</button>The button { color: black !important; } rule overrides all other color rules, even those with higher specificity like #submit-btn. So the button text will be black.
a:focus { outline: 2px solid blue; } a.special:focus { outline: none; } .special { outline: 3px solid red; }
The selector a.special:focus has higher specificity than a:focus and sets outline: none;, so the link will have no visible outline when focused, which can harm accessibility.
div > > p?div > > p { color: green; }The selector div > > p has two adjacent child combinators, which is invalid syntax. Browsers ignore this rule due to the syntax error.