Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

297
Vistas
How can i create dynamic grid layout which width changed dynamically as per their content length and adjust their positions?

I want to create a dynamic grid layout of 2 columns and any number of rows there. in this grid, I put the element's name. as per the element name's length the width of the grid going to adjust. like if the element length is less than 13 then element adjust in grid column ('1 / 2') or ('2 / 3') otherwise it take width ('1 / 3') of grid. and there is a condition is that in the grid layout there is no one going to blank.

I tried for that and I did the code but in some cases, there are some blank spaces. So try and share the optimum solution with me.

  const setGrid = (a) => {
  a.forEach((obj) => {
    if (obj.innerText.length > 13) {
      const parent = obj.parentNode;
      if (parent) {
        parent.style.gridColumn = "1 / 3";
      }
      const parentLeft = parent.previousSibling;
      if (parentLeft) {
        if (parentLeft.innerText.length < 13) {
          const parentLeftL = parentLeft.previousSibling;
          if (parentLeftL) {
            if (parentLeftL.innerText.length < 13) {
              const parentLeftLL = parentLeftL.previousSibling;
              if (parentLeftLL) {
                if (parentLeftLL.innerText.length < 13) {
                  parentLeftLL.style.gridColumn = "1/2";
                  parentLeftL.style.gridColumn = "2/3";
                  parentLeft.style.gridColumn = "1/3";
                } else {
                  parentLeftLL.style.gridColumn = "1/3";
                  parentLeftL.style.gridColumn = "1/2";
                  parentLeft.style.gridColumn = "2/3";
                }
              } else {
                parentLeftL.style.gridColumn = "1/2";
                parentLeft.style.gridColumn = "2/3";
              }
            } else {
              parentLeftL.style.gridColumn = "1/3";
              parentLeft.style.gridColumn = "1/3";
            }
          } else {
            parentLeft.style.gridColumn = "1/3";
          }
        } else {
          parentLeft.style.gridColumn = "1/3";
        }
      }
    }
  });
};

I want this kind of output

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda