Getting Started with CSS Grid

Learn CSS Grid concepts, properties, and practical techniques for building web layouts.

🎯 Introduction

As web layouts have become more complex, traditional techniques have become harder to work with. Flexbox solves many one-dimensional layout problems, but has limits when coordinating two dimensions. CSS Grid makes it easier to control rows and columns together. This beginner's guide introduces Grid concepts and core properties, then shows how to build common page layouts from scratch.

With these skills, you can build responsive grids, three-by-three grids, complex card arrangements, and entire page structures, improving development efficiency and maintainability.


🧱 What is CSS Grid?

CSS Grid is a two-dimensional layout system introduced with CSS3. It divides an area into rows and columns, then places content into the resulting cells. Think of a grid as a chessboard or table.

Compared with floats, positioning, and Flexbox, Grid's main advantage is its ability to coordinate rows and columns simultaneously rather than arrange content in just one dimension.

The Core Ideas Behind CSS Grid

  • Define a grid container.
  • Define rows and columns inside it.
  • Place child elements (grid items) into grid cells.
  • Let items span multiple rows or columns to create complex layouts.
Think of Grid as drawing a table-like structure for your page content, where you control how each item appears and how much space it occupies.

📐 Grid Concepts and Properties

Start with these important concepts:

TermDescription
Grid containerA parent declared with display: grid
Grid itemA direct child of the container
Grid lineA horizontal or vertical line dividing the grid
Grid cellA single cell bounded by adjacent grid lines
Grid areaA rectangular area covering one or more adjacent cells

Container Properties

PropertyPurposeExample
displayCreates a grid containerdisplay: grid;
grid-template-columnsDefines column widths and the number of columnsgrid-template-columns: 100px 1fr 2fr;
grid-template-rowsDefines row heights and the number of rowsgrid-template-rows: 200px auto;
grid-gap (grid-row-gap, grid-column-gap)Sets row and column gapsgrid-gap: 10px 20px;
justify-itemsAligns items horizontally within their grid areasjustify-items: center;
align-itemsAligns items vertically within their grid areasalign-items: start;

Item Properties

PropertyPurposeExample
grid-column-start/endSets an item's starting and ending column linesgrid-column-start: 2;
grid-row-start/endSets an item's starting and ending row linesgrid-row-end: span 2;
grid-areaPlaces an item in an area that can span multiple cellsgrid-area: 1 / 2 / 3 / 4;
justify-selfAligns an individual item horizontally within its grid areajustify-self: end;
align-selfAligns an individual item vertically within its grid areaalign-self: center;

🧩 Common Layouts with Grid

1. A Simple Three-Column Layout

<div class="container">
    <div class="item">Left Column</div>
    <div class="item">Main Content</div>
    <div class="item">Right Column</div>
</div>

<style>
    .container {
        display: grid;
        grid-template-columns: 1fr 2fr 1fr;
        grid-gap: 10px;
        height: 200px;
    }
    .item {
        background-color: #a0c4ff;
        padding: 10px;
        border: 1px solid #333;
        text-align: center;
    }
</style>

2. A Header–Content–Footer Layout

<div class="container">
    <header class="header">Header</header>
    <main class="main">Main Content</main>
    <footer class="footer">Footer</footer>
</div>

<style>
    .container {
        display: grid;
        grid-template-rows: 60px 1fr 40px;
        height: 300px;
        grid-gap: 5px;
    }
    .header,
    .main,
    .footer {
        background-color: #ffadad;
        padding: 10px;
        border: 1px solid #333;
        text-align: center;
    }
</style>

3. A Three-by-Three Grid

<div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
    <div class="item">7</div>
    <div class="item">8</div>
    <div class="item">9</div>
</div>

<style>
    .container {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        grid-template-rows: repeat(3, 100px);
        grid-gap: 10px;
        width: 320px;
    }
    .item {
        background-color: #ffd6a5;
        border: 1px solid #333;
        text-align: center;
        line-height: 100px;
        font-size: 24px;
    }
</style>

🔍 Grid vs. Flexbox

AspectCSS GridFlexbox
DimensionsTwo-dimensional (rows and columns)One-dimensional (a row or a column)
Layout controlPrecise control over row heights, column widths, and cell placementWell suited to flowing content
Spanning rows and columnsSupportedNot supported
Best suited toComplex grids and overall page layoutsSimple linear layouts and one-dimensional lists
Learning curveRelatively more complexEasy to get started

🛠️ Common Grid Properties

PropertyPurposeNotes
display: gridCreates a grid containerRequired
grid-template-columnsSets column count and widthsAccepts px, fr, %, auto, and other sizing values
grid-template-rowsSets row count and heightsAccepts the same sizing values
grid-gapSets row and column gapsShorthand for grid-row-gap and grid-column-gap
grid-column-start/endSets an item's starting and ending column linesAccepts line numbers and the span keyword
grid-row-start/endSets an item's starting and ending row linesAccepts line numbers and the span keyword
grid-areaDefines the area an item occupiesgrid-area: row-start / col-start / row-end / col-end
justify-itemsAligns items horizontallystart, center, end, stretch
align-itemsAligns items verticallystart, center, end, stretch
justify-selfAligns one item horizontallySame alignment values as above
align-selfAligns one item verticallySame alignment values as above

❓ Knowledge Check

Q1: What is the main difference between Grid and Flexbox?

Grid is two-dimensional and controls rows and columns together. Flexbox is one-dimensional and arranges items along a row or column. Each suits different layout needs.

Q2: What is the fr unit?

fr represents a fraction of the grid container's available space. A track sized at 1fr receives one share, making it easy to distribute space proportionally.

Q3: How can an item span multiple rows and columns?

Set its grid-column-start/end and grid-row-start/end properties, or use the grid-area shorthand.


🧪 Exercises

Exercise 1: Create a Four-Column Responsive Grid

Use Grid to create four equal-width columns with 15px gaps and a fixed height of 150px.

<div class="grid-container">
    <div class="grid-item">1</div>
    <div class="grid-item">2</div>
    <div class="grid-item">3</div>
    <div class="grid-item">4</div>
    <div class="grid-item">5</div>
    <div class="grid-item">6</div>
    <div class="grid-item">7</div>
    <div class="grid-item">8</div>
</div>

<style>
    .grid-container {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        grid-gap: 15px;
        height: 150px;
    }
    .grid-item {
        background-color: #9bf6ff;
        border: 1px solid #333;
        text-align: center;
        line-height: 150px;
        font-weight: bold;
    }
</style>

Copy the code into your editor and preview the result!


Exercise 2: Create a Card Layout with a Two-Column Card

Build a card list where the first card spans two columns and each remaining card occupies one column.

<div class="card-container">
    <div class="card card-large">Large Card</div>
    <div class="card">Card 1</div>
    <div class="card">Card 2</div>
    <div class="card">Card 3</div>
    <div class="card">Card 4</div>
</div>

<style>
    .card-container {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        grid-gap: 12px;
    }
    .card {
        background-color: #b5ead7;
        border: 1px solid #333;
        padding: 20px;
        text-align: center;
        font-size: 18px;
    }
    .card-large {
        grid-column: span 2;
        background-color: #ffcbf2;
    }
</style>

Copy the code into your editor and preview the result!


🎉 You have learned CSS Grid fundamentals, core properties, common layout patterns, and how to choose between Grid and Flexbox. With practice, Grid will become a powerful tool for building page layouts!