Web Hosting Talk







View Full Version : How to center a image by vertical and horizontal using HTML?


wht_posting
01-01-2003, 12:46 AM
I know it's kinda stupid quesetion but I just can't remember that.

Schumie
01-01-2003, 07:55 AM
<img src="blah" align="absmiddle">

If i remember correctly.

RajanUrs
01-01-2003, 08:07 AM
<div align="center"><img src="blah"</div>

cfwebworks
01-02-2003, 03:59 AM
<table>
<tr>
<td align='center' valign='middle' >blah</td>
</tr>
</table>

Website Rob
01-02-2003, 05:46 AM
Simple answer -- can't be done.

Although I hate to disagree with my colleagues, it seems some didn't pick up on the horizontal alignment part of your question and I'm not sure if you were aware that an image itself cannot be "positioned" the way you ask. What a person can do is put the image within container(s) and then position the container(s).

Easy to way to do that is with CSS...

<div style="width:100%; height:100%;">
<div style="float:right; width:54%; margin-top:25%;">

<img src="/images/whatever.gif">

</div>
</div>

This will get you in the right area and you can adjust it to your needs.

One could also use JS with innerWidth & innerHeight, but that is trickier coding. Also, more people have CSS turned on than JS, and the above CSS code will work with more Browsers/versions, than JS probably would.

UH-Matt
01-02-2003, 11:21 AM
<table cellpadding="0" cellspacing="0" border="0" height="100%" width="100%">
<tr><td align="center" valign="middle">
blah
</td></tr>
</table>

Rich2k
01-02-2003, 12:42 PM
the table height attribute does not have any meaning in the XHTML standard and therefore you shouldn't rely on it for page layout. I use it occasionally but only where the layout doesn't depend on it.

Here is a list of attributes for <table> from the XHTML 1.0 Transitional DTD

<!ATTLIST table
%attrs;
summary %Text; #IMPLIED
width %Length; #IMPLIED
border %Pixels; #IMPLIED
frame %TFrame; #IMPLIED
rules %TRules; #IMPLIED
cellspacing %Length; #IMPLIED
cellpadding %Length; #IMPLIED
align %TAlign; #IMPLIED
bgcolor %Color; #IMPLIED
>

Therefore you should use CSS

dark deity
01-02-2003, 01:01 PM
well how big is your page? if you know you can place your graphics specificly by doing

<img="url" align=right valign=in pixels halign=in pixels>
(but you must align all of your pics right for this to work, bad experiance if you don't, i dont know y)

Acronym BOY
01-02-2003, 05:28 PM
<<Insert everything Rick2k said here>>

OJI
01-02-2003, 06:37 PM
Originally posted by UH-Matt
<table cellpadding="0" cellspacing="0" border="0" height="100%" width="100%">
<tr><td align="center" valign="middle">
blah
</td></tr>
</table>
sorry guys - this ^^ is the easiest way.
:D

(the the hardcore perl and php programmers need a little HTML lesson dont you think Maatt :rolleyes: :confused: )

Juan R. Pozo
01-02-2003, 07:14 PM
This is works if you know the height of the image in pixels:

<div style="position:absolute; width:100%; top:50%; height:300px; margin-top:-150px; text-align:center">
<img src="whatever.png" alt="whatever" height="300">
</div>

(margin-top = - height/2)

Rich2k
01-02-2003, 07:16 PM
Don't get me wrong sometimes it's useful to use non standard attributes but just don't rely on them for your site because you'll suddenly find they don't work in browsers like Opera or future ones that may be standards only browsers.

CSS will work just fine though.

Website Rob
01-02-2003, 07:54 PM
If wht_posting wasn't confused before, I'll bet they are now. :D

iamdave
01-02-2003, 11:26 PM
Originally posted by Website Rob
Simple answer -- can't be done.

Although I hate to disagree with my colleagues, it seems some didn't pick up on the horizontal alignment part of your question and I'm not sure if you were aware that an image itself cannot be "positioned" the way you ask. It can be done. By using tables.

Website Rob
01-02-2003, 11:31 PM
What a person can do is put the image within container(s) and then position the container(s).

A table is a container, is it not?
One would then position the "table", correct? :)

iamdave
01-02-2003, 11:35 PM
Originally posted by Website Rob
What a person can do is put the image within container(s) and then position the container(s).

A table is a container, is it not?
One would then position the "table", correct? :) :D

Rich2k
01-03-2003, 05:42 AM
Originally posted by iamdave
It can be done. By using tables.

If you read earlier, we discussed that in can indeed be done using tables but under no circumstances should you rely on it for your site layout as the height attribute of a table as it isn't a defined attribute in the XHTML standard and so may not function the same or at all on all systems and browsers.

nameslave
01-06-2003, 09:23 PM
>wht_posting: How to center a image by vertical and horizontal using HTML?

I once had a client who asked exactly the same thing: vertically centering an image. If you mean positioning the image relative to your screen, then it canNOT be done, because screen sizes VARY.

Some so-called designers even forget that not everyone is using a 1024 by 768 setting.

Daniel141
01-06-2003, 09:35 PM
I've done it before with frames...

just make 3 horizontal frames

---------------------------------
frame1 height=*
---------------------------------
frame2 height= size of main section
---------------------------------
frame3 height=*
---------------------------------

You can also do it with a bit of DHTML... I don't have the code for the though
:(