The best solution I’ve seen for both vertically and horizontally centering a position: fixed
div
is:
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
Fiddle and source. You don’t need to know the dimensions of the div
and it doesn’t require any container div
s. The centered div
‘s width can even be a percentage (which was what I was looking for when I found this solution).