Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Centering an image horizontally

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
Is there a better way of doing this? The best I can come up with is
Code:
   <table width="100%">
      <tr>
         <td width="49%">&nbsp;</td>
         <td><img src="animage.jpg" /></td>
         <td width="49%">&nbsp;</td>
      </tr>
   </table>
I can't figure out how to do it with divs or spans.
 
Code:
<style type="text/css">
img {
display: block;
margin:0 auto;
}
</style>
<div class="center">
  <img src="favicon.ico">
</div>
 
forgot to point out, you can also set styles to the div:

Code:
style type="text/css"
.center {
}
 
Or maybe this:

Code:
<img style="margin: 0 50%;" src="favicon.ico">

Seemed to work on FireFox but I've not tested on any other browsers.


Trojan.
 
cygmorg Thanks - img {display: block;margin:0 auto;} works quite well. Am I supposed to put anything in the .center style definition? Seems to be ok with nothing in it. It is even OK without the definition.

Trojan - this does exactly what you tell it. If the image is very wide, it will still give a 50% margin on the left. I tried 15% and that seemed to be OK but when I shifted from a 1024x768 montior to 1920x1080 it didn't quite work.
 
It will work fine without the div, because the styles are being applied to the 'img' tag.
If you want, you could put it in a div and then center that instead:
Code:
<style type="text/css">
.center {
text-align: center;
}
</style>
<div class="center">
  <img src="favicon.ico">
</div>
I personally prefer doing it this way, but its up to you.

You could just have:
Code:
<style type="text/css">
img {
display: block;
margin: 0 auto;
}
</style>
  <img src="favicon.ico">

Sorry to confuse you with the div :)

Hope that helps
 
The 50% I suggested applies to both left and right sides so it should give centering regardless.
Not sure if it's a browser specific issue.


Trojan.
 
Found that this works quite well too
Code:
<center><img src="favicon.ico"></center>
 
Albeit it works, it is using a deprecated code. Unfortunately, asking such a simple question yields many solutions that might not be best. I would advise you to forget all but the last cygmorg's post. The two solutions (with #1 being best and #2 second best) are the two complete and correct solutions.

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
OK, I won't be using it on new designs but this is for a hypertext application on Win2K, which, I think, has IE5.5. It is just a cheap UI for data entry. It will probably be used for another 5-10 years until someone decides to replace the whole suite.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top