Semantic HTML: Do you know about it?
Tags: webdev, beginners, html
Published: 2022-05-02 18:36:41

Hey all 😃, I hope you are doing well. Today, I want to talk about using Semantic HTML in your markup structure.

## Table of contents

1. [What is semantic HTML?](#introduction)
2. [Why should you use semantic HTML?](#why)
3. [Examples of semantic HTML usage](#examples)
4. [Summary](#summary)

### What is semantic HTML? <a name="introduction"></a>

If you are learning HTML now, chances are, you are already using semantic HTML. Semantic HTML refers to using meaningful tags for your markup elements which helps the browser, technologies and other developers understand your intentions and what information you are planning to put in that element. 

"Can you give some examples of what is a meaningful tag and which is not?" Of course, `<div>` and `<span>` are examples of HTML elements which has no meaning (non-semantic), it tells us nothing about what information would be rendered. However, `header`, `nav`, `main` are some of the examples of meaningful (semantic) HTML elements. When deciding what elements to use, you should consider "what information do I want to show with this element?" and choose the appropriate semantic element. There are many semantic elements, you can take a look at this [cheatsheet](https://learn-the-web.algonquindesign.ca/topics/html-semantics-cheat-sheet/) for a quick overview of the meaning behind the design of each semantic tags. 

![semantic HTML meme](https://i.redd.it/z7ajuu7504l71.png)
> credit: meme taken from [reddit post](https://www.reddit.com/r/ProgrammerHumor/comments/pgkaf5/semantic_html_conveys_meaning/)

It might not be easy at first but it is important to try to utilize these semantic elements as much as possible, this will help avoid the issue of too many nested `<div>`s and bring meaning to your web pages. Furthermore, some semantic elements have certain styles natively applied to it, you should use them for their meaning and not for their styles. Remember that your HTML tags are used for structure and you should use CSS for presentation and styling.


### Why should you use semantic HTML? <a name="why"></a>

There are many reasons why you should use semantic HTML, for example, **SEO** (Search Engine Optimisation). You can influence your website's search ranking by using semantic HTML (amongst other things). Another, equally important reason, is for **Accessibility**. Remember that, there are people who require assistive technologies like screen readers when accessing the web so using semantic HTML will help the screen reader navigate through your website, find and deliver your content. 

It will also make your code more readable and can help make debugging easier.

### Examples of semantic HTML usage <a name="examples"></a>

Here is a small example of how you can use semantic HTML. I started with a `<header>` tag as it is typically for introduction of content or grouping of navigations, inside it, I have a `nav` tag which contains `<a>` tags, these are link elements that will *navigate* users away following the link. Next, the `<main>` tag signifies this is the primary content of the page and inside I made use of a `<figure>` tag to show an image and it's caption. Lastly, the `<footer>` tag describe the last bit of information of a section or contains information about copyright, etc.

```html
<header>
  <nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
    <a href="/contact">Contact</a>
  </nav>
</header>
<main>
  <figure>
    <img src="https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/c36b1dbf-4dbb-46d1-8ac7-79313cfceb24/darsc3m-4fab6737-6a16-48b0-8710-c16bc4181031.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcL2MzNmIxZGJmLTRkYmItNDZkMS04YWM3LTc5MzEzY2ZjZWIyNFwvZGFyc2MzbS00ZmFiNjczNy02YTE2LTQ4YjAtODcxMC1jMTZiYzQxODEwMzEucG5nIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0._kp8Wp6uidemvzyG5yYsV_Tp8VZm-WYvv6DNQfJhuPo" alt="Naturo anime character, fourth hokage">
    <figcaption>Fig 1. The Yellow Flash of the Hidden Leaf, the Fourth Hokage.</figcaption>
  </figure>
</main>
<footer>copy right</footer>
```

Just by looking at the code, you have an idea of what would be populated on the page. After some simple styling, it looks something like this: 
![Semantic HTML example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8np55pc6mit3zqph7uj.png)

#### Real-world example 

I found a real-world example of a website using semantic HTML and that is: [A11Y](https://www.a11yproject.com/), which is the website for the Accessibility Project, definitely check it out to learn more about accessibility.

You can see in the image below how they have used semantic HTML to structure their homepage but they have used semantic HTML throughout the website, and if you view code in the footer, you will be able to see a navigation bar within a `nav` element.

![A11Y HTML code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbq0v8b6n6700t3pzg6y.png)
 
### Summary <a name="summary"></a>

In summary, semantic HTML is the practice of using meaningful HTML elements that describe or convey what information is to be displayed. It allows for better SEO to improve the website ranking, increase accessibility of the website, and provides a clearer more readable code for computers and humans. As you move forward with your learning journey, remember to think about semantic HTML when writing your code.

If you want to learn more, here are some additional resources:
- [W3School](https://www.w3schools.com/html/html5_semantic_elements.asp)
- [codecademy course](https://www.codecademy.com/learn/learn-html/modules/learn-semantic-html)
- [Google Chrome Developers video](https://www.youtube.com/watch?v=arMgwKY52Bs&ab_channel=GoogleChromeDevelopers)

Thank you for reading and as always, please leave a comment if you found this helpful, or would like to provide feedback 😃