A short guide to help you pick the correct HTML tag
A semantically correct HTML tag informs the browser and the developer about its content. It also helps keyboards and assistive technologies to navigate through a website.
There are more than 100 HTML tags, but which ones are semantic and which are not?
Non-semantic elements: div and span, etc. Unlike semantic elements, these are not holding any extra information about their content.
Semantic elements: header, main, footer, article, img, p, h1, etc.
When you create a new HTML file, you have something like this:
<html> <head> ... </head> <body> ... </body> </html>
I will focus on what's inside the body element and name only the most important tags. You can find a link with a full list of HTML elements at the end of this article.
The HTML tags can be separated into different groups and it helped me to understand the whole concept and I think this is the best way to explain it, so let's start.
Content sectioning or landmarks
The layout of the main page of this blog looks like this:
header and nav
<header>, you can find the logo, which is a link and it leads to the main page and
<nav> element which contains the links to my other pages.
You can also put here some introduction about the page, a search bar, or other elements.
<main> element holds different content on each page. This holds the primary information of the current page or the main functionality of an application.
<footer>tag creates a footer for a section or for the whole document. You can have multiple
<footer> elements in one page, which typically contains data, like: contact info to the author, copyrigt information, related links etc.
You can also find the
<aside> on the right side of my page. It holds complementary content. If I remove it, nothing will be missing from the primary content. You can put here for example table of contents, related posts, etc.
<main> element, you can find the
<section> tag. If there isn't a more specific HTML element for a part of your document, you can group related elements with this tag. This element should always have a heading.
h1 - h6
<h6>, are the titles of the different sections.
<h1> is the highest level of the headings, and it is necessary to have one on each page but there should be only one per page.
<h6> is the lowest. If you run out of headings, there is another option: the 7th heading
The reason you should use landmarks is that screen readers and other assistive technologies can jump back and forth between these elements. For example, all the pages have the same header with the same logo, the same navigation, but with a keyboard shortcut, you can jump over them and start with the content of the main tag.
Text content elements
These can help you organize the written content on your pages and give information about the content to search engines and screen readers.
<p> stands for paragraph. In other words, a block of text. You will use it a lot for wrapping your text content with it.
ul, ol, li
<ul> - unordered list,
<ol> - ordered list,
<li> - list elements of the two kind of lists. If you need to create a list you have to use them.
List items are wrapped in
<ul>, if the order is not important. It will put bullet points before each list item.
<ol>, if the order of your list items is important. By default, it will put a number before each item in ascending order.
Figure, figcaption, blockquote, cite
I will explain all of them with one example. Look at this block of code:
<figure> <blockquote cite="https://www.htmhell.dev/24-a-placeholder-is-not-a-label/"> Every form input element needs a label. </blockquote> <figcaption>Manuel Matuzović, <cite>A placeholder is not a label - HTMHell</cite></figcaption> </figure>
<figcaption> go hand in hand
<figure> is a self-contained element optionally, but usually with a caption. This caption is the
<blockquote> is for quotations, where you want to indicate the author, the source, etc. (You can use the cite attribute on this tag to give the URL of the source of this quotation).
<cite> element is holding the reference to a quoted content. Name of the author, title of the cited material, etc. and this is the first element of our next group, the inline text elements.
Inline text elements
You can wrap smaller parts of text like words, sentences, etc into different tags to define its style, meaning, or structure.
<a> or anchor elements are links, which have a href attribute, which leads to other web pages, files, email addresses, phone numbers, or a location on the same page.
Use this element to emphasize any text content. Screen readers read this part of a text in a different way. Browsers render the content of this text in italic font style but don't use it only for styling.
Use this element to mark content, which is more important than the rest of the text. Screen readers read this part of a text in a different way. Browsers render the content of this text in bold font style but don't use it only for styling.
A short inline quotation, which doesn't need an entire paragraph. (You can use the cite attribute on this tag to give the URL of the source of this quotation)
Image and multimedia
audio is used to embed sound into your website.
img is used to embed an image into your website.
video is used to embed a video into your website.
I think an example is the easiest way to explain these elements:
<table> creates a two-dimensional table with rows and columns
<thead> is a row or rows that define the column heading
<th> is a single cell as a header of a group of cells. This group of cells can be a column or a row, and you have to define it by the scope attribute. The scope attribute can have the value of col or row.
<tbody> wraps the main content of our table, which is usually between the thead and the tfoot elements.
<tr> defines a row of cells in a table.
<td> stands for table data. This is a single cell that holds the information.
This is a row at the end of the table, that summarizes its content.
<form> element wraps the entire form that users can use to enter their data and submit it to the server.
<fieldset> groups related inputs in a form. Fieldsets need a caption and this caption is the legend element. For example, if you have a form with a shipping address and billing address, you can create two groups of inputs with the fieldset element.
<legend> element defines a caption for the fieldset.
Users can interact with
<input> elements to enter their data. There are many different kinds of input fields for different types of information, like text, color, file, etc.
<input> field needs a caption and this caption is the
<label> element. You can't replace the
<label> tag with the placeholder attribute.
select and option
<select> is a special kind of input field, with a dropdown menu. This dropdown menu contains
Users can enter multiple lines of text in this field
<button> tag creates a clickable element, which looks like a button. It is used to submit forms.
You can embed other HTML files into your website with the
<iframe> element. For example, you can insert a song from Spotify or a video from YouTube into your website.
<picture> has almost the same purpose as the
<img> element, but you can specify more sources and browsers decide, which is the best for them. If you want to use a new image format, like .avif, (which is not yet widely supported at the time I write this post), you should give the image in another format, like png with the
With this element you can specify multiple sources for the
<video> elements, like this:
<picture> <source srcset="image.png"> <source srcset="image.webp"> <img src="image.avif" alt="" /> </picture>
I could describe it in a more complicated way, but
<svg> is mostly used to display a vector graphic image, that you can enlarge or reduce to any size without becoming pixelated.
Learn by playingPaul Foster built this awesome memory game. Test yourself and beat your score tomorrow.
If you pick the correct elements, your code will be easier to read by other developers,
it will be easier for assistive technology users to use your website,
the search engines will rank your website higher (SEO).
Sometimes you should stop and think about whether there is a better HTML element than the one you want to use.
If you are missing something or find any inaccuracies, please let me know and I will add or correct it as soon as I can.
You can read more about this topic here:
- Element diversity - Manuel Matuzović
- Semantics | MDN Web Docs
- HTML elements reference | MDN Web Docs
- HTML: A good basis for accessibility | MDN Web Docs
- HTML Semantic Elements - W3Schools Online Web Tutorials