MyInternships.in

CSS Grid

Grid Container

The grid container is where you define the shape of your grid: how many columns and rows, how wide each is, how big the gaps are, and optionally a map of named areas. Master these container properties and you can express almost any layout compactly.


Creating a Grid Container

display:grid turns an element into a block-level grid; display:inline-grid makes an inline-level one. Its direct children become grid items and are placed into cells automatically unless you position them explicitly.

Container Properties at a Glance

PropertyPurpose
grid-template-columnsDefines the number and size of columns
grid-template-rowsDefines the number and size of rows
gap / row-gap / column-gapSpacing between tracks
grid-template-areasNames regions to place items visually
justify-itemsAligns items horizontally within their cells
align-itemsAligns items vertically within their cells
justify-content / align-contentPositions the whole grid inside the container

The fr Unit and repeat()

The fr unit represents a fraction of the free space in the grid. Writing 1fr 1fr 1fr 1fr for four equal columns is repetitive, so repeat() is a shorthand: repeat(4, 1fr) means the same thing.

repeat() keeps track definitions short
.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* four equal columns */
  gap: 16px;
}
Four equal columns using repeat() — click Run
Example
<!DOCTYPE html>
<html>
<head>
<style>
  .grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    padding: 12px;
    background: #f1f5f9;
    font-family: sans-serif;
  }
  .cell {
    background: #8b5cf6; color: #fff; padding: 22px;
    border-radius: 6px; text-align: center; font-weight: 700;
  }
  .cell:nth-child(3n) { background: #f43f5e; }
</style>
</head>
<body>
  <div class="grid">
    <div class="cell">1</div><div class="cell">2</div>
    <div class="cell">3</div><div class="cell">4</div>
    <div class="cell">5</div><div class="cell">6</div>
    <div class="cell">7</div><div class="cell">8</div>
  </div>
</body>
</html>

Mixing Fixed and Flexible Tracks

You can combine fixed lengths, fr units and keywords like auto in one definition. A common pattern is a fixed sidebar and a flexible content column: grid-template-columns: 240px 1fr.

Fixed sidebar plus a flexible content column
Example
<!DOCTYPE html>
<html>
<head>
<style>
  .app {
    display: grid;
    grid-template-columns: 200px 1fr; /* fixed + flexible */
    gap: 14px;
    padding: 14px;
    font-family: sans-serif;
  }
  .side {
    background: #0f172a; color: #fff; padding: 24px;
    border-radius: 8px; font-weight: 700;
  }
  .content {
    background: #dbeafe; color: #1e3a8a; padding: 24px;
    border-radius: 8px; font-weight: 700;
  }
</style>
</head>
<body>
  <div class="app">
    <div class="side">Sidebar 200px</div>
    <div class="content">Content 1fr (takes the rest)</div>
  </div>
</body>
</html>
💡

repeat(auto-fit, minmax(180px, 1fr)) creates a fully responsive grid that fits as many 180px+ columns as will fit, then stretches them — no media queries needed.

Named Grid Template Areas

grid-template-areas lets you draw your layout as ASCII art. You name regions in the container and then assign each item a grid-area name. It makes complex page layouts remarkably readable.

A page layout built from named areas
Example
<!DOCTYPE html>
<html>
<head>
<style>
  .page {
    display: grid;
    grid-template-columns: 160px 1fr;
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
      "header header"
      "nav    main"
      "footer footer";
    gap: 10px;
    height: 320px;
    padding: 10px;
    font-family: sans-serif;
  }
  .page > div {
    color: #fff; display: flex; align-items: center;
    justify-content: center; border-radius: 6px; font-weight: 700;
  }
  .header { grid-area: header; background: #6366f1; }
  .nav    { grid-area: nav;    background: #14b8a6; }
  .main   { grid-area: main;   background: #f59e0b; }
  .footer { grid-area: footer; background: #ec4899; }
</style>
</head>
<body>
  <div class="page">
    <div class="header">Header</div>
    <div class="nav">Nav</div>
    <div class="main">Main</div>
    <div class="footer">Footer</div>
  </div>
</body>
</html>
ℹ️

Each row of grid-template-areas is a quoted string. Every row must have the same number of columns, and a name repeated across adjacent cells makes that item span them.

Key Points

  • display:grid creates the container; template properties define its tracks.
  • The fr unit distributes free space; repeat(n, size) avoids repetition.
  • Mix fixed lengths and fr, e.g. 240px 1fr for a sidebar layout.
  • repeat(auto-fit, minmax(...)) builds responsive grids without media queries.
  • grid-template-areas maps a layout visually using named regions.

Related CSS Topics

Keep learning with these closely related tutorials.

Ready to use your CSS skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships