By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,494 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,494 IT Pros & Developers. It's quick & easy.

Centering image in div

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
Knud Gert Ellentoft wrote:
Use text-align: center


VERTICALLY
Jul 20 '05 #4

P: n/a
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 vo. O resto imperfeito.
- Renato Russo -

Jul 20 '05 #5

P: n/a
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

P: n/a
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

P: n/a
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.