QP School
CSS Grid Subgrid - Printable Version

+- QP School (https://qomplainerzschool.lima-city.de)
+-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3)
+--- Forum: CSS3 Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=5)
+--- Thread: CSS Grid Subgrid (/showthread.php?tid=5192)



CSS Grid Subgrid - Qomplainerz - 07-26-2023

Description:

CSS Grid's subgrid value allows you to create nested grids that inherit the column and row sizes from the parent grid.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>CSS Grid Subgrid Example</title>
  <style>
    .grid-container {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      grid-template-rows: repeat(2, 150px);
      grid-gap: 10px;
    }
    .grid-item {
      border: 1px solid black;
      padding: 10px;
    }
    .nested-grid {
      display: grid;
      grid-template-columns: subgrid;
      grid-gap: 5px;
    }
    .nested-item {
      background-color: lightblue;
      padding: 5px;
    }
  </style>
</head>
<body>
  <div class="grid-container">
    <div class="grid-item">
      <div class="nested-grid">
        <div class="nested-item">A</div>
        <div class="nested-item">B</div>
      </div>
    </div>
    <div class="grid-item">
      <div class="nested-grid">
        <div class="nested-item">C</div>
        <div class="nested-item">D</div>
      </div>
    </div>
  </div>
</body>
</html>

Explanation:

In this example, the .grid-container creates a 2x2 grid layout. Each .grid-item contains a .nested-grid, which uses display: grid and grid-template-columns: subgrid to inherit the column size from the parent grid. This way, the nested grids automatically align with the parent's column sizes, creating a consistent layout.