Understanding CSS Selectors and Their Specificity
Selects all elements on the page. Used for global styles.
Specificity: 0-0-0
The universal selector is often used for CSS resets and applying base styles. It has the lowest specificity and will be overridden by any other selector. Use with caution as it can impact performance when overused.
* { margin: 0; padding: 0; }
Selects all elements of specified type.
Specificity: 0-0-1
Element selectors target HTML elements directly by their tag name. They have low specificity but are useful for setting default styles for particular elements across your website.
p { color: blue; }
Selects elements with specific class.
Specificity: 0-1-0
Class selectors are reusable and can be applied to multiple elements. They're the most flexible way to style elements and are fundamental to maintaining clean, modular CSS. Multiple classes can be applied to a single element.
.special { color: green; }
Selects element with specific ID. Should be unique.
Specificity: 1-0-0
ID selectors have the highest specificity among simple selectors. They should be used sparingly as they can make styles difficult to override and maintain. Best reserved for unique elements that appear once on a page.
#header { background: black; }
Specificity: 0-1-0
Pseudo-classes target special states of elements like :hover, :focus, :first-child, etc. They're crucial for creating interactive and dynamic interfaces without JavaScript.
a:hover { color: red; }
Notice the first letter styling.
Specificity: 0-0-1
Pseudo-elements create artificial elements in the document tree, allowing you to style specific parts of an element like ::first-letter, ::before, ::after. They're powerful for adding decorative content.
p::first-letter { font-size: 2em; }
CSS combinators define relationships between selectors:
div span { color: blue; }
div > div { border-color: red; }
div + div { background-color: #ffeb3b; }
div ~ div { background-color: #ffeb3b; }
Specificity: Varies based on selectors used
Combinators are powerful tools for creating specific styling rules based on element relationships:
Best Practices:
Specificity: 0-1-0
Attribute selectors target elements based on their attributes or attribute values. They support pattern matching using:
[attr] — Elements with the attribute[attr="value"] — Attribute equals specific value[attr^="value"] — Attribute starts with value[attr$="value"] — Attribute ends with value[attr*="value"] — Attribute contains valueinput[type="text"] { padding: 5px; }
Combined multiple classes
Specificity: Cumulative
Compound selectors combine multiple simple selectors without a combinator. They're useful for targeting specific elements that meet multiple criteria. The specificity is the sum of all included selectors.
p.special.highlighted { background: yellow; }
Modern CSS supports nesting, similar to preprocessors like Sass. This helps maintain cleaner, more organized stylesheets by grouping related styles together.
.nested-parent { .nested-child { color: purple; } }