Staff spaces...

Colin Jones

Web Developer

Need a background-image on a table row?

Posted on 11/17/2006 by Colin
comment bubble3 Comments

Don't do it the intuitive way...

<table>
 <tr style="background: url(/images/tr-background.gif) no-repeat 0 0;">
  <td>Row 1</td>
  <td>Row 2</td>
  <td>Row 3</td>
 </tr>
</table>

...because it doesn't work in IE6 or Safari, even if you set the <td> background element to 'transparent' or 'none'.

But you can still make it happen with just the one image:

<table>
 <tr>
  <td style="background: url(/images/tr-background.gif) no-repeat 0 0;">Row 1</td>
  <td style="background: url(/images/tr-background.gif) no-repeat 50% 0;">Row 2</td>
  <td style="background: url(/images/tr-background.gif) no-repeat 100% 0;">Row 3</td>
 </tr>
</table>

You're just altering the background-position of the image, so that what should be on the left goes on the left (at 0), the middle part goes to the middle (50% horizontally), and the last part goes at the end (100%). Remember that values in the background-position element are ordered horizontal, then vertical, unlike the margin and padding elements.

And, of course, you'll want to separate presentation from content by putting the CSS elsewhere and classing the td's.

Tagged:  table, background-image, tr, css

Comments

Posted by Jamie Fehr on March 01, 2007

Came across this same problem, I haven't yet figured out how to make safari play nice, but Internet Explorer will respect your Background Image declaration if you add position: relative; after the image declaration. Hopefully I'll get the safari thing figured out before long.

Posted by DraganR on April 03, 2008

This thing works! Thanks for the tip. I'm testing my layout in IE7, Opera 9.02 and Firefox 2.0. By the way, I didn't need to set position to relative. Here's my row declaration:

1 2 3

Posted by scott Dilley on June 24, 2008

you rock.

Post a Comment

Name

Comment

192

Space Highlight

Trumpet player

583

I created a site for my trumpet playing and teaching - check it out. I play for weddings and other events!

flickr

SupportSupport

Pay OnlinePay Online

Contact UsContact Us

Ruby on Rails
Ruby on Rails Web Development

Plexus Web Creations builds custom, rich internet applications on the Ruby on Rails web application framework.

ProductCart
ProductCart E-Commerce Developer

Plexus Web Creations provides ProductCart Shopping Cart for e-commerce web sites.