Lesson 13 of 29 · CSS Basics
CSS Selectors: Element, Class, ID and Group Selectors
HTML decides what is on a page; CSS (Cascading Style Sheets) decides how it looks: colours, fonts, spacing and layout. The same HTML can look completely different with another style sheet. And everything in CSS starts with one question — which elements do you want to style? That's what selectors answer.
Where CSS goes
The simplest place for CSS is a <style> element inside <head>:
<head>
<meta charset="utf-8">
<title>My page</title>
<style>
h1 {
color: teal;
}
</style>
</head>
Bigger sites keep their CSS in a separate file and load it with <link rel="stylesheet" href="styles.css">. The rules inside are exactly the same.
Rules and declarations
A CSS rule is a selector followed by a block of declarations in curly braces. Each declaration is a property, a colon, a value and a semicolon:
p {
color: navy;
font-size: 18px;
}
Read it as: "for every <p>, make the text navy and 18 pixels tall". Comments in CSS look like /* this */.
Four kinds of selectors
- Element selector — a tag name. It matches every element of that type:
p,h1,a. - Class selector — a dot plus a class name. It matches every element with that
classattribute:.highlightmatches<p class="highlight">. Classes are reusable, and one element can have several, separated by spaces:class="card featured". - ID selector — a hash plus an id. It matches the one element with that
id:#titlematches<h1 id="title">. An id is unique on the page, so an id selector styles exactly one element. - Group selector — several selectors separated by commas share the same declarations, so you don't repeat yourself.
h2, h3 { color: darkslateblue; } /* group */
.highlight { color: crimson; } /* class */
#title { color: teal; } /* id */
<h1 id="title">The Painted Cliffs</h1>
<p class="highlight">Bring water.</p>
The cascade in one paragraph
What if two rules give the same element different colours? The cascade decides. First, the more specific selector wins: an id selector beats a class selector, and a class selector beats an element selector — so a <p class="highlight"> turns crimson even when another rule makes every p grey. If two selectors are equally specific, the rule that comes later wins. And some properties, like color and font-family, are inherited: set them on body, and every element inside follows unless a rule says otherwise.
Common mistakes
- Forgetting the dot or the hash.
highlight { … }looks for a<highlight>element, which doesn't exist. - Putting the dot in the HTML. It's
class="highlight"in HTML and.highlightin CSS. - A missing semicolon or brace. The browser skips what it can't understand, so one typo can silently cancel the declarations after it.
- Ids for things that repeat. If something can appear twice, give it a class.
Assignment
Write four rules in the <style> element:
- An element selector: all paragraphs get
color: dimgray. - A class selector: elements with the class
highlightgetcolor: crimson. - An ID selector: the element with the id
titlegetscolor: teal. - A group selector:
h2andh3headings getcolor: darkslateblue— both in one rule.
In the Preview, notice how the highlighted paragraph turns crimson even though rule 1 makes paragraphs grey: the class selector is more specific.
Premium lesson
Unlock the full Learn HTML & CSS course
- All 29 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .