Skip to content

Instantly share code, notes, and snippets.

@colintoh
Created October 27, 2014 07:08
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save colintoh/62c78414443e758c9991 to your computer and use it in GitHub Desktop.
Save colintoh/62c78414443e758c9991 to your computer and use it in GitHub Desktop.
douchebag way of vertical alignment
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
@josephschmitt
Copy link

This works well a lot of the time, but just be aware that since css transforms are performed on layers on the GPU, the calculations it returns will not necessarily land on even pixels. When that happens you're going to get blurry text and edges.

If you know the height of the element you're centering, it's better to use a negative top margin that is half the height. That will always round to the nearest pixel. Or just cave and use flexbox 😄

@mehas
Copy link

mehas commented Jun 28, 2016

To prevent the half-pixel issue, set this on the parent element:

  transform-style: preserve-3d;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment