View video tutorial

CSS Height

CSS

The height property sets the height of an element.

CSS height property

➔ The height property is used to set the height of an element.

➔ The height property does not include padding, border, or margin, which means it determines the height of the element inside the padding, border, and margin.

➔ The height attribute can have one of the following values.

  • auto This is the default value and the browser automatically calculates the height of the element.
  • length Calculates the height in px, cm etc.
  • % Calculates the height as a percentage of the parent element.
  • initial Resets the height to its default value.
  • inherit The height will inherit from its parent element height.

Example

.p1 {
  height: 200px;
  border: 1px solid black;
  background-color: #96bcec;
}
Try it Now »

Click on the "Try it Now" button to see how it works.


Height in %

In percentage measurements, the child calculates from the parent unit.

Example

.p1 {
  height: 600px;
  border: 1px solid black;
  background-color: #959595;
}
.p2 {
  height: 50%;
  border: 1px solid black;
  background-color: #96bcec;
}
Try it Now »

Click on the "Try it Now" button to see how it works.


Vertically Center

Example

.p1 {
  position:relative;      
  height: 300px;
  border: 1px solid black;
  background-color: #959595;
}
.p2 {
  height: 20%;
  border: 1px solid black;
  background-color: #96bcec;
  margin:0;
  position:absolute;
  top: 50%;
  transform: translateY(-50%);
}
Try it Now »

Click on the "Try it Now" button to see how it works.


Vertically Center using flexbox

Example

.p1 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
  border: 1px solid black;
  background-color: #959595;
}
.p2 {
  height: 50%;
  border: 1px solid black;
  background-color: #96bcec;
  margin: 0;
}
Try it Now »

Click on the "Try it Now" button to see how it works.


Horizontally and Vertically Center

Example

.p1 {
  position:relative;
  height: 300px;
  border: 1px solid black;
  background-color: #959595;
}
.p2 {
  height: 20%;
  width:50%;
  border: 1px solid black;
  background-color: #96bcec;
  margin:0;
  position:absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
Try it Now »

Click on the "Try it Now" button to see how it works.