CSS/Webkit: Background Images for Table Row

后端 未结 3 1027
Happy的楠姐
Happy的楠姐 2020-12-31 20:38

For some reason when you apply a background image to a tr in Safari/Chrome it renders it as if the rule applies to every td.

Firefox:


(source: whyp

相关标签:
3条回答
  • 2020-12-31 20:41

    Will your table always only have 2 rows? Such as:

    <table>
      <tr>
        <td></td>
        <td></td>
      </tr>
    </table>
    

    If so, a simple but not overly elegant solution would be to split your background image into two images, and apply a CSS class to the left and right column, applying half of the arrow to the right side of the left column, and to the left side of the right column:

    <table>
      <tr>
        <td class="left"></td>
        <td class="right"></td>
      </tr>
    </table>
    

    Your CSS could then be similar to:

    td.left
    {
      background: #ffffff url(../PathToLeftBackground.png) top right;
    }
    td.right
    {
      background: #fffff url(../PathToRightBackground.png) top left;
    }
    

    You could also use a sprite image where you use 1 image and position it differently for the two backgrounds.

    I realize it's probably not the most ideal solution but it would at least fix your issue and get your project moving. I sometimes use simple solutions such as this in order to make forward progress, and then revisit the problem to make it more efficient later.

    Hope this helps!

    0 讨论(0)
  • 2020-12-31 20:46

    By default, the TR and TD have display properties table-cell and table-row. We need them to forget about it and feel like simple block elements:

    tr {display: block;}
    td {display: inline-block; background: transparent;}
    

    This code solved the problem of rendering for me.

    0 讨论(0)
  • 2020-12-31 20:51

    it's bit late but, you can use "background-attachment : fixed" to solve this problem.

    <table>
      <tr class="bg">
        <td></td>
        <td></td>
      </tr>
    </table>
    

    and in CSS

    tr.bg {
        background-image : url(../PathToLeftBackground.png) repeat-y 50px 0px;
        background-attachment: fixed;
    }
    

    and it works!

    0 讨论(0)
提交回复
热议问题