184

I'd like to have the html below showing in n equal columns whether there are two, or three, or more child elements to the row element using css grid - Flexbox makes this easy but I cannot get it done using css grid - any help is appreciated.

<div class="row">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
user1678736
  • 2,133
  • 4
  • 16
  • 10

11 Answers11

417

The common answer of repeat(3, 1fr) is not quite correct.

This is because 1fr is about the distribution of available(!) space. This breaks as soon as the content becomes bigger than the track size. By default, it does not overflow and adjust the column width accordingly. That's why not all 1fr are guaranteed to be of equal width. 1fr is actually rather just a shorthand for minmax(auto, 1fr).

If you really need the columns to be the exact same width you should use:

grid-template-columns: repeat(3, minmax(0, 1fr));

minmax(0, 1fr) allows the grid tracks to be as small as 0 but as large as 1fr, creating columns that will stay equal. But, be aware that this will cause overflows if the content is bigger than the column or cannot be wrapped.

Here is an example that demonstrates the difference.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
tcurdt
  • 14,518
  • 10
  • 57
  • 72
  • 4
    As far as I can tell, this is the only one that actually produces even-width columns. – James Baker May 26 '20 at 11:08
  • 5
    "This breaks as soon as the content becomes bigger than the track size." - very valuable notice. Thank you! – Takeshi Tokugawa YD Jun 23 '20 at 01:26
  • I came here because `repeat(3, 1fr)` did not give the expected results and your solution works perfectly. Thank you! – Rapti Aug 11 '20 at 11:34
  • @QMaster there is even a working codepen linked. Maybe explain what did not work for you? – tcurdt Oct 31 '20 at 01:58
  • 11
    When you don't know the number of columns you can instead do this: `grid-auto-columns: minmax(0, 1fr); grid-auto-flow: column;` – zauni Jan 13 '21 at 14:44
  • How can I make content wrap? – An Tran T. Aug 28 '21 at 05:39
  • 3
    @TrườngAn I think you can use word-wrap: break-word; https://www.w3schools.com/cssref/css3_pr_word-wrap.asp – Benny Chan Oct 12 '21 at 05:39
  • would you mind converting the codepen into [a stack snippet](https://meta.stackoverflow.com/q/358992/11107541)? – starball May 26 '23 at 07:11
  • The problem with this approach is it changes how entire grid is sized -- since `fr` is about distributing the space, the entire grid expands. – greenoldman Aug 20 '23 at 16:43
  • @greenoldman not sure I can follow. – tcurdt Aug 22 '23 at 01:09
  • Let's say your entire content takes 100px, while your screen is 2000px. Once you put "1fr", not only the space for cells will be distributed, but also grid will expand taking entire screen (on width) -- 2000px. I already found a solution, using inline-grid instead of grid :-). – greenoldman Aug 22 '23 at 14:04
125

@Michael_B's answer is almost there.

.grid-container {
   display: grid;
   grid-auto-columns: minmax(0, 1fr);
   grid-auto-flow: column;
}

Gives you one row of equally sized columns in Chrome, Firefox, and Safari as of writing.

wegry
  • 7,046
  • 6
  • 39
  • 59
48

Define this on your grid container. Sets up three columns of equal width.

grid-template-columns: repeat(3, 1fr);
TylerH
  • 20,799
  • 66
  • 75
  • 101
Kevin
  • 1,934
  • 1
  • 15
  • 10
17

Try this:

.grid-container {
   display: grid;
   grid-auto-columns: 1fr;
}

.grid-items {
   grid-row: 1;
}

Otherwise, here's a demo that may be useful: jsFiddle

To learn about the fr unit, see these posts:

TylerH
  • 20,799
  • 66
  • 75
  • 101
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 1
    that's still not working for me - columns take 100% of the width and are stacked one below the other - what am I missing? – user1678736 Dec 05 '17 at 15:20
  • @user1678736 Please mark answer as accepted (green check to the left from the answer) if it helped you. – Vadim Ovchinnikov Mar 05 '18 at 06:03
  • Exact same problem here: 4 boxes stacked on top of each other in stead of *next* to each other. – JvO Mar 11 '18 at 20:36
  • @user63457, what's the desired behavior. How about adding `grid-row: 1` to your grid items? Or how about this: https://jsfiddle.net/sk2jc1uy/2/ – Michael Benjamin Mar 20 '18 at 15:05
  • 1
    @Micheal_B The desired behaviour is as the original question describes. Adding grid-row:1 to the child elements looks like the correct solution, otherwise columns will be stacked. – user63457 Mar 21 '18 at 09:46
  • 2
    Yes, eventually I resorted to 4 times '1fr' to get the desired result (this design called for 4 columns only). One thing is sure though: 'auto' does not make the columns all the same width. – JvO Mar 22 '18 at 23:08
  • @user63457 adding `grid-auto-flow: column` removes the need to add properties to children. – wegry Dec 21 '18 at 12:17
  • Correct answer by @Kevin below. – Andrew Dec 17 '19 at 16:14
10

This allows the columns to distribute better, and the columns are the same size regardless of whether the size of the items does not adjust.

.row {
  display: grid;
  grid-template-columns: repeat( auto-fit, minmax(33.33%, 1fr) );
}
.item {
  grid-column: span 1;
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Alexis Vera
  • 357
  • 3
  • 10
8

The question asks for arbitrary number of columns, not 3! So use this:

.grid-container {
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
}
.grid-container > * {
  overflow: hidden;
}

This way, its children (selected using ">*" in css) do not need to have any specific class or even be div.

Example: https://codepen.io/dimvai/pen/RwVbYyz

Dim Vai
  • 726
  • 8
  • 14
5

How about this?

.row {
  display: grid;
  grid-template-columns: repeat(3, calc(100% / 3));
}
TDAK
  • 134
  • 1
  • 7
4

Full-Width with Responsive Wrapping

None of these answers were full width or wrapped to a new row when downsizing on mobile. This is probably what you are looking for if you want something similar to bootstrap. Note the 200px is the lower bound where wrapping to a new row will occur.

.grid-container {
    display: grid;
    width: 100%;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}
devdrc
  • 1,853
  • 16
  • 21
1

Here is simple answer(at least in my perspective). I got this issue above answers not helped me. Here the code to divide 'div' into equal width and with required number of columns.

//CSS

.grid-container {
  display: grid;
  grid-template-columns: auto auto auto; // no of 'auto's will be number of columns here it's 3
}

//HTML
<div class="grid-container">
    <div></div>
    <div></div>
    <div></div>
</div>

more info on grid can be seen here W3Schools

Himavan
  • 385
  • 3
  • 16
0

None of these answers worked for me, So I tried another way. In my case, item size is related to the content. Some contents are bigger than others, so all columns will not be equal. I just wrapped any item with another division that has 100% width and 100% height and that is working.

<div class="row">
    <div style="width: 100%; height: 100%;">
      <div class="item"></div>
    </div>
    <div style="width: 100%; height: 100%;">
      <div class="item"></div>
    </div>
    <div style="width: 100%; height: 100%;">
      <div class="item"></div>
    </div>
</div>

That is worked for me and I hope to help you.

QMaster
  • 3,743
  • 3
  • 43
  • 56
0

You can try to distribute each list to take all the space that the parent grid has for it, instead of allowing the grid to automatically distribute the space based on what each list has as content.

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.grid-container .list {
  width: 100%;
}
<div class="grid-container">
  <div class="list">a</div>
  <div class="list">b</div>
  <div class="list">c</div>
</div>
corn on the cob
  • 2,093
  • 3
  • 18
  • 29