HTML Docs

HTML Layout

A detailed guide on how to structure layouts using HTML `<div>` elements, semantic tags, Flexbox, and Grid for creating modern web designs.

Creating a layout in HTML involves structuring the page in a way that content is organized, accessible, and visually appealing. You can use a combination of <div>, semantic HTML elements, and CSS techniques like Flexbox and Grid to achieve a responsive and organized layout.

1. 🧑‍💻 Using <div>

The <div> tag is one of the most commonly used elements in HTML for creating divisions or sections in a webpage. It's a non-semantic element, which means it doesn't add meaning to the content it wraps, but it is incredibly useful for styling and grouping elements.

Example:

<div class="header">
  <h1>My Website</h1>
  <p>Welcome to my website!</p>
</div>
 
<div class="main-content">
  <p>This is where the main content goes.</p>
</div>
 
<div class="footer">
  <p>&copy; 2023 My Website</p>
</div>
  • <div>: Used for grouping and styling elements, often with the help of CSS classes.

While <div> is flexible and widely used, it's often better to use semantic HTML elements when possible to improve accessibility and SEO.

2. 📑 Using Semantic Tags

HTML5 introduces semantic elements that not only structure the page but also provide meaning. These tags make it easier for search engines and assistive technologies to understand the content.

Some commonly used semantic tags:

  • <header>: Represents the header section of the page or a section of the page.
  • <nav>: Defines a navigation section.
  • <main>: Represents the main content of the document.
  • <section>: Defines a section of content, often with a heading.
  • <article>: Represents an independent piece of content.
  • <footer>: Represents the footer section of the page or a section.

Example:

<header>
  <h1>Website Title</h1>
</header>
 
<nav>
  <ul>
    <li><a href="#home">Home</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#contact">Contact</a></li>
  </ul>
</nav>
 
<main>
  <section>
    <h2>Introduction</h2>
    <p>Welcome to my website!</p>
  </section>
  
  <article>
    <h3>Blog Post</h3>
    <p>This is the content of my blog post.</p>
  </article>
</main>
 
<footer>
  <p>&copy; 2023 My Website</p>
</footer>
  • Semantic tags: Improve accessibility, SEO, and readability.

3. 💪 Flexbox (CSS)

Flexbox is a powerful layout model in CSS that allows you to design complex layouts with ease. It enables flexible and responsive designs by distributing space between items and aligning them in various ways.

Example:

<div class="container">
  <div class="box">Box 1</div>
  <div class="box">Box 2</div>
  <div class="box">Box 3</div>
</div>
 
<style>
  .container {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .box {
    width: 100px;
    height: 100px;
    background-color: lightblue;
    text-align: center;
    line-height: 100px;
  }
</style>

In this example, Flexbox arranges the three boxes in a row, distributing space between them. You can easily control the alignment, distribution, and wrapping of items.

Key Flexbox properties:

  • display: flex: Enables Flexbox on a container.
  • justify-content: Aligns items horizontally (e.g., space-between, center).
  • align-items: Aligns items vertically (e.g., center, flex-start).

4. 🧩 Grid (CSS)

CSS Grid is another layout system that allows for more complex and two-dimensional layouts. Unlike Flexbox, which is one-dimensional (either row or column), Grid allows you to define both rows and columns, making it perfect for complex web designs.

Example:

<div class="grid-container">
  <div class="item1">Item 1</div>
  <div class="item2">Item 2</div>
  <div class="item3">Item 3</div>
  <div class="item4">Item 4</div>
</div>
 
<style>
  .grid-container {
    display: grid;
    grid-template-columns: repeat(2, 1fr); /* 2 equal-width columns */
    grid-gap: 10px;
  }
  .item1, .item2, .item3, .item4 {
    background-color: lightcoral;
    text-align: center;
    padding: 20px;
  }
</style>

In this example, the grid container creates a 2-column layout, and items inside the grid are placed into columns.

Key Grid properties:

  • display: grid: Enables Grid on a container.
  • grid-template-columns: Defines the number and size of columns.
  • grid-gap: Adds space between grid items.

🧪 Try Yourself

import "./styles.css";

document.getElementById("app").innerHTML = `
<h1>Hello world</h1>
`;

🏁 Conclusion

HTML layouts can be achieved through a variety of methods, from using <div> and semantic tags to utilizing modern CSS techniques like Flexbox and Grid. By combining these elements, you can create responsive, accessible, and organized webpage layouts.

Pro Tip: Use semantic tags for better SEO and accessibility, and use Flexbox or Grid for flexible and responsive layouts.

On this page