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-height
overrides theheight
property, butmin-height
overridesmax-height
.
- 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
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified 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.
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: 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.