Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

max-height and min-height:

Definition:

  • max-height sets the maximum height an element can grow to.
  • min-height sets the minimum height an element must be, even if its content is smaller.

Syntax:

selector {
  max-height: value;
  min-height: value;
}
/*value can be in units like px, %, em, rem, etc.*/

Example:

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .tall-box {
      min-height: 100px;
      max-height: 200px;
      background-color: lightcoral;
      border: 2px solid maroon;
      overflow: auto;
      padding: 10px;
      margin: 20px;
    }
  </style>
  <title>Max and Min Height Example</title>
</head>
<body>
  <div class="tall-box">
    This box has a minimum height of 100px and a maximum height of 200px. If the content exceeds 200px, scroll will appear.
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla commodo, nisi at fringilla convallis, mauris justo porta orci, vitae pulvinar velit velit sed justo.</p>
    <p>Additional content to exceed max-height.</p>
  </div>
</body>
</html>