Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image in absolute position div [duplicate]

Tags:

html

css

Possible Duplicate:
How to make an image center (vertically & horizontally) inside a bigger div

I have a div which is with absolute position (width/height are 100%). Notice, not px, percents.
Inside this div I have an image. How do I center this image in this div?

like image 778
ruby_newbie Avatar asked Feb 03 '11 19:02

ruby_newbie


1 Answers

CSS:

/* where margin-left = {img width}/2, and margin-top= {img height}/2 */
.bigdiv {
   width:100%;
   height:100%;
   position:absolute;
}
.bigdiv img {
   position:absolute;
   left:50%;
   top:50%;
   margin-left:-10px;
   margin-top:-10px;
}

HTML:

<div class="bigdiv"><img src="eg.png" /></div>

You could also put your margin-left, margin-top commands as a style on the img tag instead (since they're unique per image).

like image 193
Rudu Avatar answered Sep 18 '22 05:09

Rudu