CSS Selector: Simple, Compound, Combinator, Complex
ai answer
CSS Selectors are patterns used to select HTML elements to style them. They are the core of CSS.
CSS selectors are broadly classified into several categories:
1. Simple Selectors
These target elements based on a single criterion.
- Type (Element) Selectors:
div,p,h1— selects by tag name. - Class Selectors:
.classname— selects elements with a specific class. - ID Selectors:
#idname— selects a unique element by ID (highest specificity). - Universal Selector:
*— selects all elements. - Attribute Selectors:
[attr],[attr=value],[attr~=value],[attr|=value],[attr^=value],[attr$=value],[attr*=value]— selects based on attributes. - Pseudo-class Selectors:
:hover,:nth-child(2),:first-child,:focus,:not(),:is(),:where(), etc. - Pseudo-element Selectors:
::before,::after,::first-line,::selection, etc.
2. Compound Selectors
A compound selector is a sequence of simple selectors without any combinators between them. It represents an element that matches all the conditions simultaneously.
Examples:
div.classname→ a<div>that also has classclassnamea#special:hover→ an<a>with idspecialthat is being hoveredinput[type="text"]:focus→ a text input that is focusedp:first-child→ a paragraph that is the first child of its parent
Compound selectors are powerful because they increase specificity without adding extra rules.
3. Combinators (Relationship Selectors)
Combinators define the relationship between two or more selectors. They are placed between selectors.
| Combinator | Name | Description | Example | Meaning |
|---|---|---|---|---|
(space) |
Descendant combinator | Selects elements that are anywhere inside another element | div p |
Any <p> inside a <div> (even nested) |
> |
Child combinator | Selects direct children only | ul > li |
<li> that are direct children of <ul> |
+ |
Adjacent sibling | Selects the element that is immediately next sibling | h2 + p |
<p> that comes right after an <h2> |
~ |
General sibling | Selects all following siblings (not necessarily immediate) | h2 ~ p |
All <p> that come after an <h2> at the same level |
Important notes on combinators:
- Combinators have different precedence. The descendant (space) has the lowest precedence.
- You can chain multiple combinators:
div > ul li + p(read from right to left). - Combinators work with compound selectors too:
section.article > h2 + p.special.
4. Other Important Selector Types
Complex Selectors
A selector that contains at least one combinator is called a complex selector.
Selector Lists
Multiple selectors separated by commas (logical OR):
h1, h2, h3, .title { font-family: sans-serif; }
Pseudo-class Functions (newer powerful ones)
:is()— logical OR inside a selector (reduces repetition):is(header, main, footer) p { ... } :where()— like:is()but with zero specificity:not()— negation:has()— parent selector (very powerful, "if it contains...")article:has(h2) { ... } /* articles that contain an h2 */
Specificity Rules (Quick Reminder)
Specificity determines which rule wins when multiple selectors match the same element.
Order (highest to lowest):
- Inline styles (
style="...") - ID selectors
- Classes, attributes, pseudo-classes
- Elements, pseudo-elements
Compound selectors add up specificity. Combinators themselves do not add specificity (except the :is()/where() behavior).
Examples in Action
/* Simple + Compound */ p.special { color: blue; } #hero { font-size: 2rem; } /* Combinators */ article > h2 { margin-top: 0; } /* direct child */ nav ul li + li { border-left: 1px solid; } /* adjacent siblings */ h1 ~ p { margin-top: 0.5em; } /* general siblings */ /* Complex compound */ div.content > p:first-child { font-weight: bold; } /* Modern */ :is(h1, h2, h3) { line-height: 1.2; } article:has(> img) { border: 1px solid; }
Summary Table
| Category | Purpose | Contains Combinators? | Example |
|---|---|---|---|
| Simple | Basic matching | No | .class, div |
| Compound | Multiple conditions on one element | No | a.button:hover |
| Complex | Relationships between elements | Yes | div p |
| Selector List | Group rules | Can | h1, h2, .title |
CSS, Selectors
- CSS Selector: Index
- CSS Selector: Tutorial
- CSS Selector: check tag name
- CSS Selector: check attribute
- CSS Selector: root element
- CSS Selector: check parent
- CSS Selector: no child
- CSS Selector: check descendant (has)
- CSS Selector: check sibling
- CSS Selector: pseudo-class
- CSS Selector: pseudo-element
- CSS Selector: negation
- CSS Selector: is any of (is where)
- CSS Selector: syntax shortcuts
- CSS Selector: Simple, Compound, Combinator, Complex