Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatic row spanning in css grid

Tags:

css

css-grid

Is there a way to get automatic row spanning (item occupies automatic number of vertical grid cells)?

In my example, I'd like cell 8 to span 2 (or more) cells automatically, without having to explicitly say span 2. I've been looking high and low how to do this, but it doesn't seem possible.

I'd like to have each cell a multiple of, say 100px, so that the smallest cell is 100px, if the content overflows it becomes 200px tall, and with more content 300px, etc.

In other words I'm hoping there is a way the css grid can determine XX automatically:

.i {  grid-row: span XX; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 2fr));
  grid-auto-flow: dense;
}

.grid div {
  border-left: 1px solid #000;
  border-top: 1px solid #000;
  padding: 10px;
}

.grid div.i {
  grid-row: span 2;
}
<div class="grid">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
  <div class="i">8aslkdj ljsa lkj dasljdks lajsld jlasdl sldjslda ld jla8aslkdj ljsa lkj dasljdks lajsld jlasdl sldjslda ld jla8aslkdj ljsa lkj dasljdks lajsld jlasdl sldjslda ld jla8aslkdj ljsa lkj dasljdks lajsld jlasdl sldjslda ld jla</div>
  <div>9</div>
  <div>10</div>
  <div>11</div>
  <div>12</div>
  <div>13</div>
  <div>14</div>
  <div>15</div>
  <div>16</div>
  <div>17</div>
</div>

https://codepen.io/joe_g/pen/VbLRpN

like image 427
joe_g Avatar asked Apr 17 '17 14:04

joe_g


People also ask

What is grid auto rows in CSS?

The grid-auto-rows property sets a size for the rows in a grid container. This property affects only rows with the size not set.

How does auto placement in CSS Grid work?

Auto-placement by 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. As with implicit row tracks, these column tracks will be auto sized.

How do you span grid items?

Note: The grid-column property is a shorthand property for the grid-column-start and the grid-column-end properties. To place an item, you can refer to line numbers, or use the keyword "span" to define how many columns the item will span.

What is span in CSS Grid?

With span , you set the starting line and then how many columns to span into from that starting point. In this case, grid-column: 1 / span 12 would be equivalent to grid-column: 1 / 13 , and grid-column: 2 / span 6 would be equivalent to grid-column: 2 / 8 . .child-span-12 {


1 Answers

No, this is not currently possible with CSS grid. You’ll have to continue use the span keyword.

You may have to use JavaScript to detect long content and dynamically add a class to the grid items that have more content.

like image 157
keithjgrant Avatar answered Dec 11 '22 09:12

keithjgrant