min-height

The min-height property in CSS sets the minimum height of an element. This means the element cannot be smaller than the specified value, but it can be larger if its content requires it.

Syntax

element {
  min-height: value;
}
  • element: The HTML element you want to apply the min-height to.
  • value: The minimum height you want to set. This can be a length (vh, px, em, rem, etc.), a percentage, or auto.

Example

<div class="container">
  This content can be of varying height.
</div>
.container {
  min-height: 200px;
  background-color: lightblue;
}

In this example, the .container div will always be at least 200 pixels tall, regardless of its content.

Common Use Cases

  • Footers: Ensure the footer stays at the bottom of the page, even with short content.
  • Content sections: Maintain a minimum height for sections, even with limited content.
  • Layout columns: Keep columns at a minimum height for consistent layout.

Important Considerations

  • box-sizing: By default, min-height doesn’t include padding, border, or margin. To include them, use box-sizing: border-box;.
  • Overriding: min-height overrides both height and max-height if its value is greater.
  • Units: Choose appropriate units based on your design (px for fixed sizes, em or rem for relative sizes, vh or vw for viewport-based sizes).

Example with box-sizing

.container {
  min-height: 200px;
  background-color: lightblue;
  box-sizing: border-box; /* Include padding and border in min-height */
  padding: 20px;
  border: 1px solid black;
}

Additional Tips

  • Combine min-height with other properties like height and max-height for flexible layouts.
  • Use overflow: auto; to add scrollbars if content exceeds the min-height.
  • Consider using vh units for full-screen or viewport-based designs.

By understanding these points, you can effectively use min-height to create well-structured and responsive layouts.