Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to span to the last column in an implicit grid?

Tags:

css

css-grid

I want an item to start at the first column and end at the last column, no matter how many columns there are.

It should work like this:

item {
  grid-column-start: 1;
  grid-column-end: -1;
}

according to MDN:

Contributes the nth grid line to the grid item’s placement. If a negative integer is given, it instead counts in reverse, starting from the end edge of the explicit grid.

So this would only work on explicit grids? Why shouldn't it work on implicit grids?

like image 594
loominade Avatar asked Jul 08 '19 09:07

loominade


1 Answers

Why shouldn't it work on implicit grids?

Because we can easily run on undefined cases1 or a cyclic dependency. If it was the implicit grid, it means that we need to first place all the others element to identify the implicit grid then we place our element BUT if we place our element we will obtain another implicit grid so technically you cannot know the implicit grid without placing the element.

The idea behind the implicit grid is to place the element that doesn't have anything defined for their placement automatically after placing the ones with known places.


You can overcome this by using some hacks either for row or column:

Stretch an element to the end of the automatically calculated grid, not just the explicit grid

Forcing a column to be empty in a responsive grid layout


1 A basic example:

.grid {
  display: grid;
  grid-template-columns: 50px;
  grid-gap: 5px;
  grid-auto-flow: column;
  grid-auto-columns:50px;
}

.grid>span {
  height: 50px;
  background: red;
}

.grid>span.place {
  grid-column: 1 / -1;
  background: blue;
}
<div class="grid">
  <span></span>
  <span class="place"></span>
</div>

Logically we will first place the blue span in the explicit grid then the we place the red one automatically to obtain an implicit grid of 2 columns.

Considering your logic I have the following cases:

  1. I change the place of the blue so it takes the two columns which will move the red to a third one. I repeat again (an infinite loop)

  2. I change the place of the blue so it takes two columns and overlap the red one (not really logical and intuitive)

  3. I don't place the blue but I first place the red then I either overlap the red one (not logical) or I place the blue in that column and push the red to anoher one and I get back to (1)

like image 81
Temani Afif Avatar answered Sep 29 '22 12:09

Temani Afif