Html Semantics
By chukwulete ekene front-end.
What are Semantic Elements?
HTML was originally created as a markup language to describe documents on the early internet. As the internet grew and was adopted by more people, its needs changed.
Where the internet was originally intended for sharing scientific documents, now people wanted to share other things as well. Very quickly, people started wanting to make the web look nicer.
As the use of visually designed layouts progressed, programmers started to use a generic “non-semantic” tag like <div>
. They would often give these elements a class
or id
attribute to describe their purpose. For example, instead of <header>
this was often written as <div class="header">
.
As HTML5 is still relatively new, this use of non-semantic elements is still very common on websites today.
List of new semantic elements
The semantic elements added in HTML5 are:
<article>
<aside>
<details>
<figcaption>
<figure>
<footer>
<header>
<main>
<mark>
<nav>
<section>
<summary>
<time>
Elements such as <header>
, <nav>
, <section>
, <article>
, <aside>
, and <footer>
act more or less like <div>
elements. They group other elements together into page sections. However where a <div>
tag could contain any type of information, it is easy to identify what sort of information would go in a semantic <header>
region.
Why use semantic elements?
To look at the benefits of semantic elements, here are two pieces of HTML code. This first block of code uses semantic elements:
Whilst this second block of code uses non-semantic elements:
First, it is much easier to read. This is probably the first thing you will notice when looking at the first block of code using semantic elements. This is a small example, but as a programmer you can be reading through hundreds or thousands of lines of code. The easier it is to read and understand that code, the easier it makes your job.
It has greater accessibility. You are not the only one that finds semantic elements easier to understand. Search engines and assistive technologies (like screen readers for users with a sight impairment) are also able to better understand the context and content of your website, meaning a better experience for your users.
HTML5, HTML5 provided us with <section>
and <article>
in a way to replace <div>
.
The <section>
and <article>
elements are conceptually similar and interchangeable. To decide which of these you should choose, take note of the following:
An article is intended to be independently distributable or reusable.
A section is a thematic grouping of content.
<header>
and <hgroup>
The <header>
element is generally found at the top of a document, a section, or an article and usually contains the main heading and some navigation and search tools.
The <hgroup>
element should be used where you want a main heading with one or more subheadings.
REMEMBER, that the <header>
element can contain any content, but the <hgroup>
element can only contain other headers, that is <h1>
to <h6>
and including <hgroup>
.
<aside>
The <aside>
element is intended for content that is not part of the flow of the text in which it appears, however still related in some way. This of <aside>
as a sidebar to your main content.
Before HTML5, our menus were created with <ul>
’s and <li>
’s. Now, together with these, we can separate our menu items with a <nav>
, for navigation between your pages. You can have any number of <nav>
elements on a page, for example, its common to have global navigation across the top (in the <header>
) and local navigation in a sidebar (in an <aside>
element).
<footer>
If there is a <header>
there must be a <footer>
. A <footer>
is generally found at the bottom of a document, a section, or an article. Just like the <header>
the content is generally metainformation, such as author details, legal information, and/or links to related information. It is also valid to include <section>
elements within a footer.
<figure>
and <figcaption>
<figure>
is for wrapping your image content around it, and <figcaption>
is to caption your image.