Explain Codes LogoExplain Codes Logo

Html5 best practices; section/header/aside/article elements

html
semantic-elements
best-practices
responsive-design
Anton ShumikhinbyAnton Shumikhin·Sep 13, 2024
TLDR

Maximize HTML5 semantics by associating section with grouped themes, header for introductions, aside for side content, and article for standalone pieces. Proper usage looks like:

<article> <header><h1>Article Heading</h1></header> <section><p>Main Topic</p></section> <aside><p>Related Info</p></aside> </article>

Remember: Align elements with their intended meaningheader for leading content, section for distinct topics, aside for complementary material, and article for standalone text. This enhances both accessibility and SEO, ensuring content is easily navigable and understandable.

Deciding between <article>, <section>, and <div>

Understanding when to use <article>, <section>, and <div> can greatly improve your HTML:

  • <article> should be used for independent, self-contained content. Think of it as a standalone narrative or story.
  • <section> is for thematically-related content. Like different topics in a book.
  • <div> is a generic container with no meaning. Use it as a last resort, when there's no suitable semantic tag.

Applying these elements excessively can lead to overly complex documents. Use them where they make sense.

How and when to use <nav> and <aside>

Implementing <nav> and <aside> correctly facilitates user navigation:

  • <nav> should encapsulate the principal navigation blocks, not just any group of links.
  • <aside> works for mildly related-to-main content. Think of it as a side note.

Remember, user experience is dramatically influenced by how you organize your page content.

<header> and <footer> add meaning to their containing element, not just for the whole page:

  • Inside <article>, header might contain the title and author, while footer could include date and related links.
  • Within <section>, header could describe that segment, and footer might summarize or link to related content.

These elements contribute to a more structured and meaningful page layout.

Using HTML5 outlining to your advantage

The HTML5 outlining algorithm forms a structured document outline based on intelligent sectioning:

  • Single <main> element reflects the main content area despite multiple sections.
  • <header> and <footer> mark zones and provide context.

Structured outlines improve accessibility and boost SEO performance.

Avoid excessive use of <div>

Adopt HTML5's semantic elements to avoid "divitis":

  • Replace <div>s with <section> and <article> where it enhances semantics.
  • Semantic tags not only enhance readability but also improve page's understanding by screen readers and search engines.

Nuances of using <header> and <body>

Headers don't always need <h1>-<h6> tags:

  • Headers can include logos, search forms, or any introductory content.
  • <body> tag is mainly for applying overarching styles rather than indicating structural separation.

Such nuances in using HTML5 elements enhance the precision and presentation of your content.