467,880 Members | 1,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,880 developers. It's quick & easy.

Centering image in div

Hi,

I have a div with an image inside it. The image needs to be centered in
the div. I thought I could do this by setting the image's margin to
"auto auto", but it seems to do nothing. What am I missing here?

Thanks,
Aaron

P.S. Here are the exact styles I'm using:
..imageholder { width: 100px; height: 50px; border: 2px solid black;
float: right; margin-right: 20px;}
..cat-table td img {margin: auto auto;}

Jul 20 '05 #1
  • viewed: 6627
Share:
7 Replies
Aaron skrev:
I have a div with an image inside it. The image needs to be centered in
the div. I thought I could do this by setting the image's margin to
"auto auto", but it seems to do nothing. What am I missing here?


Use text-align: center

auto works only on block elements, not inline elements.

And if you want to use margin: auto on block elements, you will
have to set width on it.
--
Knud
Jul 20 '05 #2
Allright, I set the style for the image to {display: block; margin:
auto;}. Shouldn't this then center it in the div it's in? It does
center it horizontally, but it remains at the top vertically. Any ideas?

Thanks,
Aaron

Knud Gert Ellentoft wrote:
Aaron skrev:

I have a div with an image inside it. The image needs to be centered in
the div. I thought I could do this by setting the image's margin to
"auto auto", but it seems to do nothing. What am I missing here?

Use text-align: center

auto works only on block elements, not inline elements.

And if you want to use margin: auto on block elements, you will
have to set width on it.


Jul 20 '05 #3
Knud Gert Ellentoft wrote:
Use text-align: center


VERTICALLY
Jul 20 '05 #4
Els
Timo Nentwig wrote:
Knud Gert Ellentoft wrote:
Use text-align: center


VERTICALLY


No need to shout. At the time Knud responded, the OP hadn't
stated yet that he meant vertically.

--
Els
http://locusmeus.com/
Sonhos vem. Sonhos v„o. O resto ť imperfeito.
- Renato Russo -

Jul 20 '05 #5
Aaron wrote:
Hi,

I have a div with an image inside it. The image needs to be centered in
the div. I thought I could do this by setting the image's margin to
"auto auto", but it seems to do nothing. What am I missing here?

Have you tried "vertical-align"?

--
jmm dash list at sohnen-moe dot com
(Remove .TRSPAMTR for email)
Jul 20 '05 #6
Els wrote:
No need to shout. At the time Knud responded, the OP hadn't stated yet
that he meant vertically.


Oh, sorry, my fault. I just posted a similar question shortly ago
("centering vertically" :-)

I apologize.

Jul 20 '05 #7
Aaron wrote:
Shouldn't this then center it in the div it's in? It does center
it horizontally, but it remains at the top vertically. Any ideas?


Sure. Search the group archives. The topic of centering --
horizontally and vertically -- has been discussed *numerous* times.

--
Brian (remove ".invalid" to email me)
http://www.tsmchughs.com/
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Greg Shannan | last post: by
18 posts views Thread by Heath | last post: by
2 posts views Thread by Ryan W Sims | last post: by
17 posts views Thread by Applebrownbetty | last post: by
6 posts views Thread by Jerry Camel | last post: by
3 posts views Thread by The New iSoftware Company | last post: by
3 posts views Thread by John Pote | last post: by
1 post views Thread by =?Utf-8?B?ZnJhbmt5?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.