Why <td > height not equal to <img > height inside of it when DOCTYPE is XHTML 1.0 Strict?

StackOverflow https://stackoverflow.com/questions/1450274

  •  11-09-2019
  •  | 
  •  

Question

<!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>
</head>
<body>

<table width="100%" cellspacing="0" cellpadding="0">
    <tr>
        <td align="right" colspan="5">
            <span class="validationInline">*</span> 
            <span class="hint">Required fields</span>
        </td>
    </tr>
    <tr>
        <td colspan="5" background="http://media.monster.com.hk/bgr_8.gif">
            <img src="/static/cleardot.gif" height="1" width="1" />
        </td>
    </tr>
</table>

</body>
</html>

Can check it out here: http://maishudi.com/tt2.html

I've known it's caused by DOCTYPE ,because deleting that part will make it normal:

http://maishudi.com/tt.html

So what's wrong?How can I make it work with the DOCTYPE ?

Was it helpful?

Solution

Note: this probably depends on the browser.
The size of block-level element (td, div, etc) if not specified will only be as big as needed, according to the space taken by its content. If specified, it will try to expand accordingly, except if the content is bigger, in which case it will expand as necessary.

In your example, the cell contains a single character (the non-breaking space), which take the size of single line. Hence, the block element must be at least 1 line-height high; it can't assume any smaller size. This is why your height declaration was ignored.

You may want to use this style:

line-height: 1px;

This sets the line-height to 1px. Line-height is not an element, so the above rule doesn't apply.

OTHER TIPS

Add a style block with this rule

td img {display: block;}

and see https://developer.mozilla.org/en/Images,_Tables,_and_Mysterious_Gaps for a full explanation.

background isn't a standard attribute for TD elements is the reason. Instead use:

style="background: url(/path/to/image.png);"

As for your 1 pixel image, I assume this is simply to make the table cell appear? If so, that's not the advised way of doing it. You can either do:

table { empty-cells: show; }

in CSS although I don't think IE6 supports that. The more standards compliant way is to use a non breaking space:

<td>&nbsp;</td>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top