How to span a grid cell across all dynamic rows? [duplicate]

…衆ロ難τιáo~ 提交于 2020-12-11 06:08:40

问题


The attached example works properly, the first child of .container is spanned across all rows, but the problem is that the grid-row-end value is dependent on a number of children of div.container. Is it possible to do it without the magic constant (4) and WITHOUT changing an HTML structure. Unfortunately using grid-row-end:-1 is possible only for the explicit grids.

    .container {
        display:grid;
        grid-gap: 0.4em;
    }
    .container > * {
        background-color: lightgray;
    }
    .container *:first-child {
        grid-column: 1;
        grid-row-start: 1;
        grid-row-end: 4;
    }
    .container *:not(:first-child) {
        grid-column: 2;
    }
 
<div class="container">
    <div>IMG</div>
    <div>A</div>
    <div>B</div>
    <div>...</div>
</div>

回答1:


This was asked already Make a grid item span to the last row / column - Roko C. Buljan

if you remove the gap propertie and instead dispatch margin on the children (standing in the second column and from the second row, you can set a hudge value that the possible numbers will never match or be at the most the same amount of actual rows of the grid – G-Cyrillus

the linked question did not take into account the gap set.

snippet drawing of my comment:

.container {
        display:grid;  
    }
    .container > * {
        background-color: lightgray;
    }
    .container *:first-child {
        grid-column: 1;
        grid-row-start: 1;
        grid-row-end: 100;/* use a value that will over the expected numbers of row */
        grid-gap:0; /* empty rows will collapse to none height */
        grid-auto-rows:auto; /* do not give an height here , else empty rows will use space */
        margin-right:0.2em;
    }
    .container :nth-child(2)   {
        margin-left:0.2em;
        grid-column:2;
    }
    .container :nth-child(2) ~ div  {
        grid-column:2;
        margin-top:0.4em;
        margin-left:0.2em;
    }
<div class="container">
    <div>IMG</div>
    <div>A</div>
    <div>B</div>
    <div>...</div>
    <div>...</div>
    <div>...</div>
    <div>...</div>
    <div>...</div>
    <div>...</div>
    <div>...</div>
</div>

Edit

If javascript is okay with you

let rowsToSpan = document.querySelectorAll(".container>*");
rowsToSpan[0].style.gridRowEnd = rowsToSpan.length
.container {
  display: grid;
  grid-gap: 0.4em;
}

.container>* {
  background-color: lightgray;
}

.container *:first-child {
  grid-column: 1;
  grid-row-start: 1;
  /*grid-row-end: 4;*/
}

.container *:not(:first-child) {
  grid-column: 2;
}
<div class="container">
  <div>IMG</div>
  <div>A</div>
  <div>B</div>
  <div>...</div>
  <div>...</div>
  <div>...</div>
  <div>...</div>
  <div>...</div>
  <div>...</div>
</div>

inspired from https://codepen.io/gc-nomade/pen/pRYPwK



来源:https://stackoverflow.com/questions/65091323/how-to-span-a-grid-cell-across-all-dynamic-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!