🚨 Get Your Free NFT Certificate Mint by Completing the Web3 Exam! START NOW

Code has been added to clipboard!

Controlling CSS Height: Learn to Set Max-Height and Min-Height

Reading time 3 min
Published Sep 9, 2016
Updated Oct 2, 2019

TL;DR — The CSS height property sets the height of HTML elements. As a rule, height always describes the height of the content area. It is also possible to set the height of the border area when box-sizing has border-box value.

Exploring the height property

The CSS height controls the height of boxes. The property works with all values of length (percentages and length indicators). It does not accept negative values.

This example sets <div> height with 150 pixels and 600 pixels width:

This element has a height of 150 pixels and a width of 600 pixels.

 

Example
div {
    height: 150px;
    width: 600px;
    background-color: lavender;
}

CSS size can be calculated by browsers automatically. This option is possible if you set CSS height property to auto.

The CSS max-height and min-height override the settings of height.

Here is a list of CSS size properties for controlling height of elements:

Property Description
height Sets an element's height
max-height Specifies an element's maximum height
min-height Sets an element's minimum height

In the next example, we set the <div> height to 150 pixels and 50% width:

This element has a height of 150 pixels and a width of 50%.

 

Example
div {
    height: 150px;
    width: 50%;
    background-color: lavender;
}

Note: The width and height properties do not include border, padding, or margins. Instead, they set the area's parameters inside the border, padding, and margin of the element.

Max height

The CSS max-height property is for specifying the maximum height of elements. Rules of use indicate that the property works with all positive length values (percentages and length indicators).

The following example sets the <div> height. However, it is bigger than the specified max-height. As a result, max-height replaces height.

Example
div {
    height: 320px;
    width: 600px;
    max-height: 150px;
    background-color: rebeccapurple;
    text-align: center;
    border: solid 2px;
    color: white;
}

Note: CSS max-height overrides the height property, but min-height overrides max-height.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

Min height

The CSS min-height is for setting the minimum height of elements. It works with positive length values. Remember that min-height replaces both max-height and height.

The following example sets the height and max-height for <div>, but CSS min-height overrides both of these properties.

Example
div {
    height: 100px;
    width: 600px;
    max-height: 300px;
    min-height: 200px;
    background-color: rebeccapurple;
    text-align: center;
    border: solid 2px;
    color: white;
}

CSS height: useful tips

  • The difference between height: 100% and height: auto is that the percentage value gives the element the height of its parent. auto height is flexible as it is based on the height of its children elements.
  • CSS height does not work on inline elements, column groups, and table columns.