Learn about semantic elements in HTML and how they enhance the structure and semantics of web pages.
Explore examples and best practices for using semantic elements effectively.
Semantic elements, also known as semantic HTML elements, are specific HTML tags that provide meaning and structure to the content within a web page. These elements are designed to describe the purpose or role of the content they enclose, rather than just dictating how the content should be presented visually.
Semantic elements play a crucial role in improving web accessibility, search engine optimization (SEO), and overall code maintainability. They enable developers and assistive technologies to better understand the structure and context of the content on a web page.
By using semantic elements, developers can provide meaningful information to assistive technologies, improve search engine rankings, and make it easier to maintain and style web pages.
Represents the introductory content or a container for the heading section of a page or a section.
Here’s a complete HTML code example showcasing the use of the <header> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Header</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <header> element is used to contain the introductory content of the web page, including the main heading and navigation links. The <h1> tag represents the main heading of the page, while the <nav> tag contains an unordered list (<ul>) of navigation links using <li> and <a> tags.
Note that the example also includes a <main> element to wrap the main content of the page and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Defines a navigation section containing navigation links.
<nav>:complete code example
Here’s a complete HTML code example showcasing the use of the <nav> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Navigation</title> </head> <body> <header> <h1>Welcome to My Website</h1> </header> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> <main> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <nav> element is used to contain the navigation section of the web page. The <ul> tag represents an unordered list, and each navigation item is defined using an <li> tag. The <a> tags within the list items represent the actual navigation links.
Note that the example also includes a <header> element for the introductory content of the page, a <main> element for the main content, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Indicates the main content of a document.
<main>: complete code example
Here’s a complete HTML code example showcasing the use of the <main> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Main Content</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> <h2>Our Services</h2> <ul> <li>Web Design</li> <li>Graphic Design</li> <li>SEO Optimization</li> </ul> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <main> element is used to contain the main content of the web page. It includes a heading <h2> for the “About Us” section and a paragraph <p> with some dummy text. Additionally, it includes another heading <h2> for the “Our Services” section, followed by an unordered list <ul> with three list items <li> representing different services.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Represents a self-contained composition, such as a blog post, article, or news story.
<article>:complete code example
Here’s a complete HTML code example showcasing the use of the <article> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Article</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <article> <h2>Blog Post Title</h2> <p>By John Doe | Published on May 30, 2023</p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </article> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <article> element is used to wrap a self-contained composition, such as a blog post. Within the <article> element, we have a <h2> tag for the blog post title, followed by two <p> tags. The first <p> tag displays information about the author and publication date, while the second <p> tag contains the content of the blog post.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Defines a standalone section within a document, often with its own heading.
<section>:complete code example
Here’s a complete HTML code example showcasing the use of the <section> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Sections</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </section> <section> <h2>Our Services</h2> <ul> <li>Web Design</li> <li>Graphic Design</li> <li>SEO Optimization</li> </ul> </section> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <section> element is used to define standalone sections within the <main> content of the web page. The first <section> includes a <h2> tag for the “About Us” section and a <p> tag with some dummy text. The second <section> includes a <h2> tag for the “Our Services” section, followed by an unordered list <ul> with three list items <li> representing different services.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Represents content that is tangentially related to the surrounding content, such as sidebars or pull quotes.
<aside>:complete code example
Here’s a complete HTML code example showcasing the use of the <aside> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Aside</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </section> <aside> <h3>Upcoming Events</h3> <ul> <li>Event 1: May 31, 2023</li> <li>Event 2: June 15, 2023</li> <li>Event 3: July 5, 2023</li> </ul> </aside> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <aside> element is used to represent content that is tangentially related to the surrounding content. It is placed within the <main> element. The <aside> contains an <h3> tag for the “Upcoming Events” section and an unordered list <ul> with three list items <li> representing the upcoming events.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Defines the footer section, typically containing information about the author, copyright notice, or related links.
<footer>:complete code example
Here’s a complete HTML code example showcasing the use of the <footer> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Footer</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </section> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <footer> element is used to define the footer section of the web page. The <footer> contains a <p> tag that displays the copyright notice and a text.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <main> element for the main content section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Used together to represent a media element (image, video, audio) and its caption.
<figure>:complete code example
Here’s a complete HTML code example showcasing the use of the <figure> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Figure</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </section> <figure> <img src="gogacademy.png" alt="A beautiful image"> <figcaption>This is a beautiful image.</figcaption> </figure> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <figure> element is used to encapsulate a self-contained media content, such as an image. It includes an <img> tag with the src attribute specifying the path to the image file and the alt attribute providing alternative text for the image. The <figcaption> tag is used within the <figure> element to provide a caption or description for the image.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Represents a specific date, time, or duration.
<time>:complete code example
Here’s a complete HTML code example showcasing the use of the <time> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Time</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </section> <section> <h2>Latest News</h2> <article> <h3>Article Title</h3> <time datetime="2023-05-30T08:30:00Z">May 30, 2023, 08:30 AM</time> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae.</p> </article> </section> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <time> element is used to represent a specific date and/or time. It is placed within an <article> element to denote the publication date of an article. The <time> element includes the datetime attribute to specify the date and time in a machine-readable format, and the content within the <time> element provides the human-readable date and time representation.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Used together to create collapsible content with a summary or title.
<details>:complete code example
Here’s a complete HTML code example showcasing the use of the <details> semantic element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Details</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </section> <section> <h2>Frequently Asked Questions</h2> <details> <summary>Question 1: What is Lorem Ipsum?</summary> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae.</p> </details> <details> <summary>Question 2: How can I contact you?</summary> <p>You can contact us by email or phone. Our email address is example@example.com, and our phone number is 123-456-7890.</p> </details> </section> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <details> element is used to create a disclosure widget that can be toggled open and closed. It is often used to present collapsible sections, such as frequently asked questions. The <summary> element is used as a heading for each question, and the content within the <details> element provides the answer to the question.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
is used as a heading for each question within the <details> element
uses with complete code example
Here’s a complete HTML code example showcasing the use of the <summary> element in conjunction with the <details> element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Summary</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> </section> <section> <h2>Frequently Asked Questions</h2> <details> <summary>Question 1: What is Lorem Ipsum?</summary> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae.</p> </details> <details> <summary>Question 2: How can I contact you?</summary> <p>You can contact us by email or phone. Our email address is example@example.com, and our phone number is 123-456-7890.</p> </details> </section> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <summary> element is used as a heading for each question within the <details> element. When the user clicks on the summary, the associated details content will be revealed or hidden. The content within the <details> element provides the answer or additional information for each question.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
is used to highlight a specific text within a paragraph
Here’s a complete HTML code example showcasing the use of the <mark> element:
<!DOCTYPE html> <html> <head> <title>Website with Semantic Mark</title> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <h2>Highlighted Text</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis condimentum est, in lobortis justo maximus vitae. Sed tincidunt fermentum nibh, sit amet tempor enim fermentum ac. Ut fermentum mi id diam pulvinar, in aliquam lorem volutpat. Duis auctor augue id felis pharetra, vitae auctor ipsum volutpat. Integer malesuada nunc a aliquet interdum.</p> <p>In this paragraph, we want to <mark>highlight</mark> a specific text.</p> </section> </main> <footer> <p>© 2023 My Website. All rights reserved.</p> </footer> </body> </html>
In this example, the <mark> element is used to highlight a specific text within a paragraph. The content within the <mark> tags will be visually marked or highlighted, typically using a yellow background color by default.
Note that the example also includes a <header> element for the introductory content of the page, a <nav> element for the navigation section, and a <footer> element for the footer section. These additional semantic elements further enhance the structure and semantics of the HTML code.
Here’s a multiple-choice quiz based on the lesson content about semantic elements. The correct answers are marked with an asterisk (*):
1-Which HTML element is used to define the main content of a web page?
a) <section>
b) <main>*
c) <article>
d) <body>
2-What is the purpose of using semantic elements in HTML?
a) To improve the visual appearance of the webpage.
b) To enhance the accessibility and structure of the webpage.*
c) To increase the page loading speed.
d) To add interactive functionality to the webpage.
3-Which element is used to represent a self-contained media content, such as an image?
a) <image>
b) <picture>
c) <figure>*
d) <media>
4-Which element is used to represent a section of content that can be optionally toggled open or closed?
a) <collapse>
b) <section>
c) <details>
d) <toggle>
5-What is the purpose of the <mark> element in HTML?
a) To create a bookmark within the webpage.
b) To highlight important or relevant text within a paragraph.*
c) To insert a horizontal rule on the webpage.
d) To create a link to another webpage.
6-Which HTML element is used to represent a header or a heading section?
a) <header>
b) <h1>
c) <nav>
d) <h2>
7-What is the purpose of the <nav> element in HTML?
a) To display a navigation menu on the webpage.*
b) To style the background color of a section.
c) To include a video or audio content on the webpage.
d) To mark a specific text as important.
8-Which HTML element is used to represent a standalone piece of content, such as a blog post or news article?
a) <main>
b) <aside>
c) <article>*
d) <section>
9-What is the purpose of the <footer> element in HTML?
a) To create a fixed position footer at the bottom of the webpage.
b) To include a copyright statement or credits for the webpage content.*
c) To mark a specific text as deleted or removed.
d) To insert an image or visual content on the webpage.
10-Which HTML element is used to represent a related content section that is tangentially related to the main content?
a) <related>
b) <aside>*
c) <section>
d) <div>
11-Which HTML element is used to group together related form elements?
a) <group>
b) <fieldset>*
c) <formset>
d) <container>
12-What is the purpose of the <time> element in HTML?
a) To display the current time on the webpage.
b) To specify the duration of a video or audio content.
c) To represent a specific date and/or time.*
d) To define a countdown timer.
13-Which HTML element is used to represent a block of quoted content from another source?
a) <quote>
b) <blockquote>*
c) <cite>
d) <q>
14-What is the purpose of the <figure> element in HTML?
a) To group together related sections of content.
b) To represent a caption for an image or multimedia content.
c) To create a visual separator or divider on the webpage.
d) To represent self-contained content, such as images, diagrams, or code snippets.*
15-Which HTML element is used to define a short caption for a table?
a) <table>
b) <caption>*
c) <summary>
d) <header>
16-Which HTML element is used to define a section of navigation links?
a) <nav>
b) <menu>
c) <navbar>
d) <ul>
17-What is the purpose of the <aside> element in HTML?
a) To mark a section of content as a sidebar.
b) To include additional information or content that is related but separate from the main content.*
c) To specify a section of content as a footnote.
d) To create a blockquote for quoted content.
18-Which HTML element is used to represent a standalone, independent piece of content?
a) <main>
b) <section>
c) <article>
d) <div>
19-What is the purpose of the <header> element in HTML?
a) To define a header for a specific section or block of content.
b) To include a logo or branding information for the webpage.
c) To mark a specific text as a heading.
d) To represent introductory content or a container for the content at the top of a webpage.*
20-Which HTML element is used to mark up a piece of content that is particularly important or emphasized?
a) <em>
b) <strong>
c) <highlight>
d) <b>
21-Which HTML element is used to represent a list of items with no specific order?
a) <ol>
b) <ul>*
c) <li>
d) <dl>
22-What is the purpose of the <section> element in HTML?
a) To group together related form elements.
b) To mark a section of content as a sidebar.
c) To represent a self-contained thematic grouping of content.*
d) To create a clickable link to another webpage.
23-Which HTML element is used to mark a specific span of text as a definition of a term?
a) <dfn>
b) <dl>
c) <dt>
d) <dd>