HTML Layout
Most websites share a familiar shape: a header with the logo and menu, a main content area, maybe a sidebar, and a footer at the bottom. HTML provides semantic layout elements that name these regions, and CSS arranges them on the screen.
Layout elements
| Element | Typical content |
|---|---|
<header> |
Logo, site name, introductory content for a page or section |
<nav> |
A block of main navigation links |
<main> |
The unique, central content of the page (only one per page) |
<section> |
A thematic group of content, usually with its own heading |
<article> |
A self-contained piece: blog post, news story, product card |
<aside> |
Related but secondary content: sidebar, tips, ads |
<footer> |
Copyright, contact info, related links |
These elements look exactly like <div>s by default — they are block boxes with no styling. Their value is in meaning: screen readers can jump straight to the <nav> or <main>, and search engines understand what is important.
┌──────────────── header ────────────────┐
├──────────────── nav ───────────────────┤
│ main / article │ aside │
│ │ │
├──────────────── footer ────────────────┤Layout techniques
| Technique | Best for |
|---|---|
| Normal flow | Simple documents: blocks stack top to bottom |
Flexbox (display: flex) |
Arranging items in one direction — a row of menu links, a header bar |
Grid (display: grid) |
Two-dimensional page layouts with rows and columns |
| Floats | Older technique; today mainly for wrapping text around an image |
| Tables | Not for layout — only for tabular data |
A page layout with CSS Grid
Grid lets you name areas and place regions into them, almost like drawing the layout in text.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body { margin: 0; font-family: sans-serif; }
.page {
display: grid;
grid-template-areas:
"header header"
"nav nav"
"main aside"
"footer footer";
grid-template-columns: 3fr 1fr;
gap: 8px;
padding: 8px;
}
header { grid-area: header; background: #e34c26; color: white; padding: 12px; }
nav { grid-area: nav; background: #333; padding: 8px; }
nav a { color: white; margin-right: 12px; text-decoration: none; }
main { grid-area: main; background: #f4f4f4; padding: 12px; }
aside { grid-area: aside; background: #ffe0b2; padding: 12px; }
footer { grid-area: footer; background: #ddd; padding: 8px; text-align: center; font-size: 14px; }
</style>
</head>
<body>
<div class="page">
<header><h1 style="margin:0">City Library</h1></header>
<nav><a href="#">Home</a><a href="#">Catalogue</a><a href="#">Events</a></nav>
<main>
<article>
<h2>Reading club returns</h2>
<p>Our monthly reading club meets again on the first Friday of every month.</p>
</article>
</main>
<aside>
<h3>Opening hours</h3>
<p>Mon–Fri 9–18<br>Sat 10–14</p>
</aside>
<footer>© City Library</footer>
</div>
</body>
</html>Try changing 3fr 1fr to 1fr 1fr, or swap main and aside in the template to move the sidebar left.
A header bar with Flexbox
Flexbox shines for one row or column of items, such as a header with a logo on the left and links on the right.
<!DOCTYPE html>
<html>
<head>
<style>
header {
display: flex;
justify-content: space-between;
align-items: center;
background: #1e293b;
color: white;
padding: 12px 20px;
font-family: sans-serif;
}
header nav { display: flex; gap: 16px; }
header a { color: #fbbf24; text-decoration: none; }
.logo { font-weight: bold; font-size: 20px; }
</style>
</head>
<body style="margin:0">
<header>
<span class="logo">🚲 BikeFix</span>
<nav>
<a href="#">Services</a>
<a href="#">Prices</a>
<a href="#">Contact</a>
</nav>
</header>
</body>
</html>Cards with flex-wrap
Combining flexbox with flex-wrap gives a simple card layout that wraps onto new lines when space runs out.
<!DOCTYPE html>
<html>
<head>
<style>
.cards { display: flex; flex-wrap: wrap; gap: 12px; font-family: sans-serif; }
.cards article {
flex: 1 1 140px;
border: 1px solid #ccc;
border-radius: 8px;
padding: 12px;
background: #fafafa;
}
.cards h3 { margin-top: 0; }
</style>
</head>
<body>
<section class="cards">
<article><h3>🍎 Apples</h3><p>Crisp and sweet.</p></article>
<article><h3>🍐 Pears</h3><p>Soft and juicy.</p></article>
<article><h3>🍇 Grapes</h3><p>Great for snacks.</p></article>
<article><h3>🍋 Lemons</h3><p>Sour but useful.</p></article>
</section>
</body>
</html>Tip: Make the preview narrower (or resize your browser) and watch the cards wrap. In the next lesson you will learn to make whole layouts adapt to the screen size.
Test yourself
Which element should wrap the main, unique content of a page?
<section><main><body><article>
Which CSS technique is designed for two-dimensional layouts with rows and columns?
- Floats
- Flexbox
- Grid
- Tables
What is the best home for a sidebar with related links?
<aside><footer><nav>inside<header><table>