Set line-height as a percentage relative to the parent element

Here's another way to center an element vertically. I came across this technique some time ago. Basically it uses a pseudo element and vertical-align: middle.

.block::before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */
}

/* The element to be centered, can
   also be of any width and height */ 
.centered {
  display: inline-block;
  vertical-align: middle;
  width: 300px;
}

Since it's 2019 already, you could also use flexbox to achieve this :)

To do so, add the following classes to the parent element:

display: flex;
flex-direction: column;
justify-content: center;

See this Fiddle

Tags:

Css