Mastering CSS3 Selectors and Specificity for Precise Styling
๐ฏ CSS3 Selectors & Specificity
Selectors determine which HTML elements are targeted by CSS rules.
CSS3 expands selector capabilities for precise styling and advanced targeting.
๐งพ CSS3 Selector Examples
/* ๐ Attribute Selector */
input[type='text'] {
border: 1px solid #ccc;
}
/* ๐ฏ Pseudo-class */
a:hover {
color: red;
}
/* ๐ญ Pseudo-element */
p::before {
content: "๐ ";
}
๐ง Understanding Specificity
When multiple rules apply, specificity determines which one takes precedence.
| Selector Type | Specificity Value |
|------------------------------------|-------------------|
| Inline styles | 1000 |
| ID selectors (`#id`) | 100 |
| Class, attribute, pseudo-class | 10 |
| Element, pseudo-element selectors | 1 |
๐ Best Practices
- ๐ฏ Use clear, structured selectors
- ๐งผ Avoid over-specificity to keep CSS maintainable
- ๐ ๏ธ Use developer tools to debug specificity conflicts
Mastering selectors and specificity is key to writing predictable, scalable, and efficient CSS3 styles.