Skip to content
Kuza Ndoto Academy

Syntax & Selectors

Beginner → IntermediateChapter 1 · Page 3 of 224 min readUpdated 15 Nov 2026

A CSS rule has two parts: a selector that picks which elements to style, and a declaration block that says how to style them. Selectors are the heart of CSS, because you can't style something you can't select.

Anatomy of a rule

h1 {
  color: navy;
  font-size: 2rem;
}
  • Selector (h1): which elements the rule applies to.
  • Declaration block: everything between { and }.
  • Declaration: one property: value; pair.
  • Property (color): what you want to change.
  • Value (navy): what you want to change it to.
h1 { color : navy ; font-size : 2rem ; } Selector Declaration Declaration property value property value Declaration block (everything inside the braces)
Figure 1. A rule is a selector followed by a block of declarations. Each declaration is a property and a value joined by a colon and ended with a semicolon.

Whitespace and line breaks don't matter to the browser, so h1{color:navy} works too, but one declaration per line is far easier to read. The semicolon after the last declaration is optional, but always adding it prevents bugs when you add another line later.

The basic selectors

Type (element) selector

Matches every element of that type: p styles all paragraphs, button styles all buttons.

Class selector

Starts with a dot and matches every element whose class attribute contains that name. Classes are reusable and are the selector you will use most.

<p class="price">KES 1,200</p>
<p class="price sale">KES 950</p>
.price { font-weight: bold; }
.sale  { color: crimson; }

Both paragraphs are bold. Only the second is crimson, because an element can have several classes separated by spaces.

ID selector

Starts with # and matches the element with that id. An id must be unique on the page, so #site-logo matches at most one element. IDs are very strong in the cascade, so most developers prefer classes for styling and keep ids for links and JavaScript.

Universal and attribute selectors

* matches every element. Attribute selectors match by attribute: [disabled] matches any element that has the attribute, and input[type="email"] matches only email inputs. Variants such as a[href^="https"] (starts with) and a[href$=".pdf"] (ends with) are useful for links.

Combining selectors

SelectorNameMatches
h1, h2, h3Selector list (grouping)All three element types; saves repeating the same declarations
nav aDescendantAny a inside a nav, at any depth
ul > liChildOnly li elements that are direct children of a ul
h2 + pNext siblingA p immediately after an h2
h2 ~ pSubsequent siblingEvery p after an h2 with the same parent
p.introCompoundA p that also has class intro (no space!)
Common mistake

p.intro and p .intro are different. Without the space you select paragraphs that have the class; with the space you select elements with the class that are inside a paragraph.

pTypeevery p element .priceClasselements withclass="price" #logoIDthe one elementwith id="logo" *Universalevery element [type="email"]Attributetype is "email" nav aDescendanta anywhere in nav ul > liChildli directly in ul h1, h2Selector listall h1 and h2
Figure 2. The simple selectors (top row) pick elements by type, class, id or everything; attribute selectors and combinators (bottom row) narrow or widen the match.

Try it

This CSS is applied to a sample page with a heading, a paragraph, a link and a button. Try changing the selectors.

* {
  box-sizing: border-box;
}

h1, button {
  font-family: Georgia, serif;
}

p a {
  color: #b45309;
  font-weight: bold;
}

button {
  background-color: #1e3a8a;
  color: white;
  border: none;
  padding: 0.6rem 1.2rem;
}

The heading and the button switch to the Georgia serif font. The link inside the paragraph turns bold and amber, and the button becomes a dark blue rectangle with white text. If the link isn't inside a paragraph on the sample page, change p a to just a.

Key points

  • A rule is selector { property: value; }; each property: value; pair is a declaration.
  • Use type, class (.name), id (#name), universal (*) and attribute ([attr]) selectors.
  • Combinators (space, >, +, ~) select by relationship; commas group selectors.
  • Prefer classes for styling: they are reusable and easy to override.
Up next · Getting StartedColoursNamed colours, hex, rgb, hsl and transparency.
→

Tip: use the ← and → keys to move between pages.