Skip to main content

How to Make Equal-Width Columns with CSS Grid

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Two declarations put items in columns of the same width: display: grid on the element that holds them, and grid-template-columns: repeat(3, 1fr) for three equal columns. The items fill the columns row by row.

Syntax​

Syntax
<container> {
display: grid;
grid-template-columns: repeat(<count>, 1fr);
}
PartWhat it does
<container>The element that holds the items.
display: gridMakes it a grid container. Its children become the grid's items.
grid-template-columnsSets the grid's columns.
repeat(<count>, 1fr)<count> columns of 1fr each, such as repeat(3, 1fr) for three.
1frOne share of the free space. Columns with the same number of fr get the same width.

MDN's grid-template-columns page lists every value, and its flex value page says the fr unit represents a fraction of the leftover space in the grid container.

The Example​

The example is a menu of six drinks in three equal columns. A thin border on each item shows where the columns are.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule for the class drinks with display grid and grid-template-columns repeat 3 times 1fr, a rule giving the paragraphs inside a light grey border and no margin, and a div of class drinks with six paragraphs: Coffee, Tea, Juice, Water, Milk and Lemonade.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Grid columns</title>
<style>
.drinks {
display: grid;
grid-template-columns: repeat(3, 1fr);
}

.drinks p {
border: 1px solid #d1d1d1;
margin: 0;
}
</style>
</head>
<body>
<div class="drinks">
<p>Coffee</p>
<p>Tea</p>
<p>Juice</p>
<p>Water</p>
<p>Milk</p>
<p>Lemonade</p>
</div>
</body>
</html>

The second rule gives each item a border and removes the space a paragraph normally has above and below it, so the cells are easy to see.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: six bordered cells in two rows of three equal columns, Coffee, Tea and Juice, then Water, Milk and Lemonade.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The six drinks sit in three equal columns. In a window 480 pixels wide, each column was about 155 pixels wide.
  • The items filled the grid row by row: Coffee, Tea and Juice in the first row, then Water, Milk and Lemonade in the second. The grid added the second row by itself.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the comma: repeat(3 1fr)The value is invalid and ignored, so the grid had one column and the items stood one under the otherWrite repeat(3, 1fr)
Leaving out display: gridgrid-template-columns does nothing on its own: the items stayed one under the otherAdd display: grid to the same rule

Troubleshooting​

SymptomLikely causeFix
One column is much wider than the othersA long word in it: a 1fr column grew to fit Pneumonoultramicroscopicsilicovolcanoconiosis, and the columns became 75, 314 and 75 pixels wideUse repeat(3, minmax(0, 1fr))
All items are in one columnThe grid isn't on, or the repeat() value is invalidCheck display: grid and the comma in repeat()

With minmax(0, 1fr), the columns stayed 155 pixels each, and the long word ran on into the next column instead.

FAQ​

How do I make two or four columns?​

Change the count. With repeat(2, 1fr), the items sat in two columns.

Can the columns have different widths?​

Yes, give them different shares. MDN's flex value page shows 1fr 1fr 2.5fr 1.5fr, where the third column gets 2.5 shares of the free space.

Conclusion​

Put display: grid and grid-template-columns: repeat(<count>, 1fr) on the element that holds the items, and they line up in columns of the same width. Switch to minmax(0, 1fr) when long words must not stretch a column.

Additional Resources​