Code Bytes · "Web Fundamentals" · Episode 5 · HTML
Semantic HTML
Semantic HTML means using tags for their meaning, so each tag says what that part of the page is. A div is just a plain box. header, main, footer and nav look the same as plain boxes in the browser, but screen readers can jump straight to the main part and search engines understand the page better.
Semantic means "about meaning"
Semantic tags say what each part of a page is, not just how it looks.
You've met some already:
h1says headingpsays paragraph
A div is just a plain box
A div tag is a plain box. You can put anything inside it, but it doesn't say what's inside. A box with no label.
Wrapping a page in meaning
Here's a tiny blog page. We wrap "My Blog" in a header tag, the post in a main tag, and "By Sam" in a footer tag:
``html <header>My Blog</header> <main><p>Hi, I'm Sam!</p></main> <footer>By Sam</footer> ``
headeris the top of the page, like the site titlemainis the main content, the part the page is really aboutfooteris the bottom, often with the author or contact details
It looks the same
In the browser, this page looks exactly the same as if every tag were a plain div:
My Blog Hi, I'm Sam! By Sam
Each one is drawn as a plain block on its own line. How it looks comes later, with CSS.
So why bother?
Because the meaning is for the programs that read your page, not just for your eyes:
- Screen readers, apps that read pages aloud for people who are blind or have low vision, use these tags as landmarks. A listener can jump straight to the main part instead of hearing the whole header first.
- Search engines can use the structure to understand the page better.
One more: nav
nav is for a menu of links, like Home, Shoes and Contact at the top of a shop's page.
Try it yourself
- Open a plain text editor (Notepad on Windows, TextEdit in plain-text mode on a Mac) and type:
``html <header>My Recipes</header> <main><p>Today: pancakes!</p></main> <footer>By Alex</footer> ``
- Save it as
recipes.htmland open it in your browser. It looks like three plain lines. - Now change all three tags to
div(opening and closing), save, and refresh. It looks exactly the same, but the page no longer says which part is the top, the main content and the bottom.
Good to know
- Most HTML tags have a meaning, like
h1andp. The generic ones with no special meaning arediv(andspan, which you'll meet later). - Use a
divwhen no tag with a meaning fits, for example a box you only need for layout. - A real menu uses links, made with the
atag, inside thenav. Links come later; here "Home | Shoes" is just text. - Semantic tags don't change how a page looks by default. CSS does that, and it's next.
Answer to the comment question
How many of these tags are semantic?
``html <header>Shop</header> <div>Sale!</div> <nav>Home | Shoes</nav> <div>New in</div> <footer>Contact us</footer> ``
3: header, nav and footer each say what that part of the page is. The two div tags are plain boxes with no special meaning.
Test your knowledge
Quick quiz: Semantic HTML
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- MDN Web Docs, "Semantics": semantic HTML uses elements for their meaning, which helps screen readers and search engines
- MDN Web Docs, "The Content Division element": div is a generic container with no special meaning
- MDN Web Docs, "The Header element", "The Main element" and "The Footer element": what header, main and footer mean
- MDN Web Docs, "The Navigation Section element": nav is a section of navigation links
- MDN Web Docs, "HTML: A good basis for accessibility": screen readers use semantic elements to move around a page
Video transcript
A header tag looks just like a plain box. So why use it?
Semantic means about meaning: tags that say what each part of a page is.
A div tag is just a plain box. It doesn't say what's inside.
You've met some already: H one says heading, P says paragraph.
Now we wrap My Blog in a header tag, the post in a main tag, and By Sam in a footer tag.
In the browser, it looks the same as plain boxes.
But screen readers, apps that read pages aloud, can jump straight to the main part, and search engines understand it better.
One more: nav, for a menu of links.
How many of these tags are semantic? Comment your answer below!
That's your byte.
More from "Web Fundamentals"
- "Web Fundamentals" EP1What happens when you open a website
- "Web Fundamentals" EP2Front end vs back end
- "Web Fundamentals" EP3What HTML is