Introduction
You could build a whole page out of div elements, and with CSS it would look perfect. But to a screen reader or a search engine, it would be a long, undifferentiated pile of text. Semantic elements tell them what each region of the page is for, and they cost nothing to use.
How it works
Think of the page regions that nearly every site has:
| Element | Means |
|---|---|
header | introductory content: logo, site title, sometimes a menu |
nav | a main block of navigation links |
main | the main content of this page; only one is visible |
article | something that makes sense on its own: a post, a product, a comment |
section | a themed part of a larger whole, usually with a heading |
aside | related but secondary content: a sidebar, a note |
footer | closing information: copyright, contacts, related links |
These are landmarks. A screen reader user can open a list of them and jump straight to the main content, without hearing the entire menu on every page. Search engines use the same structure to work out what is the actual content and what is decoration around it.
Choosing between article, section and div. Ask two questions. Could this be taken out and make sense somewhere else, like in a news feed? Then it is an article. Is it a distinct part of the topic, with a heading? Then it is a section. Do you just need a wrapper to style? Then use a div. A div is not wrong: it is the right choice precisely when no other element describes the content. span is the inline version, for styling a few words.
Some other useful elements: time gives a date in a form programs can read (<time datetime="2030-03-26">26 March</time>), blockquote marks a long quotation, code marks code, and address gives contact details for the page or article.
Buttons and links are semantics too. A real button can be focused with the keyboard and activated with Enter or Space, and it is announced as a button. A div with a click handler has none of that unless you rebuild it by hand, and people rarely do.
Syntax
<body>
<header>...</header>
<nav>...</nav>
<main>
<article>...</article>
</main>
<footer>...</footer>
</body>
Example
Line by line
headerholds the site title.navholds the main links, and a screen reader can jump to it.mainwraps what is unique to this page.articleis the news item, which would make sense on its own.timegives programs a machine-readable date, while people see "26 March".asideholds related content, andfootercloses the page.
Expected result
The page looks plain, because semantic elements have almost no default style. That is expected: they change what the page means, and CSS changes how it looks.
In the real world
Rich search results, browser "reader" views and voice assistants rely on structure to find the real content in a page. Many countries require public websites to be accessible, and landmarks are one of the first things an accessibility audit looks for.
Common mistakes
- Using several
mainelements that are visible at once. - Using
sectionas a styling wrapper, when adivis what you mean. - Wrapping every small group of links in
nav. Keep it for major navigation. - A clickable
divin place of abutton.
Best practices
- Sketch the regions of the page before you write it.
- Give each
sectiona heading. - Reach for a semantic element first, and for a
divwhen none fits.
Summary
Semantic elements name the regions of a page. Landmarks give assistive technology a map, and search engines a clearer picture. article stands alone, section is a part with a heading, and div and span are right when nothing else fits.
Quick check
Key takeaways
- A page has one main, and usually a header, nav and footer.
- article stands alone; section is a themed part of something larger, usually with a heading.
- div and span are right when no element describes the content.
- Semantics cost nothing, and they give screen reader users a way to navigate.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.