Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a grid item span to the last row / column in implicit grid

Tags:

html

css

css-grid

Is it possible to make a grid item span from the first to the last row when I don't know the number of rows?

Lets say I have the following html with an unknown number of boxes.

How can I make the third .box span from the first grid-line to the last?

.container {    display: grid;    grid-template-columns: repeat(3, minmax(10rem, 1fr)) [last-col] 35%;    grid-template-rows: auto [last-line];  }    .box {    background-color: blue;    padding: 20px;    border: 1px solid red;  }    .box:nth-child(3) {    background-color: yellow;    grid-column: last-col / span 1;    grid-row: 1 / last-line;  }
<div class="container">    <div class="box"></div>    <div class="box"></div>    <div class="box">3</div>    <div class="box"></div>    <div class="box"></div>    <div class="box"></div>    <div class="box"></div>    <div class="box"></div>    <div class="box"></div>    <div class="box"></div>    <div class="box"></div>  </div>
like image 457
jbe Avatar asked May 18 '17 15:05

jbe


People also ask

Which CSS rule will you use to place a grid item into the grid so that it starts from the second column and second row and spans three columns and two rows?

Auto-placement by column Using the property grid-auto-flow with a value of column . In this case grid will add items in rows that you have defined using grid-template-rows . When it fills up a column it will move onto the next explicit column, or create a new column track in the implicit grid.

What does grid column end do?

The grid-column-end CSS property specifies a grid item's end position within the grid column by contributing a line, a span, or nothing (automatic) to its grid placement, thereby specifying the block-end edge of its grid area.


1 Answers

Is it possible to make a grid item span from the first to the last row when I don't know the number of rows?

A natural Grid solution to this problem appears to be missing in the current spec (Level 1). So the answer would be "no", strictly with Grid properties.

However, as pointed out in this answer, it may be possible with absolute positioning.


While CSS Grid cannot make a grid area span all columns / rows in an implicit grid, it can do the job in an explicit grid.

Use negative integers.

Here are two interesting sections in the CSS Grid specification:

7.1. The Explicit Grid

Numeric indexes in the grid-placement properties count from the edges of the explicit grid. Positive indexes count from the start side, while negative indexes count from the end side.

and here...

8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties

If a negative integer is given, it instead counts in reverse, starting from the end edge of the explicit grid.

In other words, when dealing with an explicit grid, which is a grid that you define using these properties:

  • grid-template-rows
  • grid-template-columns
  • grid-template-areas

... you can make a grid area span all columns by setting this rule:

grid-column: 3 / -1; 

That tells the grid area to span from the third column line to the last column line.

The reverse would be:

grid-column: 1 / -3; 

Again, this method works only in explicit grids.

like image 73
Michael Benjamin Avatar answered Sep 19 '22 03:09

Michael Benjamin