Code Snippet
Exactly Center an Image/Div Horizontally and Vertically
.center {
width: 300px;
height: 300px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -150px;
}
Negative margins are exactly half the height and width, which pull the element back into perfect center. Only works with elements of a fixed height/width.
This doesn’t work on IE…
works in IE 8 for me.
I’d use display:inline-block for that. Works much better (dynamic vertical and horizontal dimersions for both container and content).
So, what do I do with the HTML?
Seriously I lol’d. Make a div with class center.
Excellent, thank you. No troubles here.. works well in IE, FF, etc.
Ugh! I’m really having trouble getting a popup div to center vertically on mobile devices. When I’m scrolled half way down the screen and click the link button, the popup appears up top where I cannot see it. Any suggestions?
Cool! This is just what I was looking for (I’m not lying.)!
I’ve been using this, but… let’s say you resize your window to like 100px wide. Then content starts to get cut off on the left side and you can’t scroll to it. Is there a fix for this?
Thanks you. But I am just beginning of web design.
Thanks Chris, works perfect for me! :)
Very nice solution. This helped me center a “loading” image within a fluid width container. Worked like a charm.
this doesn’t work on iOS, but all good for cross browsers for me…why?!?
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.