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

176
Vistas
Flex direction column and fill screen width

I have a list of elements of varying height and want to lay them out such that the full screen width gets filled (but not overflown with a horizontal scrollbar):

enter image description here

How can I accomplish this via CSS? I cannot simply switch to flex-direction: row as there would be large gaps between rows due to the varying heights of the elements.

Here is my hacky attempt via JS: https://codesandbox.io/s/dreamy-joliot-9n3woy?file=/src/App.js

import React, { useState, useEffect } from "react";

const BOX_WIDTH = 200;
const boxHeights = [...Array(500)].map(() => Math.random() * 100);

export default function App() {
  const ref = React.useRef();

  const [originalHeight, setOriginalHeight] = useState();
  useEffect(() => {
    if (!originalHeight && ref.current?.clientHeight) {
      setOriginalHeight(ref.current?.clientHeight);
    }
  }, [originalHeight, ref.current?.clientHeight]);

  const [height, setHeight] = useState();
  useEffect(() => {
    if (originalHeight && ref.current?.clientWidth) {
      setHeight(
        originalHeight / Math.floor(ref.current?.clientWidth / BOX_WIDTH) / 0.9
      );
    }
  }, [ref.current?.clientWidth, originalHeight]);

  return (
    <>
      <div
        ref={ref}
        style={{
          display: "flex",
          flexDirection: "column",
          flexWrap: "wrap",
          ...(height && { height })
        }}
      >
        {[...Array(500)].map((_, i) => (
          <div
            style={{
              border: "solid",
              width: BOX_WIDTH,
              height: boxHeights[i],
              margin: "10px"
            }}
          />
        ))}
      </div>
    </>
  );
}
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