2

Problem

I have content with some unknown height. For the purposes of example, call this height

DUMMY_CONTENT_HEIGHT

I want users to press a button, and have the content collapse to a known smaller height,

MINIMIZED_CONTENT_HEIGHT

I implemented this (I need to use a grid for my project),

import { useState } from "react";
export default function App(props) {
  const [minimized, setMinimized] = useState(false);

  const DUMMY_CONTENT_HEIGHT = 100;
  const MINIMIZED_CONTENT_HEIGHT = 20;
  const NORMAL_CONTENT_HEIGHT = "100%";

  return (
    <div style={{ display: "grid" }}>
      <button
        onClick={(e) => {
          setMinimized(!minimized);
        }}
        style={{ gridRow: "1", gridColumn: "1" }}
      >
        CLICK ME
      </button>
      <div
        style={{
          gridRow: "2",
          gridColumn: "1",
          overflow: "hidden",
          transition: "all 1s ease",
          maxHeight: minimized
            ? MINIMIZED_CONTENT_HEIGHT
            : NORMAL_CONTENT_HEIGHT
        }}
      >
        <div
          style={{ height: DUMMY_CONTENT_HEIGHT, backgroundColor: "rgb(255 0 0)" }}
        ></div>
      </div>
      <div
        style={{
          gridRow: "3",
          gridColumn: "1",
          height: 50,
          backgroundColor: "rgb(50 0 0)",
        }}
      ></div>
    </div>
  );
}

Edit sweet-perlman-u8vb17

Issue

The problem is I want the lower div to get "carried along" with the upper div, so that there is no whitespace between them.

Note that setting NORMAL_CONTENT_HEIGHT to some large value (like 10000) works, but the animation is inconsistent with different CONTENT_HEIGHTs (and is not a solution).

How do you transition with no whitespace between the upper and lower divs, using css only?

Or is this not possible?

Mondo Duke
  • 185
  • 8
  • Possible duplicate: [How can I transition height: 0; to height: auto; using CSS?](https://stackoverflow.com/q/3508605). – InSync Apr 20 '23 at 08:16
  • The 'correct answer' in that post is to provide the height of the content in pixels. As I mentioned in my question, I don't have access to the content height. Unless there's an answer hidden somewhere in that post, I don't this it's a duplicate. – Mondo Duke Apr 20 '23 at 21:54

0 Answers0