Cascading Style Sheets (CSS) selectors are patterns that tell the browser which HTML elements to apply a given style to. They're the foundation of CSS, allowing you to target specific elements on a webpage for styling.
p { color: blue; }.highlight { background-color: yellow; }Combinators are used to combine other selectors in meaningful ways.
div p { color: green; }ul > li { list-style-type: square; }a[target] { color: purple; }img[src$=".png"] { border: 2px solid red; }Pseudo-classes select elements based on their state or position.
button:hover { background-color: lightblue; }li:nth-child(2n) { color: red; }div:not(.active) { opacity: 0.5; }Pseudo-elements allow you to style specific parts of an element.
p::before { content: "★ "; }p::after { content: " ✔"; }p::first-letter { font-size: 2em; }| Aspect | Pseudo-classes | Pseudo-elements |
|---|---|---|
| Definition | Select elements based on state or position. | Select parts of an element (not whole elements). |
| Syntax | Single colon (:) | Double colon (::) |
| Examples | :hover, :nth-child | ::before, ::first-letter |
By combining pseudo-classes and pseudo-elements, you can achieve highly specific and creative styling with minimal markup.