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

337
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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