Forum Moderators: open

Message Too Old, No Replies

VAlign DOCTYPE issue

DOCTYPE causes VAlign to not align all the way to the top

         

nablaodel

7:03 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



Hello,

My issue occurs while viewing the page in Fire Fox 2 but not IE6/7. It is resolved when I remove the DOCTYPE but I don't want to do that.

I'd like to have a table valign a small image to the very top of a cell. valign="top" moves the image up, but not very far. I've tried various style settings using position, margin, top, vertical-align but nothing I try works.

This code illustrates the issue:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head><title>Test</title></head>
<body>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td valign="top"><img src="/img/tlsmalldarkcorner.gif" width="5" height="5" border="0" /></td>
<td>A<br />B</td>
</tr>
</table>
</body>
</html>

encyclo

7:13 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld nablaodel! The problem occurs because, in standards-compliance mode, images are inline elements by default. As such, you have a line-height issue on small items. Increase the image dimensions to 25px and you'll see the difference.

The fix is easy - you set the image to display as a block-level element instead. In your CSS stylesheet:

img {display:block;}

or in the HTML:

<img src="/img/tlsmalldarkcorner.gif" width="5" height="5" border="0" [b]style="display:block;"[/b] />

nablaodel

7:26 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



Beautiful! Thanks so much.

rocknbil

8:03 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



encyclo, doesn't this also work?
<td valign="top" style="font-size: 2px;">

Thought I'd seen that approach somewhere around . . .

encyclo

8:14 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<td valign="top" style="font-size: 2px;">

It does work, in that you are reducing the line-height including descenders to being less than the height of the inline image. However, setting the image as a block-level element is usually simpler. :)

You could well argue in the above example that the image shouldn't be in the HTML at all, but set as a background via CSS - that would avoid the line-height issue too.