Forum Moderators: open

Message Too Old, No Replies

HTML table vertical aligning problem

         

chuckee

1:19 am on Feb 4, 2007 (gmt 0)

10+ Year Member



Hey,
I am trying to get the text in the second cell of this HTML table to be centered vertically in the middle of the the image in the first cell. See the code. For some reason the text is always positioned at the bottom of the cell! Sorry for all the encrypted stuff, but this is how I can replicate this problem. Basically I want the text 'Sample Text' to be properly vertically aligned. valign="middle" is not working!

<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr valign="middle">
<td width="20%"><form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHiAY (...) PKCS7-----">
</form></td>
<td>Sample Text</td>
</tr>
</table>

[edited by: encyclo at 1:24 am (utc) on Feb. 4, 2007]
[edit reason] fixed side-scroll [/edit]

ericjust

2:50 am on Feb 4, 2007 (gmt 0)

10+ Year Member



Use valign="middle" on the cell, not the row.

<tr>
<td valign="middle">First Cell</td
<td valign="middle">Second Cell</td>
</tr>

chuckee

10:57 pm on Feb 4, 2007 (gmt 0)

10+ Year Member



Hey, thanks for the advice. I still cannot get this to work with my code however. I think the problem might be that the valign attribute doesn't work when you have an HTML form <input type="image"> as your image in one of the cells. For example, the text in the second cell is still not being centred vertically:

<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="middle" width="20%"><form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form></td>
<td valign="middle">Sample Text</td>
</tr>
</table>

ericjust

11:38 pm on Feb 4, 2007 (gmt 0)

10+ Year Member



I didn't notice that you had a form in there.

You can do a couple of things...

1. Make the form display inline so that it can be vertically aligned. (<form style="display:inline;" action=.....)

2. Put the form outside the table and just include the image submit button in the cell. (<form><table>.....</table></form>)

Also - you can use CSS to do all of this:

Put in <head> and remove valign attribute. You probably want to use specific classes for tables that you want to work this way - for instance <table class="valign"> ->

For all tables and forms:

<style>

table tr td {
vertical-align:middle;
}

form {
display:inline;
}

</style>

For tables with the "valign" class (and forms in those tables):

<style>

table.valign tr td {
vertical-align:middle;
}

table.valign form {
display:inline;
}

</style>

chuckee

12:04 am on Feb 5, 2007 (gmt 0)

10+ Year Member



Excellent advice! Thanks very much, I now have it working :)