tags:

views:

573

answers:

3

I have an image of variable width that I'd like to center in a container div, which will be centered on the page.

If I set a width on the div and give it a margin: 0 auto, it will center, but the problem is that the image inside of the div is of variable width, so I can't set a width on the container div.

Any suggestions?

Clarification: The container div has a background image that needs to expand 30px on either side of the image. Because of that, the container div needs to be of a set width but be able to expand/contract based on the image size.

A: 

Maybe setting image as background and center it would work?

Pawka
See my clarification above. Thanks.
James Skidmore
+1  A: 
img#foo {
  display:block;
  margin:0 auto;
}

An image element is a replaced element and its intrinsic width will allow it to be centered without an explicit width set.

meder
+5  A: 

add padding to the image!

img#foo{
  display:block;
  margin:0 auto;
  background:url(/image/bg.gif);
  padding: 30px;
}
Moak
Oh, good idea to just add that to the image. The `div` was kind of unnecessary I guess. Thanks Moak!
James Skidmore