CSS · Fundamentals

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.

Beginner 5 min Lesson 3 of 26 Runnable examples
Before you start:CSS Syntax
0/26 in CSS
On this page · 5 min · Beginner

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#

CSS Illustrative
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#

HTMLSelectors in action Runnable
<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>
Expected output
Blue menu links with square bullets, an arrow after the external link, a bold first paragraph, a green email box and one blue button.
Opens this example in the full playground

Step-by-step explanation#

  1. .menu a — the space means “anywhere inside .menu”.
  2. .menu > li — the > means “direct children only”.
  3. h2 + p — only the paragraph immediately after the heading.
  4. a[target="_blank"] — only links that open a new tab get the arrow.
  5. input[type="email"] — the email input, not the text input.
  6. 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#

Space vs no space

.card .title (title inside card) is very different from .card.title (one element with both classes).

Over-long selectors

body div.container ul li a is fragile and hard to override. A single class like .nav-link is better.

Styling with ids

#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?
One descendant selector and one child selector.
Show solution
CSS Illustrative
footer a { color: grey; }
.steps > li { font-weight: bold; }

Mini quiz#

Selectors quizMultiple choice Question 1 of 3

Which selector targets <li> elements that are direct children of <ul>?

Score so far: 0/0

Want more? Practise all CSS MCQs · CSS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.