Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!