5

I am learning CSS Grid and trying to reproduce some flex behaviour (I know that both are complementary, this is a learning exercise).

The flex example below allows for an undefined number of divs to be stacked (undefined in the sense that their number does not impact the CSS part)

#flex {
  height: 100px;
  display: flex;
  flex-direction: row;
}

#flex > div {
  background-color: blue;
}
<div id="flex">
  <div>a</div>
  <div>b</div>
  <div>c</div>
</div>

I tried to port this to a CSS Grid implementation:

/* updated with correct comment delimiters, thanks @vals */

#grid {
  height: 100px;
  display: grid;
  grid-auto-flow: column;  /* when something has to be added, be it a column */
  grid-auto-columns: fit-content; /* when a new column is added, its size will be driven by the content. Unfortunately this does not work yet but never mind for now */
  grid-row: auto; /* one row, auto-sized */
}

#grid > div {
  background-color: lime;
}
<div id="grid">
  <div>a</div>
  <div>b</div>
  <div>c</div>
</div>

The fact that the width of the cells is not as expected (per the comments) is something I will investigate later, my main problem is that there are three extra columns, one before each of the expected ones.

When inspecting the grid, I can see that some of the lines are plain, while others are dotted (I am not sure whether this is an indication of something):

enter image description here

Are the blanks extra columns?

  • if yes: where do they come from?
  • if not: are they some kind of padding, and why isn't the column taking all the available space then?
TylerH
  • 20,799
  • 66
  • 75
  • 101
WoJ
  • 27,165
  • 48
  • 180
  • 345
  • ` grid-auto-columns: fit-content; ` does not exist you mean `grid-auto-columns: max-content;` Than it also looks the same :) grid-row: auto also does not exist – Doomenik Jan 04 '18 at 10:40
  • Not sure about why, but you have nbsp in your HTML. Remove them and you are ok. – vals Jan 04 '18 at 10:44
  • @Doomenik: thanks, yes, I had `max-content`in mind (but `fit-content`exists: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-columns). Also `grid-row: auto` exists (https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row) – WoJ Jan 04 '18 at 11:37

2 Answers2

1

In your container code:

#grid {
  height: 100px;
  display: grid;
  grid-auto-flow: column;  
  grid-auto-columns: fit-content; 
  grid-row: auto;
}

The grid-row rule is having no effect. This property applies only to grid items.

You may be wanting grid-template-rows or grid-auto-rows, which apply to grid containers.

Also, before editing your question, you were using an invalid form of comments in your CSS. That was causing a syntax error.

The problem is explained here:

TylerH
  • 20,799
  • 66
  • 75
  • 101
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

#grid {
  height: 100px;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: max-content; 
}

#grid > div {
  background-color: lime;
}
<div id="grid">
  <div>a</div>
  <div>b</div>
  <div>c</div>
</div>

At grid-auto-columns: fit-content;, you probably meant max-content. grid-row is unnecessary.

Also, as mentioned by @vals in the comments, you had &nbsp; line breaks.

A good resource to read about grids is at CSS Tricks.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Doomenik
  • 868
  • 1
  • 12
  • 29
  • Thanks for the cleanup. As I mentioned in a comment, `fit-content` and `grid-row: auto` are described in the Mozilla docs (https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row and https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-columns). – WoJ Jan 04 '18 at 11:39
  • @WoJ strange cause mozillas browser does not provide them as autocomplete. Anyway removed it from my answer – Doomenik Jan 04 '18 at 11:57
  • @WoJ did I answered your question ? – Doomenik Feb 02 '18 at 06:49