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:
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:
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.
div {
height: 320px;
width: 600px;
max-height: 150px;
background-color: rebeccapurple;
text-align: center;
border: solid 2px;
color: white;
}
Note: CSS
max-heightoverrides theheightproperty, butmin-heightoverridesmax-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.
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%andheight: autois that the percentage value gives the element the height of its parent.autoheight is flexible as it is based on the height of its children elements. - CSS
heightdoes not work on inline elements, column groups, and table columns.