iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Grid Intro

CSS Grid is a two-dimensional layout system. You declare a grid of rows and columns, then drop children into specific cells (or let them flow into the next available one).

Tracks, lines, and cells

A B C D E F G H I grid-template-columns: 1fr 1fr 1fr — three equal column tracks
Fig 1. Three column tracks × three row tracks = nine cells.

Key properties

PropertyGoes on…Purpose
display: gridContainerTurn the element into a grid.
grid-template-columnsContainerDefine column tracks. repeat(3, 1fr) = three equal columns.
grid-template-rowsContainerDefine row tracks. Often left to auto.
gapContainerSpace between tracks. Replaces grid-gap.
grid-columnItemWhich columns the item spans, e.g. 1 / 3 or span 2.
grid-rowItemWhich rows the item spans.
Tip: The fr unit means "fraction of free space". 1fr 2fr gives the second column twice as much room as the first.

Example

Example
<!DOCTYPE html>
<html>
<head>
<style>
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.grid > div { background: #2965F1; color: #fff; padding: 24px; text-align: center; }
</style>
</head>
<body>

<div class="grid">
    <div>A</div><div>B</div><div>C</div>
    <div>D</div><div>E</div><div>F</div>
</div>

</body>
</html>
Try it Yourself »

Exercise

Create three equal columns with one rule.

.grid { display: grid; grid-template-columns: (3, 1fr); }

Test yourself

Q1. Which unit means "fraction of remaining space"?
Q2. How do you create three equal columns?
Q3. Which property goes on a grid item (not the container)?

Discussion

Loading…