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.

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.
Tutorial
CSS3 Features
Syntax
Classes
ID Selectors
Attribute Selectors
Stylesheets
Inline
Internal
External
Box Model
Children Selectors
Pseudo Classes
Pseudo Elements
Variables
Counters
Text
Fonts
Web Fonts
Backgrounds
Background Images
Colors
Gradients
Opacity / Transparency
Shadow Effects
Transitions
Tooltip
Transform
Animations
Layout — Display
Layout — Position
Layout — Float
Layout — Clear
Layout — Horizontal & Vertical Align
Multiple Columns
Introduction
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Responsive Web Design — Videos
Borders
Margin
Padding
Width
Height
Outline
Links
Lists
Tables
Dropdown Menu
Navigation Bar
Images
Image Gallery
Border Images
Forms
Rounded Corners
Buttons
Box-Sizing
Selector Reference
Pagination Examples
Code Examples
CSS3 Browser Support Reference
Functions Reference
Speech Module Reference
Units
Web Safe Font Combinations
Cheat Sheet
:hover
@font-face
@keyframes
@media
align-content
align-items
align-self
all
animation
backface-visibility
background
background-clip
background-color
background-image
background-origin
background-position
background-size
border
border-image
border-radius
border-style
box-shadow
box-sizing
color
columns
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-weight
hanging-punctuation
justify-content
line-height
margin
offset
opacity
order
outline
overflow
padding
perspective
position
resize
tab-size
text-align
text-decoration
text-emphasis
text-transform
text-shadow
transform
transition-property
translate
vertical-align
word-break
word-wrap
z-index