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

335
Views
responsive div height on conditional rendering?

I am working on a react project, where I want to conditionally render a div above an existing div that currently covers the screen with an image. I would like the existing div to reduce in height, by shrinking the size of the image, when the second div conditionally renders. The second div can have a varied height. ( e.g. list of items).

This is simplified version of what I have in my App so far:

function App() {
  const [show, setShow] = useState(false);
  return (
    <div
      style={{
        border: "solid",
        width: "100%",
        maxHeight: "100vh"
      }}
    >
      {show && (
        <div>
          <div style={{ height: "300px", backgroundColor: "red" }}></div>
          <button
            onClick={() => {
              setShow(false);
            }}
          >
            Close
          </button>
        </div>
      )}
      <div
        style={{
          display: "flex",
          justifyContent: "center"
        }}
      >
        <img
          src="https://i.imgur.com/LiAUjYw.png"
          alt="test"
          style={{
            maxWidth: "100%",
            height: "auto"
          }}
        />
      </div>
      <button onClick={() => setShow(true)}>SHow</button>
    </div>
  );
}

It initially looks like this: https://imgur.com/a/R0e74Xk

And on clicking the 'Show' button, it looks like this: https://imgur.com/a/Iy6osio

As you can see the bottom div gets shifted down, and goes over the container div.

I was wondering how I can make the div responsive enough to change its height when the other element is rendered.

I am fairly new to styling so any help on this would be greatly appreciated.

Thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here's one simple idea. See how the style is given to the divs like so:

<div
    style={{
        display: "flex",
        justifyContent: "center"
    }}
>

Note how the style has this weird {{ }} notation. That's because style accepts an object, and the object here is:

{
    display: "flex",
    justifyContent: "center"
}

You could take that object and save it as a variable somewhere, for example:

const styleWhenShow = {
    display: "flex",
    justifyContent: "center",
    height: "100px"
}

const styleWhenNoShow = {
    display: "flex",
    justifyContent: "center",
    height: "500px"
}

Since you already have your show state, now it's just a matter of replacing the style prop with:

style={show ? styleWhenShow : styleWhenNoShow}

In case you don't want to repeat some common styles between the show and noshow styles, you can also do something like:

const commonStyle = {
    display: "flex",
    justifyContent: "center"
}

const styleWhenShow = {
    height: "100px"
}

const styleWhenNoShow = {
    height: "500px"
}

And set the style prop like:

style={show ? { ...commonStyle, ...styleWhenShow } : { ...commonStyle, ...styleWhenNoShow }}
about 4 years ago · Juan Pablo Isaza Report
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!