I have had to refer to this tiny bit of code so many times, I just never seem to remember it!

As the title suggests it vertically or center aligns something. Depending on your requirements you might need to use absolute positioning.

.element {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

You can also center align..

.element {
  left: 50%;
  position: relative;
  transform: translateX(-50%);
}

Or do both, I use shorthand translate here…

.element {
  left: 50%;
  position: relative;
  top: 50%;
  transform: translate(-50%, -50%);
}

Worthy of Note

Worthy of Note is a site aimed at Web Designers & Developers. It offers a wide range of resources to help assist anyone in the web industry.

View