Make background for table rows extend past the bounds of the table

喜欢而已 提交于 2019-12-05 19:47:55

I don't think container class is meant to be used inside tables. I would just place the entire table inside a .container and lengthen the stripes using pseudo elements. With the default table-striped from bootstrap, that would look something like this:

.table-striped td:first-child,
.table-striped th:first-child,
.table-striped td:last-child,
.table-striped th:last-child {
   position: relative;
}

.table-striped td:first-child:before,
.table-striped th:first-child:before,
.table-striped td:last-child:after,
.table-striped th:last-child:after {
    content: '';
    position: absolute;
    top: -1px;
    bottom: -2px;
    width: 100vw;
    display: block;
    background: inherit;
    border: inherit;
}
.table-striped td:first-child:before,
.table-striped th:first-child:before {
    right: 100%;
}
.table-striped td:last-child:after,
.table-striped th:last-child:after {
    left: 100%
}

And the updated fiddle: http://jsfiddle.net/4L4tatk5/8/

Note that you may have to change that 100vw (= 100% of vertical width) to some other unit (just a ridiculously high 9999pxor something) if you need to support

The only other way I can see doing this is to use nested tables...

<table>
   <tr>
      <td>
         <table>
            <tr>
               <td>Item 1</td>
               <td>Item 2</td>
               <td>item 3</td>
            </tr>
         </table>
      </td>
   </tr>
   <tr>
      <td>
         <table>
            <tr>
               <td>Item 4</td>
               <td>Item 5</td>
               <td>item 6</td>
            </tr>
         </table>
       </td>
   </tr>
</table>

I suppose an even simpler solution would be to just use <div> containers with a table for each row...

<div class="container odd">
   <table>
      <tr>
         <td>Item 1</td>
         <td>Item 2</td>
         <td>item 3</td>
      </tr>
   </table>
</div>
<div class="container even">
   <table>
      <tr>
         <td>Item 1</td>
         <td>Item 2</td>
         <td>item 3</td>
      </tr>
   </table>
</div>

The disadvantages of the above are obvious. Not dynamic by any means, so if this data is being generated dynamically, this is definitely a no go. Beyond that it's bloated and not semantic, but you don't have to mess with setting the <tr>'s to display: block;

In any case I think you can see what you would do with the above, limit the width of the inner tables, and extend the width of the containing elements to 100%, style the containing elements and tada.


I have to leave work, but I will take another stab at this when I get home to see if I can't come up with some magic using :before and :after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!