Can we solve the table row background image problem, in chrome, in multi celled tables?
Asked Answered
P

3

29

It is frequently asked – but I haven’t seen a good answer yet (and I looked). If you set a background image in CSS to a table row- the image will repeat itself in every cell. If you set the position: relative (for the row) and set the background-image: none (for the cells) it solves the problem on IE but not on chrome! I can't use background positioning since there are many calls and their size varies. (And the picture is not symmetrical- It's a fade out from one side. Anybody??

Example for the css code :

tr { height: 30px; position:relative;}
tr.green { background: url('green_30.png') no-repeat left top; }
tr.orange { background: url('oranger_30.png') no-repeat left top;}
tr.red { background: url('red_30.png') no-repeat left top; }
td { background-image: none; }

The HTML is basic - A multi cell table.

The goal is to have different colors fade into every row, but it could be any non-pattern image.

Presidentelect answered 8/6, 2010 at 12:54 Comment(4)
Do you have a live example or some souce markup/css?Sula
It seems Firefox is the only browser that behaves like it should. Chrome, IE, Opera and Safari all cause the background-image to restart in each cell, rather than flowing nicely uninterrupted for the whole row. This issue is listed on code.google.com/p/chromium/issues/detail?id=44361 but looks like it won't be fixed.Reduction
Works just fine for me using Google Chrome 5.0.375.55, on Ubuntu 9.10. jsfiddle.net/pzjUt and what i see: img153.imageshack.us/img153/3521/resultm.pngFluter
UPDATE: oddly enough the new version of chrome (5.0.375.70) broke this again, so now I get the image repeating in each cell...Fluter
F
11

Ok, I spent ages reading about this, and couldn't find an easy fix for all browsers, but as I see you are using fixed height rows, I've developed my own workaround: http://jsfiddle.net/DR8bM/

Basically, instead of putting the background image on the row, you put it on an absolute-positioned div in the first cell of each row (and expand it to fill the whole row). This is slightly hacky, but may be the only reliable way to achieve what you want.

Fluter answered 16/6, 2010 at 11:6 Comment(0)
A
4

Add float:left to the row, that should fix it.

tr {float:left;}
Alvar answered 4/8, 2011 at 1:0 Comment(4)
worked for me. solved the problem in chrome and safari; didn't cause any problems, that i can see, in firefox and internet explorer (where the original problem didn't exist)Dnepropetrovsk
When applying this fix, I'm losing all my column widths. Would only work if you have you widths predefined!?Macintyre
@Macintyre You are right. But it is a kind of flex style: See my codepenMincing
In Chrome seems to work only if there is no cells in the row, which have background set themselves.Thoroughgoing
R
2

Tested in Chrome 54 on Windows 10:

tr {
  background-attachment: fixed;
}

Though it seems it's not without bugs - the background image only repeats throughout the viewport at loadtime, which means that the cells that appear when you scroll down doesn't have the background.

Recurved answered 2/11, 2016 at 12:3 Comment(1)
The comment at the bottom is what ended up helping me, thanks!Terza

© 2022 - 2024 — McMap. All rights reserved.