Home | All Questions | alt.html FAQ >

How do I put a background for a single table cell?

With HTML <td background="image.gif">Some text</td> Although this is not strictly in compliance with HTML specification (as is marginheight and marginwidth).

It is probably better to do it in CSS. Using CSS it is actually easier. For a single cell only, it's more verbose especially when done right, but for a reason: <td style="background: url(abc.gif) #fff; color: #000;">...</td> This explicitly suggests a text colour too, and a background colour to be used when the image is not used for whatever reason (network congestion, document viewed locally with no access to images, or images turned off, for example). The text colour should of course be selected so that it is readable against the suggested background image and against the suggested background colour.

And if you wish to use the same background in different cells or other elements on a page, or even across several pages, then the CSS approach is easier. You would specify in one CSS rule the actual colours and image to be used and just make use of that rule using e.g. a CLASS attribute. The more you wish to say about the preferred presentation, the more you'll appreciate the simplicity of the CSS approach as opposite to scattering attributes and tags around.

However, as a practical problem, Netscape 4 does not seem to get those background colour and text colour properties right. It uses the background image, though. So the situation is roughly the same as when using <td background="...">, except that the style sheet will not be applied at all when style sheets are disabled, and on Netscape 4, disabling JavaScript automatically disables style sheets too.

Using background attribute in a td is not legal according to the W3C specifications

Recommended Resources


Related Questions