OpenReplay
Navigate back to the homepage
BLOG
Browse Repo
Back

Semantic elements in HTML: why use them

Emeka Henry Nnamani
August 25th, 2022 · 6 min read

Semantic Elements are paramount because they make our code easy to read, accessible, and equally lead to a more consistent code; hence we should use Semantic Elements so that screen readers can easily interpret our web pages. Furthermore, the semantic HTML5 element entails describing our code in a human and machine-readable way to make it look nicer. Therefore, to have a better user experience, you need to use the Semantic HTML5 element to get the best experience.

HTML was initially created as a markup language to describe documents, but as the internet grew, people wanted to make the web look nicer; hence people started to use “non-semantic” tags like div and span, but as a programmer, you need Semantic Elements to be able to write clean code in a manner that it can describe its meaning to both the Browser and the Developer. In this article, we’ll describe the available semantic HTML elements and explain their advantages.

What are “Semantic Elements”?

“Semantics” as a word relates to meanings, to studying meaning in language, and it also means the study of the meanings of words and sentences. We think of semantics as the magic that usually occurs when people communicate and understand each other fluently. Semantics simply means “Meaning”.

Semantic Elements are those elements that have some meaning. In programming, the word “Semantics” have to do with the meaning of the code. Semantic HTML elements have to do with the syntax that makes the HTML very easy to understand, thereby introducing good sense to the web pages rather than just presentations. Semantic Elements naturally represent their meaning to both the Developer and the Browser to the extent that it makes it easy for screen readers used by those with impaired vision to understand the code easily.

Semantic elements have advantages:

  • Easy to Understand Code: Semantics elements make it easy for developers and screen readers to understand the code.
  • Structuring the code we create: It helps us to structure the code, thereby making our code readable and easy to maintain.
  • Cleaner Code: Semantic Elements make our code cleaner and make more sense where each element uses the appropriate word.
  • Meaningful Code: It makes our code have meaning, bringing understanding to both the Browser and the Developer.
  • Organized Code: It organizes our code better for people to understand.
  • Consistent Code: Semantic elements leads to having a more consistent code

We should use Semantic HTML Elements because it makes our code much easier to read, and even a non-specialist can easily read and understand the code. Likewise, your code will be readily understood as a programmer if you use Semantic HTML Elements. Finally, using Semantic Elements will make it easy for more people to understand your code, especially for screen readers with a sight impairment.

Examples of semantic elements

The Semantic Elements in HTML5 are as follows:

  • article: The Article Content Element contains information that we will use in a document, and a given document can have multiple pieces on it; a good example is a blog that displays the text of each article one after the other as the reader scrolls through, each of the posts would be contained in an article element, probably with one or more section elements within.
  • aside: This represents that part of a document whose content is indirectly related to the main content. It does not render anything special to the Browser, but we can use CSS to improve the aside element.
  • details: This specifies the additional information the programmer can open and close when needed, and we can put any content inside the detail tag.
  • figure: It’s an image, diagram, code, etc., that is referenced in the main document and generally associated with a caption element called figcaption.
  • figcaption: We use the figcaption to set a description of the content of its parent figure element.
  • footer: It is an HTML5 element that we use to define the footer that contains the author information in the HTML document. This is found within the body tag and can apply to article, section, body, etc. If you are using a footer to write the address of the author, then it must be enclosed with the address tag.
  • header: The header HTML element defines a section containing information about the related content’s title and heading. It can contain some heading elements, a logo, a search form, an author name, etc.
  • section: This semantic Element provides adequate meaning to user agents and humans about the enclosed content.
  • main: This HTML element defines the main content in the document, and we should only have one main Element in the entire document. This main element is found within the body tag.
  • mark: We use the mark element to highlight important text within the HTML page.
  • nav: The nav Element makes it immensely more accessible for us to create a neat menu of text links and help screen readers correctly identify the primary navigation areas in a document.
  • time: This Element represents time or date. In addition, this element allows the user agent to add occasional events such as birthdays and other anniversaries.
  • summary: The summary element provides a concise description of the content of the details element, and this is displayed when the details element is toggled closed.

The advantages of using the new HTML semantic elements are several:

  • Help the code to have meaning: The code will be meaningful once you use the new HTML Semantic elements because it brings meaning to vague HTML files.
  • Increase accessibility: The new HTML Semantic elements allow our web pages to be accessed quickly, helping those with low vision navigate quickly easy to access.
  • It helps those with impaired vision to understand the code: Using the new HTML Semantic elements’ significant advantage is that it helps even those with impaired vision to understand the code.
  • Easier to maintain: Once we use the new HTML Semantic element, it will make our code easy to maintain, helping new developers and tech enthusiasts understand.
  • Help people to understand the code quickly: People tend to understand it quickly because it’s written in human and machine-readable ways.
  • Fast interpretation: The new HTML semantic element helps screen readers interpret the code quickly.
  • Organized code: Our code will be organized once we use the new HTML semantic elements. They help keep our lines of code very organized.

Open Source Session Replay

OpenReplay is an open-source, session replay suite that lets you see what users do on your web app, helping you troubleshoot issues faster. OpenReplay is self-hosted for full control over your data.

replayer.png

Start enjoying your debugging experience - start using OpenReplay for free.

Layout example of a semantic web page

The layout example of a Semantic web page is thus:

1<html lang="en">
2 <head>
3 <meta charset="UTF-8" />
4 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
5 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6 <title>Non Semantic Element</title>
7 <link rel="stylesheet" href="styles.css" />
8 </head>
9 <body>
10 <!-- navigation -->
11 <div>
12 <ul>
13 <li><a href="#">Home</a></li>
14 <li></li>
15 <li><a href="#">About</a></li>
16 <li></li>
17 <li><a href="#">Products</a></li>
18 <li></li>
19 <li><a href="#">Contact</a></li>
20 <li></li>
21 </ul>
22 </div>
23
24 <!-- header-->
25 <div>
26 <h1>Tom's Fruit Co.</h1>
27 <img src="https://bit.ly/2KYbBR1" alt="main banner" />
28 <button>Find out more</button>
29 </div>
30
31 <!-- about section -->
32 <div>
33 <h2>About Us</h2>
34 <p>
35 Life in flower goes on when a male meet a female to protect itself.
36 While growing up to be a man! I equally found out that life in human
37 goes on when a male meet a female to protect itself also.
38 </p>
39 </div>
40
41 <!-- products section-->
42 <div>
43 <h3>Our Products</h3>
44 <ul>
45 <li>Apples</li>
46 <li>Oranges</li>
47 <li>Grapes</li>
48 <li>Limes</li>
49 </ul>
50 </div>
51
52 <!-- contact -->
53 <div>
54 <h3>Get in Touch!</h3>
55 <form>
56 <label>Name:</label>
57 <input type="text" />
58 <label>Email:</label>
59 <input type="email" />
60 <label>Message:</label>
61 <textarea></textarea>
62 <input type="submit" value="Submit" />
63 </form>
64 </div>
65
66 <!-- footer -->
67 <div>
68 <p>Copyright &copy; 2022</p>
69 </div>
70 </body>
71</html>

The above Toggle code block shows how the document will look without Semantic Elements. I used CSS to make this HTML document look better. I will equally drop a Screenshot of how it will appear on the Browser, say Google Chrome, and here are the screenshots:

1

2

The above screenshots show how the document will look on a Browser. I will now apply Semantic Elements to it to make it readable and more meaningful. I will start from the top of the Document and keep adding the required Semantic Element till down; we will notice as the document becomes more meaningful as I add the Semantic Elements. I will start to make the Document semantic starting from the top page to down thus:

  • Adding the nav semantic element to the document: In the section for Navigation, I will replace the opening div with a semantic nav element. The nav section helps screen readers quickly identify the primary navigation area.
1<nav>
2 <ul>
3 <li><a href="#">Home</a></li>
4 <li></li>
5 <li><a href="#">About</a></li>
6 <li></li>
7 <li><a href="#">Products</a></li>
8 <li></li>
9 <li><a href="#">Contact</a></li>
10 <li></li>
11 </ul>
12</nav>

3

  • Adding the header semantic element to the document: In the section for Header, I will replace the opening div and closing /div with the header semantic Element.
1<header>
2 <h1>Tom's Fruit Co.</h1>
3 <img src="https://bit.ly/2KYbBR1" alt="main banner" />
4 <button>Find out more</button>
5</header>

4

  • Adding the main, article, and aside semantic elements to the document, to replace several div elements.
1<main>
2 <!-- about section -->
3 <article>
4 <h2>About Us</h2>
5 <p>
6 Life in flower goes on when a male meet a female to protect itself. While
7 growing up to be a man! I equally found out that life in human, goes on
8 when a male meet a female to protect itself also.
9 </p>
10 </article>
11
12 <!-- products section-->
13 <aside>
14 <h3>Our Products</h3>
15 <ul>
16 <li>Apples</li>
17 <li>Oranges</li>
18 <li>Grapes</li>
19 <li>Limes</li>
20 </ul>
21 </aside>
22</main>

The main element defines the main content, and the article element displays the information about the content. In contrast, the aside element makes the content indirectly related to the main content readable. The use of the three semantic elements gave the Document on the Chrome Browser meaning, making the “About Us” and “Our Products” readable.

5

  • Adding the section semantic element to the document, in the contact part of our code, to replace another div.
1<section>
2 <h3>Get in Touch!</h3>
3 <form>
4 <label>Name:</label>
5 <input type="text" />
6 <label>Email:</label>
7 <input type="email" />
8 <label>Message:</label>
9 <textarea></textarea>
10 <input type="submit" value="Submit" />
11 </form>
12</section>

6

  • In the footer part of our code, we will replace the div with the footer semantic element.
1<footer>
2 <p>Copyright & copy; 2022</p>
3</footer>

7

A summary of differences

This table summarizes the differences -and advantages- of semantic elements.

SEMANTIC MARKUPNON SEMANTIC MARKUP
1. They have meaning.They don’t have meaning.
2. Those with impaired vision can easily read the Semantic markup.Those with impaired vision can not easily read non Semantic.
3. Semantic markup makes use of elements like header, nav, section, article, main etc.Non-Semantic Markup makes use of div, span etc.
4. Non-developers can easily understand semantic markup.Non-developers can not easily understand non Semantic markup.
5. The content within a Semantic Markup describes how the content within them is supposed to behave.The content can be anything.
6. The code is very well organized.The code is not well organized.
7. Semantic Markup has specific attributes for its structure.Non-Semantic Markup has just a “class” attribute that can be used for the structure.
8. Semantic markup elements are used to add meaning to non Semantic markup.Non Semantic markup can only have meaning with the aid of Semantic markup element.

Conclusion

As a 21st-century programmer, you must use the Semantic HTML5 elements to make your code meaningful, accessible, readable, organized, consistent, and perfect. Thanks a lot for reaching the end of my article on “The reasons why you should use Semantic HTML5 Elements”.

A TIP FROM THE EDITOR: For more on how web pages are rendered, take a look at our How Browser Rendering Works and Why You Should Care article.

newsletter

More articles from OpenReplay Blog

Testing tools: a Classification

An overview of testing tools for many purposes

August 24th, 2022 · 11 min read

Translate Subtitles using the LibreTranslate API

Building a full subtitle translator in React

August 24th, 2022 · 6 min read
© 2022 OpenReplay Blog
Link to $https://twitter.com/OpenReplayHQLink to $https://github.com/openreplay/openreplayLink to $https://www.linkedin.com/company/18257552