What precisely is semantic HTML and why is it needed
Tags: html, frontend, programming, tutorial
Published: 2024-05-04 23:08:22

A while ago, almost everyone made websites without considering what was under the hood. They relied on tables, used whatever was available, mostly [&lt;div&gt;](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div) and [&lt;span&gt;](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span) tags, and didn't care about [accessibility](https://developer.mozilla.org/en-US/docs/Learn/Accessibility/What_is_accessibility). 

But with the emergence of HTML5, things have changed. Semantic layout is an approach to markup that depends not on the content of the site but on the semantic purpose of each block and the logical structure of the document. Even in this article, there are headings of different levels, which helps the reader to build the structure of the document in their head.

## Why semantics is important
**To make a website accessible for all users,** including those who are blind or partially blind, it is important to use semantic markup. This helps screen readers navigate the page and understand its content.

To make the site higher in search engines and also improve SEO optimisations. Search engines do not disclose ranking rules, but it's known that the presence of semantic page markup helps search bots better understand what is on the page and, depending on this, rank sites in search results.

The HTML standard has several semantic tags that are recommended for page markup instead of using &lt;div> and &lt;span>. Semantic elements describe their role, making it easier to read and understand the code.

Well, imagine how much easier it is to read &lt;header>&lt;/header> instead of &lt;div class="header">&lt;/div>. Or here is the code. Look, and it is immediately clear what is here and why.

```html
<!DOCTYPE html>
<html lang="en">
 <head>
   <meta charset="utf-8">
   <title>Page title</title>
 </head>
 <body>
   <header class="header">
     <!- Page header ->
   </header>
   <main>
     <!- Main page content ->
   </main>
   <footer class="footer">
     <!- Footer content ->
   </footer>
 </body>
</html>
```

## Basic semantic HTML tags
Within the realm of early HTML versions, some tags hold semantic value, such as the [&lt;p>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/p) tag representing a paragraph. In contrast, tags like [&lt;i>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/i) or [&lt;b>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/b) lack semantic significance as they solely dictate text styling rather than conveying meaningful information.

The latest HTML Living Standard has semantic tags for every major site component and should be used. Here are some examples:


### &lt;article>
* Meaning: An independent, separable semantic unit, such as a comment, tweet, article, etc.
* Features: Desirable heading inside.
* Common mistakes: confused with [&lt;section>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section) and [&lt;div>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div) tags.

### &lt;section>
* Meaning: semantic section of the document. Non-separable, unlike [&lt;article>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/article).
* Features: Desirable heading inside.
* Common mistakes: confused with [&lt;article>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/article) and [&lt;div>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div) tags.

### &lt;aside>
* Meaning: side, indirect content for the page.
* Features: may have its own title. May appear multiple times on a page.
* Common mistakes: treat [&lt;aside>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/aside) tag for "sidebar" and use this tag to mark the main content that is related to the elements surrounding it.

### &lt;nav>
* Meaning: A navigational section with links to other pages or other parts of pages.
* Features: Used for main navigation, not for all link groups. The main one is navigation or not - at the discretion of the layout designer. For example, the menu in the site's footer can be left unwrapped in [&lt;nav>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/nav). A short list of links usually appears in the footer (for example, a link to the main page, copyright and conditions) - this is not the main navigation, semantically, [&lt;footer>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/footer) itself is intended for such information.
* Common mistakes: Many people think that [&lt;nav>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/nav) can only have a list of navigation links, but according to the [specification](https://html.spec.whatwg.org/multipage/sections.html#the-nav-element), there can be navigation in any form.

### &lt;header>
* Meaning: the introductory part of a semantic section or the entire site, usually contains hints and navigation. Most often repeated on all pages of the site.
* Features: There can be several of these elements on a page.
* Typical mistakes: use only as a site header.

### &lt;main>
* Meaning: the main, not repeated on other pages, content of the page.
* Features: Must be one per page, based on definition.
* Common mistakes: include in this tag what is repeated on other pages (navigation, copyrights, and so on).

### &lt;footer>
* Meaning: the final part of the semantic section or the entire site, usually contains information about the authors, a list of references, copyright, and so on. Most often repeated on all pages of the site.
* Features: There can be several of these elements on a page. The [&lt;footer>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/footer) tag does not have to be at the end of a section.
* Typical mistakes: use only as a site footer.

## How to mark up a page in terms of semantics
The process of markup can be segmented into multiple stages, each varying in levels of detail.

1. Large semantic blocks on each page of the site. Tags: &lt;header>, &lt;main>, &lt;footer>.
2. Large semantic sections in blocks. Tags: &lt;nav>, &lt;section>, &lt;article>, &lt;aside>.
3. The title of the entire document and the titles of semantic sections. Tags: &lt;h1>-&lt;h6>.
4. Small elements in semantic sections. Lists, tables, demos, paragraphs and hyphens, forms, quotes, contact information and progress.
5. Phrase elements. Images, links, buttons, videos, time and small text elements.
	
![What is Semantic HTML?](https://cdn.sanity.io/images/1e18e3je/production/6237cba3b05a35a9bf15172541c44479cddfe50c-893x827.jpg)

## Unsure which tags to use
There are simple rules for choosing the right tags:

* It turned out to find the most appropriate semantic tag - use it.
* For stream containers, &lt;div>.
* For small phrasal elements (word or phrase) — &lt;span>.
	
Rule for defining[ &lt;article>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/article), [&lt;section>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section) and [&lt;div>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div):

1. Can you give a name to the section and move this section to another site? - &lt;article>
2. Can you give a name to a section, but can't post it to another site? - &lt;section>
3. Can't give a name? It turns out something like "news and photo gallery" or "right column"? - &lt;div>

## How to avoid making mistakes
Do not use semantic tags for aesthetics. CSS is for styling.

It may seem that some tags are suitable for making a page prettier, moving text around, or adding spacing to it. But just because the default browser renders tags the way you want doesn't mean you should use it. Let's look at an example.

![No semantic example](https://cdn.sanity.io/images/1e18e3je/production/b5dea99350bbaa0d62aad33f0c2f9cd51465714e-845x267.jpg)

There are several errors here:

1. The [&lt;blockquote>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/blockquote) tag should be used to highlight quotations in the text, not just random selection of text. Coincidentally, in browsers this block is highlighted by default, but this does not mean that you need to use it in this way.
2. The [&lt;ul>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ul) tag is also used to visually "shift" the text. This is incorrect, because this tag should only be used to denote lists, and secondly, only [&lt;li>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/li) tags can be nested in the &lt;ul> tag and nothing else.
3. The [&lt;p>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/p) tag is used to visually expand the text. In fact, this tag is used to highlight paragraphs.

And any selection, shift or other text transformations can be done using CSS.
So use semantic tags as intended.

![Semantic example](https://cdn.sanity.io/images/1e18e3je/production/8185097aef699c08993a102f5990f63cbf045bdb-845x259.jpg)

This article was originally posted on [Dudych Blog](dudych.im)