
web-development · Basic · 2026-07-02
CSS Basics: How to Style Your First Web Page
Learn how CSS selectors, properties and values work — then practise colours, fonts and spacing in the live editor on this page.
CSS (Cascading Style Sheets) controls how HTML looks — colours, fonts, spacing, layout. Where HTML says "this is a heading", CSS says "make that heading big, blue and centred".
The anatomy of a CSS rule
Every CSS rule has three parts:
selector {
property: value;
}
For example:
h2 {
color: teal;
font-size: 28px;
}
- Selector (
h2) — which elements to style. - Property (
color) — what aspect to change. - Value (
teal) — what to change it to.
The three most common selectors
- Element selector —
p { ... }styles every paragraph. - Class selector —
.card { ... }styles every element withclass="card". Classes are reusable, so this is the selector you will use most. - ID selector —
#header { ... }styles the one element withid="header".
The box model — why spacing works the way it does
Every element on a page is a box with four layers, from inside out: content → padding → border → margin. Padding is space inside the border; margin is space outside it. Getting comfortable with this one idea solves most beginner layout confusion.
Practise below
The editor contains a small card with a heading, paragraph and button, already styled. Try these challenges:
- Change the card's
bordercolour and makeborder-radiusbigger. - Give the button a different
backgroundcolour. - Add
box-shadow: 0 4px 12px rgba(0,0,0,0.15);to the.cardrule. - Add a new rule that makes the
h2uppercase:text-transform: uppercase;
Press Run after each change to see the effect.
Practice exercises
These go a step beyond the challenges above — each uses the same card in the editor.
Exercise 1 (easy): Write a new rule that makes the paragraph inside the card grey. Target it with .card p so paragraphs outside the card would stay untouched.
Solution:
.card p {
color: #666;
}
Exercise 2 (medium): Make the button react to the mouse: on hover, darken its background and show a pointer cursor. You will need the :hover pseudo-class.
Solution:
button:hover {
background: #0a5568;
cursor: pointer;
}
Exercise 3 (challenge): Create a dark variant of the card. Add a .card-dark rule (dark background, white text), then duplicate the card <div> in the HTML tab and give the copy class="card card-dark" — one element carrying two classes.
Solution:
.card-dark {
background: #1a2733;
color: #fff;
border-color: #4ADE80;
}
<div class="card card-dark">
<h2>Dark mode card</h2>
<p>Two classes, one element.</p>
<button>Click me</button>
</div>
Practise what you learned
Edit the code below and press Run to see your changes live.
Test yourself
Answer from memory first, then check yourself against the answer.
Q1What are the three parts of every CSS rule?
The selector (which elements to style, like .card), the property (what to change, like color) and the value (what to change it to, like crimson). Together: .card { color: crimson; }
Q2What is the difference between a class selector and an element selector?
An element selector like button styles every element of that type on the page. A class selector like .card only styles elements that carry that class attribute — giving you precise control over which elements are affected.
Q3If two rules set a different color on the same element, which one wins?
The more specific selector wins (a class beats an element selector); when specificity is equal, the rule that appears later in the stylesheet wins. This is the 'cascade' in Cascading Style Sheets.