Click here to get back home

center an image inside a div

 HomeNewsGroups | Search | About
 comp.infosystems.www.authoring.html    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
center an image inside a div finecur 02-24-2008
Posted by finecur on February 24, 2008, 6:03 pm
Please log in for more thread options
Hi everyone:

I have a div and there is an img inside. The image is much smaller
compared with div. So I would like to center the img inside the div.
Here is my code:

<div align="center" id="myCanvas" style="border:0px,
black;position:relative;height:400px;width:0px;"
onmousemove = "mouseTrace();" onmousedown = "traceMouseDown();"
onkeydown="ctrlKeyDown();" onkeyup="ctrlKeyUp();">
<img id="p" style="vertical-align:middle;vertical-align:center;"
src="img\loading2.gif">
</div>

As you can see, I tried many things. But the small image is still on
the top-left corner of the div. What did I do wrong?

Thanks,

ff

Posted by Ben C on February 25, 2008, 2:52 am
Please log in for more thread options
> Hi everyone:
>
> I have a div and there is an img inside. The image is much smaller
> compared with div. So I would like to center the img inside the div.
> Here is my code:
>
><div align="center" id="myCanvas" style="border:0px,
> black;position:relative;height:400px;width:0px;"
> onmousemove = "mouseTrace();" onmousedown = "traceMouseDown();"
> onkeydown="ctrlKeyDown();" onkeyup="ctrlKeyUp();">
><img id="p" style="vertical-align:middle;vertical-align:center;"
> src="img\loading2.gif">
></div>
>
> As you can see, I tried many things. But the small image is still on
> the top-left corner of the div. What did I do wrong?

Use text-align: center on the div. Img is inline by default, which means
it behaves a bit like text. Centering text is something you do on the
container-- think about it, you couldn't reasonably centre some words
but not others in a paragraph.

Or you can make the img display: block and then centre it with
margin-left: auto and margin-right: auto.

Avoid deprecated presentational attributes like align="" etc., they only
make things more confusing.

Posted by prac on February 25, 2008, 5:34 am
Please log in for more thread options
Hello

I have a test here, centering a unkown height pic into a div

http://philippe.chappuis.googlepages.com/test-alain.htm

I hope this will help you

---

http://philippe.chappuis.googlepages.com

---

Similar ThreadsPosted
Text and Image inside table Alight Text Left and Image right in same cell ?? October 24, 2006, 12:55 am
help with image alignment inside a
March 19, 2005, 9:11 pm
Image Positioning Inside Table Problem (IE only) January 2, 2008, 8:09 am
Center align a DIV without
tags
June 18, 2008, 9:43 am
How to center content in div tag? December 9, 2004, 8:52 pm
center align a page August 5, 2005, 2:27 am
Frame, table, and center August 31, 2007, 5:51 am
Center-Aligning elements May 12, 2008, 12:41 am
Center Web Page Not Working July 23, 2008, 1:30 am
center a picture in the middle of a paragraph December 2, 2004, 10:14 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap