Created
October 27, 2014 07:08
-
-
Save colintoh/62c78414443e758c9991 to your computer and use it in GitHub Desktop.
douchebag way of vertical alignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.element { | |
position: relative; | |
top: 50%; | |
transform: translateY(-50%); | |
} |
some light reading for everyone here... http://colintoh.com/blog/display-table-anti-hero
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 😄
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
I do this regularly.. Is there a reason why I shouldn't or are you just kidding around?