简体   繁体   English

用colspan细胞在表格中交替列样式

[英]Style alternating columns in a table with colspan cells

I've been asked to do a project where I create a table which has alternating column background colors. 我被要求做一个项目,我创建一个具有交替列背景颜色的表。 This is all well and good, except there are a few rows which need to span the other columns while still having the "checked" background, that is determined in each td. 这一切都很好,除了有几行需要跨越其他列,同时仍然具有“检查”背景,这在每个td中确定。

Please see the jsfiddle , or the code: 请参阅jsfiddle或代码:

body {
    font-family:Calibri, Trebuchet MS, Verdana, Tahoma, sans-serif;
}
table {
    border:1px solid #444;
    text-align:center;
}
th, td {
    width:200px;
    padding:2px;
}
.lg {
    background-color:#eee;
}
.dg {
    background-color:#ddd;
}
}

Corresponding html: 对应的html:

<table>
    <tr>
        <th>Fruits</th>
        <td class="lg">Peach</td>
        <td class="dg">Blueberry</td>
        <td class="lg">Apple</td>
    </tr>
    <tr>
        <th>Chocolate</th>
        <td class="lg">Chocolate-Chip</td>
        <td class="dg">Double Chocolate</td>
        <td class="lg">Turtle</td>
    </tr>
    <tr>
        <th>Peanut Butter</td>
            <td colspan="3">Peanut Butter Swirl and a long list of items</td>
    </tr>
    <tr>
        <th>Classics</th>
        <td class="lg">Chocolate</td>
        <td class="dg">Vanilla</td>
        <td class="lg">Strawberry</td>
    </tr>
</table>

Currently I have a background image which I've inserted to replicate this effect in the td which is spanning the columns. 目前我有一个背景图片,我已插入该图像以在跨越列的td中复制此效果。 The only problem is this doesn't line up perfectly no matter how I try (taking a screen cap of the results, etc.); 唯一的问题是,无论我如何尝试(取结果的屏幕上限等),这都不能完美排列; and I should note this is a fixed-width table. 我应该注意这是一个固定宽度的表。 In all honesty, it's pretty dang close in all of them except Opera, which just looks way off. 老实说,除了Opera之外,所有这些都很接近。

Is there any way to do this consistently? 有没有办法一直这样做?

This is a really tough problem. 这是一个非常棘手的问题。 Here's an out-of-the-box kind of solution , that may or may not work for you . 这是一种开箱即用的解决方案可能适用于您也可能不适合您 It involves a linear "gradient" on the colspan3 cells, but requires: 它涉及colspan3细胞的线性“梯度”,但需要:

  • A given width on columns (which was present, at 200px, in your sample code) 列上的给定宽度(在示例代码中以200px存在)
  • A table that can in fact accommodate that width (as in my example, with a min-width of 800px) 一个实际上可以容纳该宽度的表(如我的例子中,最小宽度为800px)
  • Either include the paddings of cells in the gradients, or remove them (as in my example) 要么在渐变中包含单元格的填充,要么删除它们(如我的示例所示)

Your html was slightly updated as such with a class on the colspan-cell: 你的html在colspan-cell上有一个类略有更新:

<td class="fullspan" colspan="3">Peanut Butter Swirl and a long list of items</td>

And the CSS had this added, with the use of the Gradient Generator : 通过使用渐变生成器 ,CSS增加了这个:

.fullspan {
    background: #eee; /* Old browsers */
    background: -moz-linear-gradient(left,  #eee 0%, #eee 200px, #ddd 200px, #ddd 400px, #eee 400px); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, right top, color-stop(0%,#eee), color-stop(200px,#eee), color-stop(200px,#ddd), color-stop(400px,#ddd), color-stop(400px,#eee)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(left,  #eee 0%,#eee 200px,#ddd 200px,#ddd 400px,#eee 400px); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(left,  #eee 0%,#eee 200px,#ddd 200px,#ddd 400px,#eee 400px); /* Opera 11.10+ */
    background: -ms-linear-gradient(left,  #eee 0%,#eee 200px,#ddd 200px,#ddd 400px,#eee 400px); /* IE10+ */
    background: linear-gradient(to right,  #eee 0%,#eee 200px,#ddd 200px,#ddd 400px,#eee 400px); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#eee', endColorstr='#eee',GradientType=1 ); /* IE6-9 */
}

Giving something like this in modern browsers, including Opera: 在现代浏览器中给出类似的东西,包括Opera:

渐变黑客渲染

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM