Discover how simple tags can make your website friendlier for everyone, including search engines and users with disabilities!
Semantic vs non-semantic elements in HTML - When to Use Which
Imagine building a webpage by using only generic containers like <div> and <span> for everything, from headers to footers and articles.
Without clear meaning, it's hard for browsers, search engines, and assistive tools to understand the page structure. This makes navigation confusing and accessibility poor.
Semantic elements like <header>, <nav>, <article>, and <footer> give clear meaning to parts of the page, helping browsers and users understand the content better.
<div class="header">Welcome</div> <div class="nav">Menu</div>
<header>Welcome</header> <nav>Menu</nav>
Using semantic elements makes your webpage easier to navigate, more accessible, and better understood by search engines.
Screen readers can quickly jump between sections like navigation and main content when semantic tags are used, improving the experience for visually impaired users.
Semantic elements describe the purpose of content clearly.
They improve accessibility and SEO.
They make your code easier to read and maintain.
Practice
Solution
Step 1: Understand semantic elements
Semantic elements clearly describe their meaning, like<article>which represents an independent piece of content.Step 2: Identify non-semantic elements
<div>and<span>are generic containers without meaning.<b>is a presentational element without semantic meaning.Final Answer:
<article> -> Option AQuick Check:
Semantic element = <article> [OK]
- Confusing <div> as semantic
- Thinking <span> is semantic
- Thinking <b> is semantic
Solution
Step 1: Recognize semantic header tag
<header>is the semantic tag for page or section headers.Step 2: Differentiate from
<head><head>contains metadata, not visible content.<heading>is not a valid HTML tag.Final Answer:
<header>Welcome</header> -> Option CQuick Check:
Semantic header = <header> [OK]
- Using <head> for visible content
- Writing invalid <heading> tag
- Confusing metadata with page content
<div><h1>Title</h1><p>Paragraph</p></div>
Choose the best description.
Solution
Step 1: Understand
<div>usage<div>is a non-semantic container that can hold any flow content including headings and paragraphs.Step 2: Check rendering of heading and paragraph
The<h1>will display as a large bold heading, and<p>as a paragraph below it, both visible inside the container.Final Answer:
A heading 'Title' and a paragraph 'Paragraph' inside a generic container -> Option BQuick Check:
Non-semantic container shows all content [OK]
- Thinking div restricts content types
- Assuming heading is hidden
- Believing invalid nesting error occurs
<section><div>Content</section></div>
Solution
Step 1: Check tag nesting order
The<div>opens inside<section>but closes after</section>, breaking proper nesting.Step 2: Understand correct nesting rules
Tags must close in reverse order of opening. Here,<div>should close before</section>.Final Answer:
The <div> tag is not closed before </section> -> Option DQuick Check:
Proper nesting = close inner tags first [OK]
- Ignoring tag closing order
- Thinking div can't be inside section
- Missing closing tags
<div><div>Main content</div><div>Footer info</div></div>
Solution
Step 1: Identify semantic tags for main content and footer
<main>is the semantic tag for main page content, and<footer>is for footer information.Step 2: Replace non-semantic
Changing the outer<div>with semantic tags<div>to<main>and the footer<div>to<footer>improves meaning and accessibility.Final Answer:
Replace outer <div> with <main> and inner footer <div> with <footer> -> Option AQuick Check:
Use <main> and <footer> for better semantics [OK]
- Using <article> instead of <main> for main content
- Replacing all divs with <section> unnecessarily
- Ignoring semantic improvements
Ready to prove it?
Pass the test and this pattern moves unknown
