CSS Center Vertically

CSS Center Vertically

.center {
  padding: 70px 0;
  border: 3px solid green;
}
//to center both vertically and horizontally, use padding and text-align: center

.center {
  padding: 70px 0;
  border: 3px solid green;
  text-align: center;
}
//using line-height property

.center {
  line-height: 200px;
  height: 200px;
  border: 3px solid green;
  text-align: center;
}

/* If the text has multiple lines, add the following: */
.center p {
  line-height: 1.5;
  display: inline-block;
  vertical-align: middle;
}
//using transform

.center { 
  height: 200px;
  position: relative;
  border: 3px solid green; 
}

.center p {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
  • There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding.
  • Another trick is to use the line-height property with a value that is equal to the height property.
  • If padding and line-height are not options, a third solution is to use positioning and the transform propert.

Related concepts

CSS Center Vertically — Structure map

Clickable & Draggable!

CSS Center Vertically — Related pages: