Selectors
Selectors are how CSS points at elements: “all paragraphs”, “the buttons inside the header”, “links that open in a new tab”. Master them and you can style anything.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
Learning objectives#
- Use element, class and id selectors
- Target elements by relationship: descendant, child and sibling
- Use attribute selectors
- Choose the simplest selector that works
Explanation#
Basic: p (all paragraphs), .card (class="card"), #logo (id="logo"), * (everything).
Combined: button.primary means a button that also has class primary (no space).
Relationships: .nav a (any a inside .nav — descendant, with a space); ul > li (direct children only); h2 + p (the p right after an h2); h2 ~ p (all p siblings after an h2).
Attribute: [type="email"], a[target="_blank"], a[href^="https"] (starts with), img[src$=".svg"] (ends with).
Prefer classes for styling. They are reusable, easy to read and have moderate specificity.
Syntax#
p { } /* element */
.card { } /* class */
#logo { } /* id */
.nav a { } /* descendant */
ul > li { } /* direct child */
h2 + p { } /* next sibling */
[type="email"] { } /* attribute */
Code example: Selectors in action#
<style>
body { font-family: sans-serif; }
.menu a { color: #2965f1; } /* links inside .menu */
.menu > li { list-style: square; } /* direct children of .menu */
h2 + p { font-weight: bold; } /* first p after an h2 */
a[target="_blank"]::after { content: " ↗"; }
input[type="email"] { border: 2px solid #16a34a; }
button.primary { background: #2965f1; color: white; }
</style>
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="https://mdn.dev" target="_blank">MDN</a></li>
</ul>
<h2>News</h2>
<p>This paragraph is bold (it follows the h2).</p>
<p>This one is normal.</p>
<input type="email" placeholder="email"> <input type="text" placeholder="text">
<button class="primary">Primary</button> <button>Plain</button>
Blue menu links with square bullets, an arrow after the external link, a bold first paragraph, a green email box and one blue button.
Step-by-step explanation#
- .menu a — the space means “anywhere inside .menu”.
- .menu > li — the > means “direct children only”.
- h2 + p — only the paragraph immediately after the heading.
- a[target="_blank"] — only links that open a new tab get the arrow.
- input[type="email"] — the email input, not the text input.
- button.primary — no space: a button that has the class primary.
Real-world use case#
Attribute selectors style form fields by type; child selectors style a menu’s top level differently from its dropdowns.
Common mistakes#
.card .title (title inside card) is very different from .card.title (one element with both classes).
body div.container ul li a is fragile and hard to override. A single class like .nav-link is better.
#header is very specific and cannot be reused. Use classes for styling.
Best practices#
- Style with classes; keep ids for JavaScript and anchors
- Keep selectors short (1–2 parts)
- Use attribute selectors for form types and link types
- Test selectors in DevTools with document.querySelectorAll()
Remember this#
. = class, # = id, nothing = element.
Space = inside (any depth). > = direct child. + = next door neighbour. ~ = all later siblings.
Same selectors work in JavaScript: document.querySelector(".card").
Practice exercise#
Your task: Make every link inside a <footer> grey, and every <li> that is a direct child of .steps bold.
Need a hint?
Show solution
footer a { color: grey; }
.steps > li { font-weight: bold; }
Mini quiz#
Which selector targets <li> elements that are direct children of <ul>?
Want more? Practise all CSS MCQs · CSS interview questions
My notes#
Saved in this browser.