Simple Selectors
The basic building blocks. These target elements by tag, class, ID, or universal selector.
| Selector | Example | Description | Specificity |
|---|---|---|---|
| Element | p, div, h1 | Selects all elements of that type. | (0,0,1) |
| Class | .button, .active | Selects elements with that class. Prefix with dot. | (0,1,0) |
| ID | #header, #main | Selects the element with that ID. Must be unique. Prefix with hash. | (1,0,0) |
| Universal | * | Selects all elements. Lowest specificity. | (0,0,0) |
simple-selectors.css
Basic selector examples
Combinators
Combine selectors to target elements based on relationships in the HTML tree.
| Combinator | Example | Description | Specificity |
|---|---|---|---|
| Grouping | h1, h2, h3 | Multiple selectors separated by commas. Applies same styles to all. | Sum each |
| Descendant | nav a (space) | Selects all a descendants inside nav, any depth. |
Sum both |
| Child | ul > li | Selects direct li children of ul only. Not grandchildren. |
Sum both |
| Adjacent Sibling | h2 + p | Selects p immediately after h2. Must be siblings. |
Sum both |
| General Sibling | h2 ~ p | Selects all p siblings after h2. Not just the next one. |
Sum both |
combinators.css
Combinator examples
Attribute Selectors
Target elements based on HTML attributes and their values.
| Selector | Example | Description | Specificity |
|---|---|---|---|
| [attr] | [disabled] | Selects elements with that attribute, any value. | (0,1,0) |
| [attr="value"] | [type="email"] | Exact match. Selects elements where attr equals value. | (0,1,0) |
| [attr^="value"] | [href^="https"] | Starts with. Value must be at the beginning. | (0,1,0) |
| [attr$="value"] | [href$=".pdf"] | Ends with. Value must be at the end. | (0,1,0) |
| [attr*="value"] | [src*="image"] | Contains. Value can be anywhere. | (0,1,0) |
| [attr~="value"] | [class~="active"] | Word match. Value as a separate word (space-separated). | (0,1,0) |
| [attr|="value"] | [lang|="en"] | Language match. Value or value followed by hyphen. | (0,1,0) |
attribute-selectors.css
Attribute selector examples
Pseudo-Classes
Select elements based on their state or position in the document.
| Pseudo-Class | Example | Description | Specificity |
|---|---|---|---|
| :hover | a:hover | When element is hovered by mouse. | (0,1,0) |
| :focus | input:focus | When element has keyboard/focus attention. | (0,1,0) |
| :active | button:active | While element is being clicked/activated. | (0,1,0) |
| :visited | a:visited | Links to pages already visited. | (0,1,0) |
| :nth-child(n) | li:nth-child(2) | nth child of parent. 2n = even, 2n+1 = odd, 3n = every 3rd. |
(0,1,0) |
| :nth-of-type(n) | p:nth-of-type(3) | nth of its type among siblings. Ignores other element types. | (0,1,0) |
| :first-child | li:first-child | First child of parent. | (0,1,0) |
| :last-child | li:last-child | Last child of parent. | (0,1,0) |
| :only-child | p:only-child | Element with no siblings. | (0,1,0) |
| :not(selector) | p:not(.intro) | All elements except those matching selector. | 0 + inner |
| :empty | p:empty | Elements with no children or text. | (0,1,0) |
| :checked | input:checked | Checkboxes and radio buttons that are checked. | (0,1,0) |
| :disabled | button:disabled | Form elements that are disabled. | (0,1,0) |
| :enabled | input:enabled | Form elements that are enabled (not disabled). | (0,1,0) |
pseudo-classes.css
Pseudo-class examples
Pseudo-Elements
Create virtual elements or style specific parts of elements. Use :: (not :) in modern CSS.
| Pseudo-Element | Example | Description | Specificity |
|---|---|---|---|
| ::before | p::before | Insert content before an element. Use content property. |
(0,0,1) |
| ::after | p::after | Insert content after an element. Use content property. |
(0,0,1) |
| ::first-line | p::first-line | Style the first line of text in a block element. | (0,0,1) |
| ::first-letter | p::first-letter | Style the first letter of text in a block element. | (0,0,1) |
| ::selection | p::selection | Style text selected by the user. | (0,0,1) |
| ::placeholder | input::placeholder | Style placeholder text in inputs. | (0,0,1) |
| ::marker | li::marker | Style list item markers (bullets, numbers). | (0,0,1) |
pseudo-elements.css
Pseudo-element examples
Understanding Specificity
When multiple CSS rules target the same element, the one with highest specificity wins. Specificity is calculated as (ID, Class/Attr, Element).
| Selector | Specificity | Notes |
|---|---|---|
| element | (0,0,1) | Lowest. Elements beat nothing. |
| .class | (0,1,0) | Classes beat elements. Can have multiple classes. |
| #id | (1,0,0) | IDs beat everything else. Don't overuse them. |
| element.class#id | (1,1,1) | Combined specificity. Sum all parts. |
| .class1.class2 | (0,2,0) | Multiple classes stack specificity. |
| !important | Wins everything. Use sparingly โ breaks the cascade and makes code hard to maintain. | |
Specificity rule:
Higher specificity always wins. (1,0,0) > (0,9,9). If two rules have equal specificity, the one written last wins.
specificity-examples.css
Specificity comparison
Pro tips
Use classes, not IDs for styling:
IDs have high specificity and can't be reused. Classes are reusable and have moderate specificity โ much better for styling.
Descendant vs. child:
Use child combinator (ul > li) when you want only direct children. Use descendant selector (nav a) when children can be nested deeper.
Attribute selectors are powerful:
[type="email"] is more semantic than a class. Use them for form inputs, links by protocol, etc.
:not() is your friend:
Style all elements except one. li:not(:last-child) or p:not(.intro).
Pseudo-elements need content:
::before and ::after require the content property, even if it's empty: content: "";.
Use :: for pseudo-elements:
Modern CSS uses :: (double colon) for pseudo-elements. Single : still works for compatibility but is discouraged.
Africa & India's Best AI Academy
Master CSS from day one.
Build beautiful, precise layouts.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ