Learn CSS. Give your page a design.
Learn to style an HTML page with CSS. Change fonts, colours, spacing and layouts in the examples, then see how your design works at different screen widths.
Try the editorHTML.net helped many people learn to build their first website. Its original tutorials are now unavailable. I used them as a reference for this updated guide, writing my own explanations, examples and exercises that you can try in the editor below.
Try the code
Change the code, select Run example and see what happens. Each lesson’s Try this example button loads its code here.
Your code stays in this tab and is not saved when you leave. Download it to keep your work. This practice space runs HTML and CSS only. Links to sections within an example work in the preview. Scripts, forms and links to other pages are disabled there.
Loading the starter preview.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>My first web page</title> <style> * { box-sizing: border-box; } body { margin: 0; background: #f7f6f1; color: #202820; font: 1rem/1.6 system-ui, sans-serif; } .page { max-width: 60rem; margin-inline: auto; padding: 2rem 1rem; } h1 { font-size: clamp(2rem, 7vw, 4rem); line-height: 1.1; max-width: 12ch; } .eyebrow { color: #52634b; font-weight: 700; } .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr)); gap: 1rem; margin-block: 2rem; } .card { padding: 1.5rem; background: #e3e8de; border: 1px solid #b4bfae; } a { color: #294c2e; text-underline-offset: 0.2em; } a:focus-visible { outline: 3px solid #294c2e; outline-offset: 4px; } </style> </head> <body> <main class="page"> <p class="eyebrow">The local notebook</p> <h1>Make time to explore.</h1> <p>A simple page can still have a personality of its own.</p> <div class="cards"> <article class="card"><h2>Look closer</h2><p>Find a detail you have not noticed before.</p><a href="#next">Plan your walk</a></article> <article class="card"><h2>Take a photograph</h2><p>Keep a record of what catches your eye.</p><a href="#next">Choose a subject</a></article> </div> <h2 id="next">Start near home</h2> <p>All you need is a little curiosity.</p> </main> </body> </html>What CSS does
HTML describes the content of a page. CSS describes how that content is presented: the type, colour, spacing and layout. Keeping these jobs separate makes it easier to change a design without rewriting the content.
A browser applies its own default stylesheet even before you write one. That is why a heading starts out larger than a paragraph and a link is usually underlined. Your rules build on or replace those defaults.
Start with a page that has sensible HTML and readable content. CSS can make a well-structured page more useful, but it cannot turn a collection of meaningless containers into good headings or properly labelled controls.
This guide uses modern CSS. You do not need a separate CSS3 course first. Try the complete example above, then work through the individual ideas below.
Writing and connecting a stylesheet
A CSS rule consists of a selector followed by declarations inside braces. Each declaration has a property, a colon and a value. Separate declarations with semicolons. For example, p { color: #333; line-height: 1.6; } changes paragraph colour and line spacing.
For practice, put rules inside a style element in the document's head. Our editor uses that approach so each example stays in one downloadable file. For a website with several pages, save your rules in styles.css and connect it with <link rel="stylesheet" href="styles.css">.
A stylesheet path is relative to the HTML file. An image path inside an external stylesheet is relative to that stylesheet. Check those locations when a rule or background image appears to be missing.
CSS comments use /* a note */. Comment a decision you might otherwise forget, rather than describing every obvious property.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
body { padding: 2rem; font-family: system-ui, sans-serif; }
h1 { color: #52634b; }
p { line-height: 1.8; }
</style>
</head>
<body>
<h1>A change of style</h1>
<p>Edit the colour or spacing and run this example.</p>
</body>
</html>Try it yourself
Change line-height from 1.8 to 1.4. Notice that it changes spacing within the paragraph, not the gap above it.
Choosing elements with selectors
An element selector such as p matches every paragraph. A class selector such as .intro matches elements whose class includes that name. An ID selector such as #contact matches the element with that unique ID.
Classes are a useful default for reusable styling. Name them for their role, such as card or page-intro, rather than a temporary appearance such as green-box. You can change the colour without making the name misleading.
A space describes descendants: .card a matches links anywhere inside a card. A greater-than sign describes direct children: .card > h2. A comma groups selectors that should receive the same declarations.
A pseudo-class selects a state. For example, a:hover applies when a pointer is over a link. Include a visible keyboard focus state with :focus-visible rather than styling pointer interaction alone.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
body { padding: 1.5rem; font-family: system-ui, sans-serif; }
.intro { font-size: 1.4rem; }
.card { padding: 1rem; background: #e3e8de; }
.card h2 { color: #365b38; }
</style>
</head>
<body>
<h1>Choose what changes</h1>
<p class="intro">This paragraph has the intro class.</p>
<p>This paragraph does not.</p>
<article class="card"><h2>A card heading</h2><p>A paragraph inside a card.</p></article>
</body>
</html>Try it yourself
Add class="intro" to the second paragraph. Both paragraphs should now receive the same rule.
Understanding the cascade
More than one rule can match an element. The cascade decides which declaration wins. For ordinary author rules in the same cascade layer, a more specific selector wins. If specificity is equal, the later declaration wins.
For example, .intro is more specific than p, so a colour set on that class can beat a later general paragraph rule. An ID selector is more specific again. Styles written directly in an element's style attribute generally beat ordinary rules in a stylesheet.
Some properties, including text colour and font family, inherit from a parent if the child has no rule setting them. Others, including margin and border, normally do not. Setting a font on body is therefore a useful starting point.
When a rule loses, inspect the element in your browser's developer tools and look at its matched styles. Avoid adding !important every time something surprises you. First find the competing declaration and simplify the rules.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1.5rem; }
.intro { color: #365b38; }
p { color: #194ea5; }
</style>
</head>
<body>
<h1>Which rule wins?</h1>
<p class="intro">This paragraph is green.</p>
<p>This paragraph is blue.</p>
</body>
</html>Making sense of spacing and the box model
Every element produces a box. From the inside out, it has content, padding, a border and margin. Padding creates room inside the border; margin creates room outside it.
By default, a declared width describes the content box. Padding and borders add to that width. With box-sizing: border-box, the declared width includes the content, padding and border, which often makes layouts easier to reason about.
Use rem for spacing that relates to the root font size, and percentages for sizes that relate to a containing block. Pixels remain useful for details such as a one-pixel border. Prefer flexible widths with a sensible maximum over fixed widths that overflow a phone.
Neighbouring vertical margins in normal block layout can collapse into a single margin. In a Grid or Flexbox layout, gap gives you predictable spacing between items without adding outer margins to every card.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; }
.card { width: min(90%, 28rem); margin: 2rem auto; padding: 2rem; border: 2px solid #52634b; background: #e3e8de; }
h1 { margin-top: 0; }
</style>
</head>
<body>
<div class="card">
<h1>Inside and outside</h1>
<p>The green is padding. The line is the border.</p>
</div>
</body>
</html>Try it yourself
Increase padding from 2rem to 3rem. The outer card stays the same width because border-box includes its padding.
Choosing readable type and colour
Start with a readable body font, comfortable line spacing and a sensible line length. A system font stack is a lightweight option. A unitless line-height scales with the font size; around 1.5 to 1.7 is a useful starting point for body copy.
Use max-width to keep long paragraphs from stretching across the screen. The ch unit is based on the width of the font's zero character and can help set a reading measure. It is a useful approximation, not a precise character counter.
For normal text, WCAG's minimum contrast ratio is generally 4.5:1. Large text has a 3:1 threshold. Check your actual foreground and background combination, and keep links recognisable without colour alone. Do not remove underlines and focus outlines without providing a clear alternative.
Use clamp() for fluid headings with a minimum, a flexible middle value and a maximum. Keep headings readable when users zoom in.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
body { background: #f7f6f1; color: #202820; font: 1.125rem/1.65 system-ui, sans-serif; padding: 1.5rem; }
main { max-width: 60ch; margin-inline: auto; }
h1 { font-size: clamp(2rem, 6vw, 3.5rem); line-height: 1.1; }
a { color: #365b38; text-underline-offset: 0.2em; }
a:focus-visible { outline: 3px solid #365b38; outline-offset: 4px; }
</style>
</head>
<body>
<main><h1>Words need room.</h1><p>Good typography gives your reader a comfortable route through the page. Start with a clear size, enough line spacing and a line length that does not stretch forever.</p><a href="#more">Read the next note</a><h2 id="more">A little restraint helps</h2><p>Use a few purposeful styles consistently.</p></main>
</body>
</html>Arranging items with Flexbox
Flexbox is useful when you want to arrange items primarily along a row or a column, such as navigation links or a line of controls. Set display: flex on the parent, not on every child.
The default main axis runs along a row. justify-content distributes space along that axis and align-items aligns items across it. Change flex-direction to column and those axes change with it.
Allow wrapping with flex-wrap: wrap when items may not fit. Use gap for the space between them. Avoid changing the visual order in a way that conflicts with the HTML reading order, especially for keyboard users.
In the example, each link keeps a comfortable clickable area and wraps to a new line when needed. Try the narrow preview to see the result without changing the source.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
body { padding: 1rem; font-family: system-ui, sans-serif; }
nav { display: flex; flex-wrap: wrap; gap: 0.75rem; }
nav a { padding: 0.75rem 1rem; background: #e3e8de; color: #202820; }
a:focus-visible { outline: 3px solid #365b38; outline-offset: 3px; }
</style>
</head>
<body>
<h1>Choose a topic</h1>
<nav aria-label="Topics">
<a href="#walks">Local walks</a>
<a href="#photography">Photography</a>
<a href="#food">Food and drink</a>
</nav>
<h2 id="walks">Local walks</h2><p>Find a new route.</p>
<h2 id="photography">Photography</h2><p>Bring your camera.</p>
<h2 id="food">Food and drink</h2><p>Stop for a break.</p>
</body>
</html>Try it yourself
Add justify-content: center to nav. Then change flex-direction to column and compare the result.
Building a layout with CSS Grid
Grid is useful for layouts with rows and columns. Set display: grid on a container and define its columns with grid-template-columns. For example, repeat(3, 1fr) makes three equal flexible columns.
A fixed column count can become cramped on a small screen. The pattern repeat(auto-fit, minmax(min(100%, 16rem), 1fr)) lets the browser fit as many columns as it can. Each column aims to be at least 16rem wide but can shrink to fit a narrower container.
The fr unit shares available space within the grid. gap sets the spaces between its tracks. Start with a simple repeated card layout before moving on to spanning items or named grid areas.
Grid and Flexbox work together. You might use Grid for a collection of cards and Flexbox for the links inside each card. Use whichever suits the part of the page you’re working on.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
* { box-sizing: border-box; }
body { padding: 1rem; font-family: system-ui, sans-serif; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr)); gap: 1rem; }
article { padding: 1.5rem; background: #e3e8de; }
h2 { margin-top: 0; }
</style>
</head>
<body>
<h1>Three local discoveries</h1>
<div class="cards">
<article><h2>A quiet path</h2><p>Somewhere to slow down.</p></article>
<article><h2>A good coffee</h2><p>Somewhere to take a break.</p></article>
<article><h2>A new view</h2><p>Somewhere to bring a camera.</p></article>
</div>
</body>
</html>Try it yourself
Change 16rem to 12rem and compare the wide and narrow previews. The minimum column width changes how many cards fit.
Making a page work on different screens
Responsive design starts with flexible content. Use widths that can shrink, let text wrap and set images to max-width: 100%; height: auto. Keep the viewport meta element in your HTML head so a phone uses its actual device width.
Write the simplest narrow-screen layout first. Add a media query when the content has enough room to benefit from a different arrangement. A breakpoint is a design decision based on your content, not a requirement to target every named phone or tablet.
The example starts as one column and becomes two above 42rem. Test the point just before and after that change, not only a wide desktop and a narrow phone. Also test browser zoom and unusually long words.
Prefer normal document flow to fixed heights for text containers. Text grows when people increase its size or translate a page. A box that only fits your original paragraph is likely to break.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; padding: 1rem; font: 1rem/1.6 system-ui, sans-serif; }
main { display: grid; gap: 1.5rem; max-width: 60rem; margin-inline: auto; }
aside { padding: 1rem; background: #e3e8de; }
@media (min-width: 42rem) {
main { grid-template-columns: 2fr 1fr; }
}
</style>
</head>
<body>
<main><div><h1>A flexible layout</h1><p>This is the main story. It starts as a single column.</p></div><aside><h2>A useful note</h2><p>Give the content room when space allows.</p></aside></main>
</body>
</html>Handling images, borders and backgrounds
A content image belongs in HTML with a suitable text alternative. A purely decorative texture or shape can be a CSS background. A background has no alt attribute, so do not use one as the only way to convey essential information.
Set height: auto when scaling an image without cropping. If a design needs a consistent frame, combine aspect-ratio with object-fit: cover. That crops the image to fill its box, so check that you have not cut off a face or an important detail. Adjust object-position if needed.
Borders, corner radii and shadows can help separate content, but use them with a purpose. Add an effect only if it makes the content easier to distinguish or use. Define recurring colours as custom properties such as --surface so you can make a consistent change in one place.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
* { box-sizing: border-box; }
:root { --surface: #e3e8de; }
body { font: 1rem/1.6 system-ui, sans-serif; padding: 1rem; }
.card { max-width: 32rem; margin: auto; border: 1px solid #b4bfae; background: var(--surface); }
img { display: block; width: 100%; height: auto; }
.card div { padding: 1.5rem; }
h1 { margin-top: 0; }
</style>
</head>
<body>
<article class="card">
<img src="https://susnet.co.uk/images/new-cristis-sign.webp" alt="Cristis shop sign in Billericay" width="800" height="534">
<div><h1>A local favourite</h1><p>Make the photograph part of the story.</p></div>
</article>
</body>
</html>Keeping interactions comfortable
Give interactive elements distinct hover and keyboard focus states. Focus is the indicator that tells a keyboard user which control will respond. Removing it makes a page harder to use.
A transition can soften a small visual change. Keep it specific, such as transition: background-color 150ms, rather than animating every property. Avoid movement that distracts from reading or changes the page layout unexpectedly.
Respect a reduced-motion preference with @media (prefers-reduced-motion: reduce). In the example, the colour still changes, but the transition is removed for people who request less motion. Do not hide essential information behind a hover effect, since touchscreens and keyboard navigation do not work in the same way as a mouse.
View and copy the code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first web page</title>
<style>
body { padding: 1.5rem; font: 1rem/1.6 system-ui, sans-serif; }
.link { display: inline-block; padding: 0.75rem 1rem; background: #e3e8de; color: #202820; transition: background-color 150ms; }
.link:hover { background: #c5d0bd; }
.link:focus-visible { outline: 3px solid #365b38; outline-offset: 4px; }
@media (prefers-reduced-motion: reduce) {
.link { transition: none; }
}
</style>
</head>
<body>
<h1>A clear next step</h1>
<a class="link" href="#note">Read the note</a>
<h2 id="note">Keep controls recognisable</h2>
<p>Try pressing Tab to reach the link.</p>
</body>
</html>Testing your finished design
Use your browser's inspector to select an element and see the rules applied to it. A crossed-out declaration has lost to another rule or is disabled. An invalid property or value may be ignored. Check spelling, units, braces and the stylesheet path before adding more rules.
Test narrow and wide layouts, keyboard navigation and browser zoom. Look for clipped text, horizontal scrolling and controls that are difficult to reach. Check colour contrast with a dedicated contrast checker. If you add a web font, make sure the fallback still looks readable while the font loads.
For a final exercise, start with the complete example at the top. Change its colours, typography and spacing, add a third card and make the content your own. Download the result and open it in a browser. Our examples keep the CSS in the document; you can then move it into an external stylesheet as practice.
Keep MDN's CSS reference, the W3C CSS validator and W3C's accessibility tutorials nearby as you build. Validation can identify syntax problems, but using the page yourself remains essential.
Try it yourself
Add a third card to the starter page. Check its layout in both preview widths, follow every link with the keyboard and try browser zoom at 200%.
CSS quick reference
- .card { … }
- Select elements with the card class.
- padding / margin
- Space inside / outside the border.
- box-sizing: border-box
- Include padding and borders in a declared width.
- display: flex / grid
- Arrange children with Flexbox or Grid.
- gap: 1rem
- Space between Grid or Flexbox items.
- max-width: 100%
- Limit an element to its container’s width.
- :focus-visible
- Style a visible keyboard focus state.
- @media (min-width: 42rem)
- Apply rules when the viewport reaches that width.
Practise HTML and CSS together
Keep the HTML guide open while you practise styling. It explains the page structure that your CSS rules work with.
Learn HTMLI’m Paul, a web designer, photographer and filmmaker based in Basildon. If you’d like help with a business website, explore my web design work or tell me about your project.
