TExcelTAD
Home › CSS › CSS Grid Container

CSS Grid Container

All the structure of a grid is defined on the container: how many tracks there are, how big they are, how gaps work, and how items are aligned inside their cells. This lesson tours the container properties.

Container properties at a glance

Property Purpose
grid-template-columns / grid-template-rows Size of each explicit column / row
grid-template-areas Name regions of the grid with a text "map"
gap, row-gap, column-gap Gutters between tracks
grid-auto-rows / grid-auto-columns Size of tracks created automatically
grid-auto-flow row, column, dense — how auto-placed items fill the grid
justify-items / align-items Align items inside their cells (horizontal / vertical)
justify-content / align-content Align the whole grid inside the container
place-items Shorthand: align-items justify-items

Track sizing functions

Value Meaning
200px, 10rem, 20% Fixed or percentage size
1fr Share of free space
auto Sized by content, can stretch
minmax(100px, 1fr) At least 100px, at most 1fr
repeat(3, 1fr) Repeat a pattern
repeat(auto-fit, minmax(…)) As many tracks as fit
min-content / max-content Narrowest / widest the content can be
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .g { display: grid; gap: 6px; margin-bottom: 10px; }
  .g div { background: #8d6e63; color: white; padding: 10px 6px; border-radius: 4px; }
  .one { grid-template-columns: max-content 1fr auto; }
  .two { grid-template-columns: minmax(150px, 1fr) 2fr; }
</style>
</head>
<body>
  <div class="g one"><div>max-content label</div><div>1fr fills the middle</div><div>auto</div></div>
  <div class="g two"><div>minmax(150px, 1fr)</div><div>2fr</div></div>
</body>
</html>

Explicit and implicit rows

Tracks you define with grid-template-* form the explicit grid. If there are more items than cells, the browser creates extra implicit tracks. Control their size with grid-auto-rows:

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .g {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: 60px;          /* only the first row is explicit */
    grid-auto-rows: minmax(40px, auto); /* the rest */
    gap: 6px;
  }
  .g div { background: #29b6f6; color: white; padding: 6px; border-radius: 4px; }
  .g div:nth-child(-n+4) { background: #0277bd; }
</style>
</head>
<body>
  <div class="g">
    <div>explicit</div><div>explicit</div><div>explicit</div><div>explicit</div>
    <div>auto row</div><div>auto</div><div>auto</div><div>auto</div><div>auto</div>
  </div>
</body>
</html>

grid-template-areas

Named areas let you draw the layout as ASCII art. Each string is a row; each word is a cell. Repeating a name makes the area span; a . leaves a cell empty. Items join an area with grid-area: name.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 6px; }
  .layout {
    display: grid;
    grid-template-columns: 100px 1fr 1fr;
    grid-template-rows: 40px 1fr 36px;
    height: 250px;
    gap: 6px;
    grid-template-areas:
      "head head head"
      "side main ads"
      "foot foot foot";
  }
  .layout > div { color: white; padding: 6px; border-radius: 6px; }
  .h { grid-area: head; background: #e53935; }
  .s { grid-area: side; background: #8e24aa; }
  .m { grid-area: main; background: #1e88e5; }
  .a { grid-area: ads;  background: #fb8c00; }
  .f { grid-area: foot; background: #43a047; }
</style>
</head>
<body>
  <div class="layout">
    <div class="h">head</div><div class="s">side</div><div class="m">main</div><div class="a">ads</div><div class="f">foot</div>
  </div>
</body>
</html>

Tip: Changing grid-template-areas inside a media query is the easiest way to rearrange a whole page for mobile.

Aligning items and the grid

justify-items aligns items horizontally within their cells and align-items vertically. The default for both is stretch. place-items: center centres every item in its cell.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 10px; font-size: 12px; }
  .g { display: grid; grid-template-columns: repeat(2, 70px); grid-auto-rows: 70px; gap: 4px; background: #fff8e1; padding: 4px; }
  .g div { background: #ffb300; padding: 4px; border-radius: 3px; }
</style>
</head>
<body>
  <div>stretch<div class="g"><div>a</div><div>b</div><div>c</div><div>d</div></div></div>
  <div>place-items: center<div class="g" style="place-items:center"><div>a</div><div>b</div><div>c</div><div>d</div></div></div>
  <div>start / end<div class="g" style="align-items:end; justify-items:start"><div>a</div><div>b</div><div>c</div><div>d</div></div></div>
</body>
</html>

justify-content and align-content matter when the tracks are smaller than the container; they then position the grid as a whole (e.g. justify-content: center).

Test yourself

What does grid-auto-rows control?

  1. Rows defined in grid-template-rows
  2. The size of rows created automatically for extra items
  3. The number of columns
  4. The gap between rows

In grid-template-areas, what does a . mean?

  1. An error
  2. An empty cell
  3. A full-width area
  4. The current item

Which shorthand centres every grid item inside its cell?

  1. place-items: center
  2. justify-content: center
  3. text-align: center
  4. grid-center: true