HTML Layout

Web page layout refers to how content is arranged on a page. A good, clear layout is crucial for user experience. HTML provides elements for building layouts, while specific positioning and arrangement is achieved through CSS.

Using <div> Elements for Layout

Before HTML5, the <div> element was the only workhorse for building web page layouts. Developers would use <div> to create various sections of a page, such as headers, navigation, content areas, and footers, and then use CSS to style and position these <div> elements.

<div id="header">
  <h1>Website Title</h1>
</div>

<div id="nav">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
</div>

<div id="content">
  <h2>Article Title</h2>
  <p>This is the main content...</p>
</div>

<div id="footer">
  <p>Copyright &copy; 2024</p>
</div>

This approach works perfectly, but all sections are generic <div> elements, lacking semantic meaning.

HTML5 Semantic Layout Elements

To address the "div soup" problem, HTML5 introduced a series of layout elements with clear semantic meanings. These elements function similarly to <div> (they're all block-level containers), but their names clearly tell browsers, developers, and search engines what that section is for.

  • <header>: Defines the header of a document or section. Usually contains the site logo, title, navigation, etc.
  • <nav>: Defines a container for navigation links.
  • <section>: Defines an independent section or chapter in a document.
  • <article>: Defines an independent, self-contained unit of content, like a blog post or news article.
  • <aside>: Defines content that is indirectly related to the main content but can stand alone, like a sidebar or pull quote.
  • <footer>: Defines the footer of a document or section. Usually contains copyright information, contact details, etc.
  • <main>: Defines the core main content of the document. A document should have only one <main> element.

Using these semantic tags is a best practice in modern HTML.

Example: Restructuring layout using HTML5 semantic elements

<header>
  <h1>Website Title</h1>
</header>

<nav>
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
</nav>

<section>
  <article>
    <h2>Article Title</h2>
    <p>This is the main content...</p>
  </article>
</section>

<aside>
  <h3>Related Links</h3>
  <ul>
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 2</a></li>
  </ul>
</aside>

<footer>
  <p>Copyright &copy; 2024</p>
</footer>

Layout Techniques (Implemented via CSS)

HTML provides the "skeleton" for layouts, while specific arrangement techniques are controlled by CSS. Historically, layout techniques have gone through several stages:

  1. Table Layout: The earliest layout method, using <table> to arrange page content. This method is now considered incorrect because it misuses the semantics of tables and is difficult to maintain and make responsive.

  2. Float Layout: Using CSS's float property to float elements left or right to achieve multi-column layouts. This was the most mainstream layout method before Flexbox, but it has some side effects (like needing to clear floats) and can be tricky for complex layouts.

  3. Flexbox Layout: A modern one-dimensional layout model, ideal for aligning and distributing items in a container. It greatly simplifies many common layout tasks, such as vertical centering and creating equal-height columns. It's currently the most mainstream component-level layout solution.

  4. Grid Layout: A modern two-dimensional layout model that allows you to control both rows and columns simultaneously. It's very powerful and ideal for building complex, large-scale page-level layouts.

Summary: Learning HTML layout means learning how to use <div> and HTML5 semantic elements to build the page structure, then applying modern CSS techniques like Flexbox and Grid to achieve the final visual arrangement.