HTML and CSS course Β· Module 8: CSS Grid

Building Layouts with Flexbox and CSS Grid

11 min read Β·
In this lesson5

Welcome to another chamber of Egyptian knowledge! I am Mohamed, and today I will teach you how to build classic and advanced web page layouts using the power of CSS Grid and Flexbox.

Just as ancient architects designed different parts of pyramids - entrances, chambers, corridors - so too will we learn to design various page layouts: from simple to the most complex!

Layout 1: Holy Grail Layout

The Holy Grail is a classic page layout that was extremely difficult to achieve before CSS Grid. It consists of:

  • Header - the heading at the top
  • Footer - the footer at the bottom
  • Main - the main content in the center
  • Nav - navigation on the left side
  • Aside - sidebar on the right side

All these elements form a harmonious whole, much like corridors and chambers in an Egyptian pyramid!

CSS Grid Implementation:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Holy Grail Layout</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    .holy-grail {
      display: grid;
      grid-template:
        "header header header" auto
        "nav main aside" 1fr
        "footer footer footer" auto
        / 200px 1fr 200px;
      min-height: 100vh;
      gap: 1rem;
      padding: 1rem;
    }

    .header {
      grid-area: header;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      color: white;
      padding: 2rem;
      text-align: center;
      border-radius: 8px;
    }

    .nav {
      grid-area: nav;
      background: #f8f9fa;
      padding: 1.5rem;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    .main {
      grid-area: main;
      background: white;
      padding: 2rem;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    .aside {
      grid-area: aside;
      background: #f8f9fa;
      padding: 1.5rem;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    .footer {
      grid-area: footer;
      background: #343a40;
      color: white;
      padding: 2rem;
      text-align: center;
      border-radius: 8px;
    }

    /* Responsiveness - on small screens the layout changes to single column */
    @media (max-width: 768px) {
      .holy-grail {
        grid-template:
          "header" auto
          "nav" auto
          "main" 1fr
          "aside" auto
          "footer" auto
          / 1fr;
      }
    }

    .nav ul {
      list-style: none;
    }

    .nav li {
      margin-bottom: 0.5rem;
    }

    .nav a {
      color: #495057;
      text-decoration: none;
      padding: 0.5rem;
      display: block;
      border-radius: 4px;
      transition: background-color 0.3s;
    }

    .nav a:hover {
      background-color: #e9ecef;
    }
  </style>
</head>
<body>
  <div class="holy-grail">
    <header class="header">
      <h1>Egyptian Library of Knowledge</h1>
      <p>Discover the secrets of ancient hieroglyphs</p>
    </header>

    <nav class="nav">
      <h3>Navigation</h3>
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Pyramids</a></li>
        <li><a href="#">Pharaohs</a></li>
        <li><a href="#">Hieroglyphs</a></li>
        <li><a href="#">Mythology</a></li>
      </ul>
    </nav>

    <main class="main">
      <h2>Welcome to the world of ancient Egypt</h2>
      <p>Egypt is a land full of mysteries and architectural wonders. The pyramids that have survived for thousands of years are a testament to the extraordinary knowledge and skills of ancient builders.</p>
      <p>Here you will learn the secrets of web page layouts that - like pyramids - are built with precision and harmony.</p>
      <h3>Main principles of layout construction</h3>
      <ul>
        <li>The structure must be stable and predictable</li>
        <li>Every element has its place and function</li>
        <li>Responsiveness is the key to universality</li>
        <li>Simplicity leads to elegance</li>
      </ul>
    </main>

    <aside class="aside">
      <h3>Fun Facts</h3>
      <p><strong>Did you know...</strong></p>
      <p>The Great Pyramid of Giza was the tallest structure in the world for 3,800 years?</p>
      <p>CSS Grid allows you to create layouts as enduring as pyramids!</p>
    </aside>

    <footer class="footer">
      <p>&copy; 2024 Egyptian Coding Academy</p>
      <p>All rights reserved by the pharaohs</p>
    </footer>
  </div>
</body>
</html>

Code explanation:

  1. grid-template - defines grid areas in shorthand form:

    • First row: "header header header" auto - header spans 3 columns
    • Second row: "nav main aside" 1fr - navigation, main content, sidebar
    • Third row: "footer footer footer" auto - footer spans 3 columns
    • After / we define column widths: 200px 1fr 200px
  2. grid-area - assigns elements to named areas

  3. min-height: 100vh - the layout takes up at least the full screen height

  4. Media query - on small screens, the layout changes to a single column

Layout 2: Sidebar Layout

This layout is perfect for dashboards, admin panels, and web applications. It consists of:

  • Fixed sidebar - navigation that doesn't scroll
  • Scrollable content - scrollable main content

Implementation with Flexbox + Grid:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sidebar Layout</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      overflow: hidden;
    }

    .sidebar-layout {
      display: flex;
      height: 100vh;
    }

    .sidebar {
      width: 280px;
      background: #2c3e50;
      color: #ecf0f1;
      display: flex;
      flex-direction: column;
      flex-shrink: 0;
    }

    .sidebar-header {
      padding: 2rem;
      background: #34495e;
      border-bottom: 2px solid #e67e22;
    }

    .sidebar-header h2 {
      color: #e67e22;
      font-size: 1.5rem;
      margin-bottom: 0.5rem;
    }

    .sidebar-nav {
      flex: 1;
      overflow-y: auto;
      padding: 1rem 0;
    }

    .sidebar-nav ul {
      list-style: none;
    }

    .sidebar-nav li {
      margin: 0.25rem 0;
    }

    .sidebar-nav a {
      color: #ecf0f1;
      text-decoration: none;
      padding: 0.75rem 1.5rem;
      display: flex;
      align-items: center;
      gap: 0.75rem;
      transition: all 0.3s;
      border-left: 3px solid transparent;
    }

    .sidebar-nav a:hover,
    .sidebar-nav a.active {
      background: #34495e;
      border-left-color: #e67e22;
      padding-left: 2rem;
    }

    .sidebar-footer {
      padding: 1.5rem;
      background: #34495e;
      border-top: 1px solid #34495e;
    }

    .main-content {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
    }

    .content-header {
      background: white;
      padding: 1.5rem 2rem;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
      z-index: 10;
    }

    .content-body {
      flex: 1;
      overflow-y: auto;
      padding: 2rem;
      background: #f8f9fa;
    }

    .card {
      background: white;
      border-radius: 8px;
      padding: 1.5rem;
      margin-bottom: 1.5rem;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    .card h3 {
      color: #2c3e50;
      margin-bottom: 1rem;
    }

    /* Responsiveness */
    @media (max-width: 768px) {
      .sidebar {
        position: fixed;
        left: -280px;
        transition: left 0.3s;
        z-index: 1000;
        height: 100vh;
      }

      .sidebar.open {
        left: 0;
      }

      .main-content {
        width: 100%;
      }
    }

    .menu-toggle {
      display: none;
      background: #e67e22;
      color: white;
      border: none;
      padding: 0.5rem 1rem;
      cursor: pointer;
      border-radius: 4px;
    }

    @media (max-width: 768px) {
      .menu-toggle {
        display: block;
      }
    }
  </style>
</head>
<body>
  <div class="sidebar-layout">
    <aside class="sidebar" id="sidebar">
      <div class="sidebar-header">
        <h2>Pharaoh's Panel</h2>
        <p>Empire management system</p>
      </div>

      <nav class="sidebar-nav">
        <ul>
          <li><a href="#" class="active">Dashboard</a></li>
          <li><a href="#">Pyramids</a></li>
          <li><a href="#">Subjects</a></li>
          <li><a href="#">State Treasury</a></li>
          <li><a href="#">Hieroglyphs</a></li>
          <li><a href="#">Art</a></li>
          <li><a href="#">Army</a></li>
          <li><a href="#">Agriculture</a></li>
          <li><a href="#">Statistics</a></li>
          <li><a href="#">Settings</a></li>
        </ul>
      </nav>

      <div class="sidebar-footer">
        <p><strong>Pharaoh Ramesses II</strong></p>
        <p style="font-size: 0.875rem; opacity: 0.8;">Ruler of the Two Lands</p>
      </div>
    </aside>

    <div class="main-content">
      <header class="content-header">
        <button class="menu-toggle" onclick="document.getElementById('sidebar').classList.toggle('open')">
          Menu
        </button>
        <h1>Empire Dashboard</h1>
        <p>Welcome to the Egyptian state management panel</p>
      </header>

      <main class="content-body">
        <div class="card">
          <h3>Empire statistics</h3>
          <p>Your empire is flourishing! The number of subjects has grown by 15% this month.</p>
        </div>

        <div class="card">
          <h3>Active construction projects</h3>
          <ul>
            <li>Great Pyramid of Giza - 75% complete</li>
            <li>Temple of Luxor - 45% complete</li>
            <li>Obelisk at Karnak - 90% complete</li>
          </ul>
        </div>

        <div class="card">
          <h3>Latest decrees</h3>
          <p>The Pharaoh has ordered the construction of a new irrigation canal in the Nile Delta.</p>
        </div>

        <div class="card">
          <h3>Long content for scrolling</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
          <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
          <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
        </div>
      </main>
    </div>
  </div>
</body>
</html>

Key techniques in the Sidebar Layout:

  1. display: flex - the main container uses flexbox to arrange the sidebar and content side by side
  2. flex-shrink: 0 - the sidebar never shrinks
  3. overflow-y: auto - only the content scrolls; the sidebar remains visible
  4. position: fixed - on mobile, the sidebar is hidden and appears on click

Layout 3: Dashboard Grid (Card Grid)

The dashboard grid is a layout ideal for admin panels where you have many cards with different information.

CSS Grid Implementation:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dashboard Grid</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      padding: 2rem;
      min-height: 100vh;
    }

    .dashboard-header {
      background: white;
      padding: 2rem;
      border-radius: 12px;
      margin-bottom: 2rem;
      box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    }

    .dashboard-header h1 {
      color: #2c3e50;
      margin-bottom: 0.5rem;
    }

    .dashboard-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
      gap: 1.5rem;
      margin-bottom: 2rem;
    }

    /* Special layouts for certain cards */
    .card-large {
      grid-column: span 2;
    }

    @media (max-width: 768px) {
      .card-large {
        grid-column: span 1;
      }
    }

    .card {
      background: white;
      border-radius: 12px;
      padding: 1.5rem;
      box-shadow: 0 4px 6px rgba(0,0,0,0.1);
      transition: transform 0.3s, box-shadow 0.3s;
    }

    .card:hover {
      transform: translateY(-4px);
      box-shadow: 0 8px 12px rgba(0,0,0,0.15);
    }

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 1rem;
      padding-bottom: 1rem;
      border-bottom: 2px solid #f0f0f0;
    }

    .card-header h3 {
      color: #2c3e50;
      font-size: 1.1rem;
    }

    .card-icon {
      font-size: 2rem;
    }

    .card-stat {
      font-size: 2.5rem;
      font-weight: bold;
      color: #667eea;
      margin: 1rem 0;
    }

    .card-label {
      color: #7f8c8d;
      font-size: 0.9rem;
    }

    .card-change {
      display: inline-block;
      padding: 0.25rem 0.5rem;
      border-radius: 4px;
      font-size: 0.875rem;
      font-weight: 600;
    }

    .card-change.positive {
      background: #d4edda;
      color: #155724;
    }

    .card-change.negative {
      background: #f8d7da;
      color: #721c24;
    }

    .chart-placeholder {
      width: 100%;
      height: 200px;
      background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #7f8c8d;
      margin-top: 1rem;
    }

    .activity-list {
      list-style: none;
    }

    .activity-item {
      padding: 0.75rem 0;
      border-bottom: 1px solid #f0f0f0;
      display: flex;
      align-items: center;
      gap: 1rem;
    }

    .activity-item:last-child {
      border-bottom: none;
    }

    .activity-icon {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      background: #667eea;
      color: white;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
    }

    .activity-content {
      flex: 1;
    }

    .activity-title {
      font-weight: 600;
      color: #2c3e50;
      margin-bottom: 0.25rem;
    }

    .activity-time {
      font-size: 0.85rem;
      color: #7f8c8d;
    }
  </style>
</head>
<body>
  <div class="dashboard-header">
    <h1>Egyptian Empire Dashboard</h1>
    <p>Browse the most important metrics and statistics</p>
  </div>

  <div class="dashboard-grid">
    <!-- Card 1: Subject statistics -->
    <div class="card">
      <div class="card-header">
        <h3>Subjects</h3>
        <span class="card-icon">β—†</span>
      </div>
      <div class="card-stat">2,547,893</div>
      <div class="card-label">Total number of subjects</div>
      <span class="card-change positive">↑ +12.5%</span>
    </div>

    <!-- Card 2: Pyramids -->
    <div class="card">
      <div class="card-header">
        <h3>Pyramids</h3>
        <span class="card-icon">β—†</span>
      </div>
      <div class="card-stat">138</div>
      <div class="card-label">Completed pyramids</div>
      <span class="card-change positive">↑ +3 new</span>
    </div>

    <!-- Card 3: Treasury -->
    <div class="card">
      <div class="card-header">
        <h3>State Treasury</h3>
        <span class="card-icon">β—†</span>
      </div>
      <div class="card-stat">45,678</div>
      <div class="card-label">Gold talents</div>
      <span class="card-change negative">↓ -5.2%</span>
    </div>

    <!-- Card 4: Harvest -->
    <div class="card">
      <div class="card-header">
        <h3>Harvest</h3>
        <span class="card-icon">β—†</span>
      </div>
      <div class="card-stat">892,456</div>
      <div class="card-label">Tons of grain collected</div>
      <span class="card-change positive">↑ +18.7%</span>
    </div>

    <!-- Card 5: Chart (large) -->
    <div class="card card-large">
      <div class="card-header">
        <h3>Subject population growth</h3>
        <span class="card-icon">β—†</span>
      </div>
      <div class="chart-placeholder">
        [Line chart showing population growth]
      </div>
    </div>

    <!-- Card 6: Recent activities -->
    <div class="card card-large">
      <div class="card-header">
        <h3>Recent events</h3>
        <span class="card-icon">β—†</span>
      </div>
      <ul class="activity-list">
        <li class="activity-item">
          <div class="activity-icon">β—†</div>
          <div class="activity-content">
            <div class="activity-title">Completed construction of the pyramid at Giza</div>
            <div class="activity-time">2 hours ago</div>
          </div>
        </li>
        <li class="activity-item">
          <div class="activity-icon">β—†</div>
          <div class="activity-content">
            <div class="activity-title">Victory in the Battle of the Nile</div>
            <div class="activity-time">5 hours ago</div>
          </div>
        </li>
        <li class="activity-item">
          <div class="activity-icon">β—†</div>
          <div class="activity-content">
            <div class="activity-title">New pharaoh's decree announced</div>
            <div class="activity-time">1 day ago</div>
          </div>
        </li>
        <li class="activity-item">
          <div class="activity-icon">β—†</div>
          <div class="activity-content">
            <div class="activity-title">Harvest season has begun</div>
            <div class="activity-time">2 days ago</div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</body>
</html>

Key techniques in the Dashboard Grid:

  1. repeat(auto-fit, minmax(300px, 1fr)) - automatic adjustment of the number of columns
  2. grid-column: span 2 - some cards span 2 columns (wider)
  3. gap: 1.5rem - spacing between cards
  4. Responsiveness - automatic conversion to a single column on small screens

Comparison: When to Use Grid vs Flexbox?

Use CSS Grid when:

  • You need a two-dimensional layout (rows AND columns)
  • You have a complex layout with many sections (Holy Grail, Dashboard)
  • You want to precisely control element positions
  • You need overlapping areas

Use Flexbox when:

  • You're working with a one-dimensional layout (only rows OR columns)
  • You're arranging elements in a line (navbar, lists)
  • You need a dynamic layout that adapts to content
  • You're building components (cards, buttons, forms)

Best Combination - Grid + Flexbox:

/* Grid for the main layout */
.page {
  display: grid;
  grid-template:
    "header" auto
    "content" 1fr
    "footer" auto
    / 1fr;
}

/* Flexbox for components inside */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav {
  display: flex;
  gap: 1rem;
}

Summary

Just as Egyptian pyramids are composed of precisely arranged stone blocks, modern websites are composed of well-thought-out layouts:

  1. Holy Grail Layout - classic layout with header, footer, nav, main, aside
  2. Sidebar Layout - ideal for dashboards and panels
  3. Dashboard Grid - flexible card grid

Each of these layouts has its use case, and you can combine them to create excellent user interfaces!

Code for this lesson: index.html
<!DOCTYPE html>
<html lang="en">
<head><title>Nav and Main</title></head>
<body style="background:#d4a574;font-family:serif;padding:20px;color:#3d2914">
<h1>Nav and Main</h1>
<nav>Navigation - the corridors of the pyramid</nav>
<main>Main chamber of the pharaoh</main>
</body>
</html>

Good luck building your digital pyramids, young architect!

Carry one thought out of this chamber, young architect: CSS Grid draws the plan of the whole building in two dimensions at once - header, nav, main, aside and footer - while Flexbox arranges whatever stands inside a single chamber, which is why the strongest layouts always use both together.

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. What is the difference between auto-fill and auto-fit in repeat()?

  2. 2. What is the grid-area property used for on a child element?

Hands-on tasks in the game

  • Click in order

    Arrange CSS Grid container properties in the order of defining a grid layout:

  • Code editor

    Use named lines [sidebar-start] [main-start] [main-end] to precisely position elements in the grid.

  • Horizontal ordering

    Arrange the CSS Grid column definition with named lines:

  • Click in order

    Arrange the grid-template-areas declaration with a header, content, and footer

  1. QuestionDifference Between auto-fill and auto-fit
  2. QuestionThe grid-area Property on Elements
  3. Click in orderCSS Grid Container Properties
  4. Coding taskNamed Lines in CSS Grid
  5. Put in orderBuilding Named Grid Lines
  6. Click in orderBuilding grid-template-areas

Useful articles