Watch on Instagram

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.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

Semantic means "about meaning"

Semantic tags say what each part of a page is, not just how it looks.

You've met some already:

  • h1 says heading
  • p says 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> ``

  • header is the top of the page, like the site title
  • main is the main content, the part the page is really about
  • footer is 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

  1. 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> ``

  1. Save it as recipes.html and open it in your browser. It looks like three plain lines.
  2. 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 h1 and p. The generic ones with no special meaning are div (and span, which you'll meet later).
  • Use a div when no tag with a meaning fits, for example a box you only need for layout.
  • A real menu uses links, made with the a tag, inside the nav. 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.

1What does "semantic" mean in HTML?
2What does a div tag say about what's inside it?
3How does <main><p>Hi, I'm Sam!</p></main> look in the browser compared with the same line using div instead of main?
4Which tag is for a menu of links?

Sources

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"

All "Web Fundamentals" lessons