Code Snippet
Absolute Center (Vertical & Horizontal) an Image
CSS background-image Technique:
html {
width:100%;
height:100%;
background:url(logo.png) center center no-repeat;
}
CSS + Inline Image Technique:
img {
position: absolute;
top: 50%;
left: 50%;
width: 500px;
height: 500px;
margin-top: -250px; /* Half the height */
margin-left: -250px; /* Half the width */
}
Table technique:
html, body, #wrapper {
height:100%;
width: 100%;
margin: 0;
padding: 0;
border: 0;
}
#wrapper td {
vertical-align: middle;
text-align: center;
}
<html>
<body>
<table id="wrapper">
<tr>
<td><img src="logo.png" alt="" /></td>
</tr>
</table>
</body>
</html>
It’s also possible to do it using div’s and CSS without using px sizes or tables:
It’s explained here, but in spanish :(
explination using google page translator.
This is called “The dead center”, or am I wrong?
It’s explained in English here:
http://www.wpdfd.com/editorial/thebox/deadcentre4.html
This was exactly what I needed, thank you.
i was seeing the example, will these work (?) if the container has some margin top.
good one! but the problem is that if you re size the browser you can’t scroll to the top. i.e if i have a menu on top i cant view it!
Why does everyone in the universe write background shorthand wrong?
This is the correct way and the only way that works in ALL browsers that support background shorthand:
background: #FFF url() repeat fixed left top;
/rant
thanks, i agree with you, WHY
Craig, syntactically, you can have your properties in any arrangement:
background: #fff url() fixed left top;
is essentially the same as
background: url() fixed 0 0 #fff;
either way you look at it, it’s doing the same exact thing.
Thanks. This helped me a lot.
Brillant!!
Hi,
Thanks a lot frnd! :)
I was searching for the same thing from a long time and now I got the one…
Thanks all for the trick.. I was searching for that for some time..
YOU ARE A KING MAN
I would advice THIS PAGE for anyone whos searching for a solution regarding absolute centering of Image inside a DIV.
WOOOOW!
here is some div positioning i made some time ago, using this method.
HTML
CSS
#beers_food /* wrapper */
{
width:700px;
height:320px;
margin: 0 auto;
background-color:transparent;
position:relative;
}
#beer
{
position:absolute;
top:50%;
right:0;
margin-top:-115px;
width:220px;
height:230px;
text-align:center;
float:right;
background-image:url("../images/bottomBut.png");
border:1px solid #a73101;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius:20px ;
-moz-box-shadow: 5px 5px 5px #000000;
-webkit-box-shadow: 5px 5px 5px #000000;
box-shadow: 5px 5px 5px #000000;
}
#food
{
position:absolute;
top:50%;
left:50%;
margin:-115px 0 0 -110px;
width:220px;
height:230px;
text-align:center;
color:white;
float:right;
background-image:url("../images/bottomBut.png");
border:1px solid #a73101;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius:20px ;
-moz-box-shadow: 5px 5px 5px #000000;
-webkit-box-shadow: 5px 5px 5px #000000;
box-shadow: 5px 5px 5px #000000;
}
#shows
{
position:absolute;
top:50%;
left:0;
margin-top:-115px;
width:220px;
height:230px;
text-align:center;
float:left;
background-image:url("../images/bottomBut.png");
border:1px solid #a73101;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius:20px ;
-moz-box-shadow: 5px 5px 5px #000000;
-webkit-box-shadow: 5px 5px 5px #000000;
box-shadow: 5px 5px 5px #000000;
}
very handy.
Example
what happen when you have different image size but whatever the image sizes you want all them in center. normally in jquery gallery case. i am struggling with this one from last two days. any help will be highly appreciated.
ummm hmmm only javascipt can probably do that i think maybe? O.o
something like:
should center an image with id=”myimage”
given an absolute position of :
and if you wanted to do a bunch of them you would have to make an array maybe and cycle through them? O.o
anyone else?
well done! TY!
Hi there
May I know how you made this website? The layout is just awesome
???
What about a pure CSS solution for vertically centering an image in a div that is POSITION:FIXED
???
Thanks a lot!
Hi, I have posted about 50 post to my blog. Now I have to change my template blog with a new one. I have upload the picture too.
The problem with my new template is my picture that I have uploaded before its not in the position I wanted. I have to re-upload the picture so its fix to my new template. Is there any can help me with this. So the picture that I have upload before its force to (300X300 pixel). Thank you before.
What do you do when the size of the image is unknown?
Good attempt to make new people love css.Keep it up!
I LOVE YOU!! Ahem, thanks heaps. I was trying to figure out how to place a graphic using absolute position on a centered body container. I realized you made the graphic centered by ‘left: 50%’ and then using my container width I divided by 2 and altered the measurements to get a perfect fit ;). ‘left: 50%’ is a very important element when using absolute position. Dammit, if I only knew this before then I wouldn’t change my design and css code :(. Oh well, you live and learn :D
Thanks again and much appreciated.
CHEERS :)
ah – some very handy tricks which definitely helped me out. thanks for sharing!
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.