More HTML answers

by Danny
Published 2 min read

A div is a generic box with no meaning, a section groups a themed part of a page under a heading, and an article wraps content that makes sense on its own. Here is how to choose.

The short answer

#

Use article for content that stands on its own, such as a blog post, a comment or a product card. Use section for a themed part of a page that has its own heading. Use div when you only need a box for styling or layout and the content has no extra meaning.


_13
<main>
_13
<article>
_13
<h1>Baking sourdough at home</h1>
_13
_13
<section aria-labelledby="ingredients">
_13
<h2 id="ingredients">Ingredients</h2>
_13
<div class="two-column">
_13
<ul>...</ul>
_13
<img src="/flour.jpg" alt="Bags of strong white flour">
_13
</div>
_13
</section>
_13
</article>
_13
</main>

Why it happens

#

All three elements look identical by default: block boxes with no styling. The difference is meaning, and that meaning is exposed to the accessibility tree that screen readers, browser reader modes and other tools use.

  • div has no role. Assistive technology ignores it and reads its contents as if it were not there.
  • article has the article role. It tells tools that the content is a complete, independent unit.
  • section has the region role, but only when it has an accessible name, for example from aria-labelledby pointing at its heading. A region with a name becomes a landmark that screen reader users can jump to. Without a name it behaves like a div.

So picking the right element is less about passing a validator and more about giving people who navigate by structure something useful to navigate with.

How to fix it

#

Use article for self-contained content

A good test: would this make sense if you lifted it out and put it in an RSS feed, a search result or another page? If yes, it is an article.


_10
<article class="comment">
_10
<h3>Sam, 3 hours ago</h3>
_10
<p>This worked for me, thank you.</p>
_10
</article>

Blog posts, news stories, forum posts, comments and product cards in a listing all fit.

Use section for a themed group with a heading

A section is part of something larger and only makes sense in context: the "Ingredients" part of a recipe, the "Pricing" part of a landing page. It should almost always start with a heading. If it is important enough to be a landmark, name it with aria-labelledby.


_10
<section aria-labelledby="pricing-title">
_10
<h2 id="pricing-title">Pricing</h2>
_10
...
_10
</section>

Use div for layout and styling

Wrappers for flexbox or grid, card shells, animation hooks and anything that exists only because of CSS should be a div. There is nothing wrong with div; the problem is only using it where a meaningful element would help.

Reach for the more specific elements first

Before choosing between these three, check whether a more specific element fits: main for the main content, nav for navigation links, header and footer for introductory and closing content, aside for related but separate content, and figure for an image or code sample with a caption.

Nesting works both ways

An article can contain section elements (the parts of a long post), and a section can contain article elements (a "Latest posts" section holding several post cards). Choose based on what each piece is, not on where it sits.

Common pitfalls

#
  • Using section as a generic wrapper just to avoid div. It adds no value without a heading.
  • Naming every section. Too many landmarks make landmark navigation noisy; name only the major parts.
  • Expecting each section to reset heading levels. The old document outline algorithm was never implemented by browsers and has been removed from the HTML standard, so use h2, h3 and so on in order.
  • Wrapping the whole page in an article. Use main for that.
  • Adding role="region" or role="article" to a div when the real element would do the same job.
#

Was this resource helpful?