Syntax & Selectors
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.
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
| Selector | Name | Matches |
|---|---|---|
h1, h2, h3 | Selector list (grouping) | All three element types; saves repeating the same declarations |
nav a | Descendant | Any a inside a nav, at any depth |
ul > li | Child | Only li elements that are direct children of a ul |
h2 + p | Next sibling | A p immediately after an h2 |
h2 ~ p | Subsequent sibling | Every p after an h2 with the same parent |
p.intro | Compound | A p that also has class intro (no space!) |
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.
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; }; eachproperty: 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.
Tip: use the ← and → keys to move between pages.
