liveBook · Manning

Height property

As explained in CSS in Depth, Second Edition

The height property specifies the vertical extent of an element's content box or other applicable box.

The height property controls an element’s vertical size, but normal document flow usually lets content determine that size. Width is constrained by the parent; height grows from the children. Explicit heights can therefore cause overflow, especially when content changes. section 3.3 section 3.1

Choose the sizing strategy according to the layout problem:

  • Avoid explicit heights on content-heavy elements unless a fixed vertical constraint is intentional. section 3.3 section 3.7
  • Treat percentage heights cautiously: the containing block needs an explicitly defined height, or the browser may ignore the declaration. section 3.3.2
  • Use viewport-relative units such as 100svh or 100lvh when the goal is to fill the screen. section 3.3.2
  • Let flexbox or grid solve equal-height columns instead of forcing sibling heights with percentage values. section 3.3.2

Animating a dropdown drawer

A drawer can close with height: 0 and overflow: hidden, but height: auto cannot transition from a numeric length. JavaScript can read scrollHeight when the drawer is measurable, assign that pixel height when opening, and restore height: 0 when closing. Keep the transition in CSS while coordinating the state change with JavaScript. section 15.4

Listing 15.11 Setting the height explicitly so the transition works
<script type="module">
  var toggle = document.getElementsByClassName("dropdown__toggle")[0];
  var dropdown = toggle.parentElement;
  var drawer = document.getElementsByClassName("dropdown__drawer")[0];
  var height = drawer.scrollHeight;              #1

  toggle.addEventListener("click", function () {
    dropdown.classList.toggle("is-open");
    if (dropdown.classList.contains("is-open")) {
      drawer.style.setProperty("height", height + "px");     #2
    } else {
      drawer.style.setProperty("height", "0");    #3
    }
  });
</script>

Inspect the JavaScript pattern that measures scrollHeight, assigns an explicit opening height, and returns the drawer to zero on close. section 15.4

For positioned elements, height may supplement selected top, right, bottom, or left offsets; omitting height allows content to determine the size naturally. Fixed elements leave document flow, so surrounding content may need a compensating margin. section 6.1.3

How CSS in Depth, Second Edition covers it

How deep it goes
Explains content-driven sizing, percentage-height failure, positioned elements, and JavaScript-assisted transitions, stopping short of full layout internals.
Examples
Uses full-screen containers, equal-height columns, fixed navigation, and a dropdown transitioning between zero and measured height.
What it assumes
Assumes basic CSS and HTML, while establishing normal flow, parent-child sizing, and the containing-block requirement from fundamentals.
What you can do after
Apply height safely in CSS and coordinate explicit pixel values with JavaScript when animating content-dependent dimensions.
How it connects
Connects height with normal document flow, the box model, viewport units, positioning, flexbox, grid, transitions, and JavaScript.

Also explained in

Compare how 4 books explain height property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.