Friday, November 19, 2010

What is the correct way to center a picture using html?

I have an image I need to put in the center of the page and I'm still not quite sure how to do that. This is just my first class that showed very rough html so I don't know what exactly I'm supposed to put.What is the correct way to center a picture using html?
%26lt;center%26gt;%26lt;img src=';url';%26gt;%26lt;/center%26gt;What is the correct way to center a picture using html?
http://www.google.com/

Report Abuse



It depends on your document type and what you are putting the image inside. Note that the ';align'; attribute of the %26lt;img%26gt; tag is outdated and there are several better options:





The best way is inside a %26lt;div%26gt; element, where you control the alignment in the div tag:





%26lt;div style=';vertical-align: middle;text-align:center;'; %26gt;%26lt;img src=';your-image-file.gif'; alt=';'; /%26gt;%26lt;/div%26gt;





If you need more space around it, you can either use margins in the style tag or set the div style width and height attributes:





%26lt;div style=';vertical-align: middle;text-align:center;margin: 50px 50px 50px 50px'; %26gt;%26lt;img src=';your-image-file.gif'; alt=';'; /%26gt;%26lt;/div%26gt;





%26lt;div style=';vertical-align: middle;text-align:center;width:100%;heig鈥?%26gt;%26lt;img src=';your-image-file.gif'; alt=';'; /%26gt;%26lt;/div%26gt;





If you want some text to wrap around it, use either float:left or float:right in the div style.





Another option, although a bit more limited, is to simply add the style attribute to your %26lt;img%26gt; tag:





%26lt;img style=';margin: 50px 50px 50px 50px'; src=';your-image-file.gif'; alt=';'; /%26gt;





Finally, if your image is already in a table cell, you can simply add the style to the %26lt;td%26gt; tag and adjust the width/height of the cell if needed:





%26lt;td style=';vertical-align: middle;text-align:center;'; %26gt;%26lt;img src=';your-image-file.gif'; alt=';'; /%26gt;%26lt;/td%26gt;






Well, assuming that you are using XHTML, %26lt;center%26gt; tag is outdated for some ';doctype';s. You can do the following:


1. Take the image inside a %26lt;td%26gt; of a table and make the alignment of the %26lt;td%26gt; to ';center';.


E.g.


%26lt;table id=';maintable'; border=';0'; cellspacing=';0'; cellpadding=';0'; width=';100%';%26gt;


%26lt;tr%26gt;%26lt;td%26gt;[Something.....]%26lt;/td%26gt;%26lt;td style=';text-align:center';%26gt;%26lt;img src=';mypic.gif'; alt=';[mypic]';.... /%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;%26lt;/table%26gt;
Assuming you mean actual HTML, and not CSS, then it's %26lt;img src=';PIC'; align=';center'; /%26gt;

No comments:

Post a Comment