Semantic elements in HTML are those that clearly describe their meaning in a human- and machine-readable way. These elements not only help to structure your HTML documents but also improve accessibility and SEO.
Semantic elements provide several benefits:
Element | Description | Example |
---|---|---|
<header> | Represents a container for introductory content or a set of navigational links. |
Try yourself
|
<nav> | Defines a set of navigation links. |
Try yourself
|
<main> | Specifies the main content of a document, which should be unique and not include repeated content like sidebars, navigation, etc. |
Try yourself
|
<article> | Represents a self-contained piece of content that could be independently distributed or reused, such as a blog post or news article. |
Try yourself
|
<section> | Defines a section in a document, such as a chapter or part of a website. |
Try yourself
|
<footer> | Represents the footer for its nearest sectioning content or sectioning root element, typically containing metadata, links, and copyright information. |
Try yourself
|
<aside> | Defines content that is tangentially related to the content around it, often used for sidebars, pull quotes, or advertisements. |
Try yourself
|
<figure> | Represents self-contained content, potentially with a caption, and is typically used for images, illustrations, diagrams, etc. |
Try yourself
|
<figcaption> | Defines a caption for a <figure> element. |
Try yourself
|
Here is an example of a simple HTML document using semantic elements:
<header>
<h1>Welcome to Our Website</h1>
<nav>
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</nav>
</header>
<main>
<article>
<h2>Article Title</h2>
<p>This is the content of the article.</p>
</article>
<section>
<h2>About Us</h2>
<p>Information about our website.</p>
</section>
<aside>
<p>Related content or advertisements.</p>
</aside>
</main>
<footer>
<p>© 2023 Lets Coding. All rights reserved.</p>
</footer>
This example includes a header, navigation, main content, an article, a section, an aside, and a footer, demonstrating how to structure a typical webpage using semantic elements.
Here are some important notes and best practices when using semantic elements: