QP School

Full Version: CSS Grid Subgrid
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.