HTML/CSSBeginner#css#selectors#specificity

What is the difference between class selectors and ID selectors, and why avoid IDs for styling?

IDs must be unique per page and carry much higher specificity (0,1,0,0) than classes (0,0,1,0), making later overrides harder and reducing reusability. Classes are reusable across multiple elements and keep specificity low and predictable, which is why most style guides reserve IDs for JS hooks/anchors only.

Example
/* Avoid */
#submit-button { background: blue; }
/* Prefer */
.btn-primary { background: blue; }
<button id="submit-button" class="btn-primary">Submit</button>

Related Questions

1
HTML/CSSAdvanced#css#layout-puzzle

How do you build a CSS-only accordion or tab component?

Open
2
HTML/CSSAdvanced#css#grid

What is the 'holy grail' layout and how is it built with Grid?

Open
3
HTML/CSSAdvanced#css#units

What is the difference between relative units vw/vh and %?

Open