Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
Setting a dynamic number of rows/ columns in css grid (revised)

From my research, it seems to appear that best way to set the final value of a CSS grid is to either specify it, or create/manipulate the css :root value.

A very similar question was asked here but these answers are now several years old. Ducks answer looks the best by using a "nested grid", although manipulating the :root variable seems cleaner.

below is a working example of what i am trying to achieve. i am creating a random value and setting the CSS grid-row-end value accordingly using a css variable --ending upon completion.

does the CSS grid now have a way to do this, something like a css-row-end: max?

'use strict;'
    const maxNbrOfRows = 6;
    window.onload = () => {
        const nbrOfRows = Math.floor(Math.random() * maxNbrOfRows) + 1;
        for ( let i=1; i <= nbrOfRows; i++ )    {
            let tag = document.createElement('div');
            let text = document.createTextNode('row ' + i.toString() );
            tag.appendChild(text);
            tag.classList.add('box');
            tag.style.gridColumnStart = '2';
            let element = document.getElementById('grid');
            element.appendChild(tag);
        }
             // hack the css :root value
        document.styleSheets[0].insertRule(':root { --ending: ' + nbrOfRows.toString() + '; }');
    }
    .wrapper {
      display: grid;
      grid-template-columns: 150px 100px;
      grid-gap: 10px;
      background-color: #fff;
      color: #444;
    }

    .box {
      background-color: #444;
      color: #fff;
      border-radius: 5px;
      padding: 20px;
      font-size: 151%;
    }
    <div id='grid' class="wrapper">
        <div style='grid-row-end: span var(--ending);' class="box">First Col</div>
    </div>

Has css-grid addressed this yet?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

:root was being manipulated in other solutions, however, this appears to be a working solution (below).
There should be some CSS way of doing this, but this seems clean enough.

document.documentElement.style.setProperty('--ending', nbrOfRows.toString() );

'use strict;'
    const maxNbrOfRows = 8;
    window.onload = () => {
        const nbrOfRows = Math.floor(Math.random() * maxNbrOfRows) + 1;
        for ( let i=1; i <= nbrOfRows; i++ )    {
            let tag = document.createElement('div');
            let text = document.createTextNode('row ' + i.toString() );
            tag.appendChild(text);
            tag.classList.add('box');
            tag.style.gridColumnStart = '2';
            let element = document.getElementById('grid');
            element.appendChild(tag);
        }
        document.documentElement.style.setProperty('--ending', nbrOfRows.toString() );
        // stupid!  document.styleSheets[0].insertRule(':root { --ending: ' + nbrOfRows.toString() + '; }');
    }
    .wrapper {
      display: grid;
      grid-template-columns: 150px 100px;
      grid-gap: 10px;
      background-color: #fff;
      color: #444;
    }

    .box {
      background-color: #444;
      color: #fff;
      border-radius: 5px;
      padding: 20px;
      font-size: 151%;
    <div id='grid' class="wrapper">
        <div style='grid-row-end: span var(--ending);' class="box a">First Col</div>
    </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda