Semantic Layout - HTML Tutorial
Most web pages share the same basic parts: a header at the top, some navigation, the main content and a footer at the bottom. HTML has an element for each of these parts. Using them is called semantic layout, because each element describes what that part of the page is.
The Layout Elements
header-
Introductory content, usually at the top of the page: a logo, the site name, and often the main navigation. An
articleorsectioncan also have its ownheader. nav-
A block of major navigation links, such as the main site menu or a table of contents. Not every group of links needs to be a
nav, just the important ones. main-
The main content of the page: the part that's unique to this page, as opposed to the header, navigation and footer that repeat across the site. Use only one visible
mainper page. article-
A self-contained piece of content that would make sense on its own, such as a blog post, a news story, a product card or a comment.
section-
A thematic group of content, usually with its own heading. For example, a home page might have sections for "Our Menu", "Opening Hours" and "Find Us".
aside-
Content that's related to the main content, but separate from it, such as a sidebar, a pull quote or a box of related links.
footer-
Closing content, usually at the bottom of the page: copyright information, contact details and secondary links. Like
header, anarticlecan have its ownfootertoo.
A Complete Page
Here's how these elements fit together on a typical page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Corner Cafe - Fresh Coffee and Homemade Food</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<p class="logo">Corner Cafe</p>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="menu.html">Menu</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
</header>
<main>
<h1>Welcome to the Corner Cafe</h1>
<section>
<h2>This Week's News</h2>
<article>
<h3>New Autumn Menu</h3>
<p>Pumpkin soup and apple crumble are back from Monday.</p>
</article>
<article>
<h3>Live Music on Fridays</h3>
<p>Join us for acoustic sets every Friday afternoon.</p>
</article>
</section>
</main>
<aside>
<h2>Opening Hours</h2>
<p>Every day, 7am to 4pm.</p>
</aside>
<footer>
<p><small>© Corner Cafe. All rights reserved.</small></p>
</footer>
</body>
</html>
The © in the footer is a character reference. It displays the copyright symbol, ©. Character references like this let you type special characters using plain keyboard letters.
Why Use Semantic Elements?
Visually, these elements look exactly like div elements: they're plain blocks with no special styling. So why not just use div for everything? Because the meaning helps in several ways:
- Accessibility. Screen readers treat
header,nav,main,asideandfooteras landmarks. Users can jump straight to the main content or the navigation, instead of listening to the whole page from the top. - Search engines. Clear structure helps search engines work out which part of the page is the real content.
- Readable code.
</nav>tells you far more than</div>when you come back to your code later.
Compare the semantic example above with the same page built only from div elements:
<div class="header">
<div class="nav">...</div>
</div>
<div class="main">...</div>
<div class="footer">...</div>
A person reading the class names can guess what's going on, but browsers and screen readers can't. They only see a pile of anonymous boxes.
Common Mistakes
- Using
sectionas a generic wrapper. If a block of content doesn't have a heading and isn't a distinct topic, adivis the better choice. - More than one
main. Each page should have exactly one main content area. - Wrapping every list of links in
nav. Keepnavfor major navigation blocks, such as the main menu. - Choosing an element for its look. Semantic elements have no special appearance. Choose them for their meaning, then style them with CSS.
Your page now has a solid structure, but everything is still stacked in one column. In the next section, you'll start arranging things side by side.