Semantics
A big change in HTML5 from earlier versions of the standard is the introduction of more descriptive elements. As has been said numerous times, HTML is good at denoting what elements of the page are, not how they look. More descriptive, semantic elements help with that, and make your page cleaner and easier for computers to navigate as well.
Semantic elements replace and redefine earlier elements like <b>
, <i>
, and <small>
, among others. These elements defined how the text was supposed to look (bold, italicized, or small, respectively), not their purpose.
But what's the big deal? <strong>
and <b>
look the same!
Not necessarily. There's nothing in the HTML5 standard that says a <strong>
tag should be bold. Most browsers just style it that way by default. If you wanted to, you could style a <strong>
to be the same font weight but a different color than the main text, and its meaning would still be the same. This is the flexibility that semantic elements give you. You decide how certain elements look, but their meaning stays the same.
Semantic tags are important for other reasons as well:
Browser behavior
While you can make a <div>
look like a button, it won't act quite like a proper, semantic <button>
element. You won't be able to use the keyboard to navigate to it, page focus and context can get warped if a link takes the user elsewhere on the page, and a screen reader like macOS' VoiceOver will only consider it "clickable", not a proper button. Very subtle problems can creep up from misusing tags.
Accessibility
Alternative clients, like text-mode clients such as Lynx or screen readers, will not put the same kind of emphasis on a <b>
element as it will a <strong>
element, despite both looking the same by default in most browsers. Though it tries its best, a screen reader might read a navigational <div>
as part of the normal page content.
With semantic elements, the client knows what's important and what isn't right away, and can decide how to render the page based on that information.
Futureproofing
Semantic elements are very much the future of the standard. Not only will using less-semantic elements cause issues if clients change they way they render a specific tag, but a semantic site will often be treated better in search engine results. Potentially, newer browsers and analytics tools will be able to do more with a more semantic page.
Further, writing good markup now will save you the trouble of having to clean it up later.
Clarity
Finally, semantic elements are just nicer to read. You'll immediately know the purpose of a chunk of markup, and so will the people who read your markup.
It's also more satisfying to be able to style a <nav>
directly, rather than having to resort to IDs or classes.
Instead of using this, use this...
If you're looking for a good place to start, try replacing some of the less semantic elements in your markup with the following.
Instead of... | Use... | Explanation |
---|---|---|
<div> |
<header> <footer> <nav> <main> <article> <section> <aside> |
As an addendum, these are all block-level elements—the same as a |
<i> |
<em> <cite> |
The If you're citing the name of a long work, such as a paper, a novel, or an album name, |
<b> |
<strong> |
|
<s> |
<del> |
|
<u> |
<ins> |
|
<tt> |
<samp> <kbd> <var> <code> |
|
Redefined elements
Elements introduced early in the standard have been repurposed in HTML5. The <small>
tag, originally meant for small text, is now used for side comments and small print, such as disclaimers. Other examples are listed in the table above.
These tags should work similar to how they did before, so if you're already using them, you shouldn't need to do much. Just be aware of their new meaning.
Need an example?
If you need an example of how semantic tags should be used, just look around the markup of this site! It's all valid HTML5 and makes heavy use of semantic elements.
Summary:
- Semantic elements are far more descriptive than traditional HTML elements.
<strong>
,<nav>
, and<header>
are all examples of semantic elements. - While semantic elements generally have a default look (
<strong>
is often bold, for example), this isn't consistent among browsers and isn't defined by any standards. Use semantic tags only for defining what things are and use the stylesheet to control the look of the page.- Remember: HTML is great at describing the point of the content, not how the content should look.
- The benefits to using semantic elements are numerous:
- You'll have full control over how elements render, and their meaning will stay the same. A
<strong>
doesn't have to render bold, but a<b>
would be useless if it didn't. - Subtle bugs can crop up from using tags for different than intended purposes.
- Screen readers, reader modes, and other alternative clients use semantic elements to read and interpret your page.
- Search engines also use semantic elements to tailor the search results to the content of your site. Semantics also helps in the overall rankings.
- Finally, semantic elements just make your markup easier to read, for you and for others.
- You'll have full control over how elements render, and their meaning will stay the same. A