← Back to HTML & CSS, page 2

html-css-0023 · Easy fresher

What is semantic HTML, and why use header, nav, main and footer instead of div?

Semantic HTML uses elements that describe meaning, such as header, nav, main, article, aside and footer, instead of generic divs. It gives screen reader users landmarks to jump between, helps search engines and tools understand structure, and keeps code readable. Native elements like button also bring keyboard behaviour for free.

Full answer

Semantic HTML means choosing the element that describes the meaning of the content, not only how it looks. A nav says "this is a group of navigation links". A div says nothing.

The common structural elements are:

  • header: introductory content for the page or a section, such as a logo and title.
  • nav: a main block of navigation links.
  • main: the central content of the page. Use only one visible main per page.
  • article: a self-contained piece, such as a blog post or a news card, that makes sense on its own.
  • section: a themed group of content, normally with a heading.
  • aside: related content beside the main flow, like a sidebar.
  • footer: closing content such as copyright and contact links.
  • figure and figcaption: an illustration with its caption.

Why it matters. Screen readers list these as landmarks, so a user can jump straight to the navigation or the main content. Search engines and other tools understand the page structure better. Developers read the code faster. Some elements also come with free behaviour: button is focusable and works with Enter and Space, while a div with a click handler does neither.

In the accessibility tree this page exposes banner, navigation, main, complementary and contentinfo landmarks. Use div and span when no element fits, for example a wrapper that exists only for styling.

html
<header>
  <h1>Greenfield College</h1>
  <nav aria-label="Main">
    <ul><li><a href="/courses">Courses</a></li><li><a href="/fees">Fees</a></li></ul>
  </nav>
</header>
<main>
  <article>
    <h2>Admissions open for 2027</h2>
    <p>Applications close on <time datetime="2027-03-31">31 March 2027</time>.</p>
    <figure>
      <img src="campus.jpg" width="600" height="300" alt="Main building of the college">
      <figcaption>The main building</figcaption>
    </figure>
  </article>
  <aside aria-label="Related">Scholarship details</aside>
</main>
<footer><p>Contact: office@greenfield.example</p></footer>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Use element names that describe the content: header, nav, main, article, section, aside, footer.
  • Landmarks let screen reader users jump to navigation or main content.
  • Use one main per page.
  • Use div and span only when no semantic element fits.
  • Native elements bring behaviour, like a button's keyboard support.

Common mistakes

  • wrapping everything in div and span, then adding role attributes to repair it. Use the right element first.
  • using section as a generic wrapper. If it has no heading and no clear theme, a div is more honest.

Follow-ups

  • when is a header a banner landmark?
  • What is the difference between article and section?
  • Why is a nav element often given an aria-label?

Tips

  • Check your structure by turning off CSS or by using a screen reader landmark list. If the page makes sense as a plain outline, the semantics are good.
  • Walk through a simple page layout and name the element for each region. Keep it to the page you can picture.
  • Say what you would use for a clickable control: a button, never a div with an onclick.