使用语义正确的标记,使一个列“突出”地样式化表格

4
我想创建一个长这样的表格:

4-column table with the second column being larger than the others

一开始看起来很简单,实际上并不是:

  • 背景图片很棘手,因为它横跨了几个没有共同父元素的元素
  • 表格单元格必须具有异常大小,而表格通常不喜欢这样做
  • 悬停覆盖必须排除突出部分的列的部分

这是一个基础 fiddle,您可以用来测试。它包含了表格的基本标记和样式,但是没有异常的表格单元格和悬停效果。

我在 td 中使用了 :before 伪元素来创建蓝色背景,并使用 :after 创建了带有 multiply 混合模式的 0.5 不透明度图像。

我通过 background-position 在每个表格单元格中偏移背景图像。第一个单元格的偏移量为 0,第二个单元格的偏移量为 100%,第三个单元格的偏移量为 200% 等等。它们无缝对齐。

我尝试过的

我分叉了上面的 fiddle,试图使它视觉上正确。我几乎做到了。这是 result。不过还存在问题:

我通过在元素中使用:after伪元素创建了悬停效果。但是,这要求我使元素具有块显示(因为具有display table-row的元素显然不能具有伪元素)。这意味着如果单元格没有min-width或它们只是具有更多内容,则所有列都将不对齐,表格看起来就不像表格。可以在fiddle中看到。
因为我在每个表格单元格的背景中使用基于百分比的偏移量,所以一个稍微大一点或小一点的单元格会破坏背景图像的对齐,因为该百分比是基于元素本身的大小而不是之前的元素。在fiddle中,您可以清楚地看到背景图像被丢弃了。
问题
显然,你可以很容易地用4个相邻的元素和一些JavaScript实现悬停效果。但是,保留语义上正确的表格标记,即使用元素,是否可能创建此布局?

随意使用 this fiddle 进行测试。

1个回答

1
我保留了您的布局,只添加了一个包装器。
另一方面,特殊的弹出列仅由伪元素制成。这样,我可以调整到顶部,但无法调整到底部。这就是为什么需要容器来剪切伪元素底部的原因。
底部的阴影需要做一些微调,但除此之外,我认为结果还不错。

.container {
    margin: 20px;
    overflow: hidden;
}

table {
    margin: 10px 10px 20px 10px;
    background: #F0F6F7;
    box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.16);
    border-radius: 5px;
    border-collapse: collapse;
}


tr:hover td {
    background: rgba(255, 0, 0, 0.2);
}

tr + tr td, tr + tr td.pop:before {
    border-top: 1px solid rgba(0, 0, 0, 0.05);
}


tr:first-child .pop:after, tr:first-child .pop:before {
    top: -10px;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
}

tr:last-child .pop:after, tr:last-child .pop:before {
}

td {
    min-width: 150px;
    box-sizing: border-box;
    padding: 16px 10px 15px 10px;
    color: #787878;
    font-family: Roboto, sans-serif;
    font-size: 16px;
    text-align: center;
}

td + td {
    border-left: 1px solid rgba(0, 0, 0, 0.05);
}

td.pop {
    position: relative;
    z-index: 0;
    color: #FFF;
}

td.pop, td.pop + td {
    border-left: none;
}

tr:first-child td.pop:after {
    content: '';
    position: absolute;
    top: -10px;
    right: 0;
    left: 0;
    z-index: -2;
    background: url('https://i.imgur.com/lcKmrnE.jpg')  #539BFC;
    background-blend-mode: screen;
    opacity: 0.75;
    height: 1000%;
}

tr:last-child td.pop:after {
    content: '';
    position: absolute;
    bottom: -10px;
    left: 0;
    width: 100%;
    z-index: -1;
    height: 10px;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    box-shadow: 2px 2px 2px 0px lightgray, 0px 10px 0px 10px white;
}
<div class="container">
<table>
    <tbody>
        <tr><td>Josh</td><td class="pop">3 BTC</td><td>$46,343</td><td>27/12/17</td></tr>
        <tr><td>Anne</td><td class="pop">2 BTC (veeery big cell)</td><td>$38,452</td><td>26/12/17</td></tr>
        <tr><td>Jack</td><td class="pop">6 BTC<br><small>bigger</small></td><td>$126,989</td><td>26/12/17</td></tr>
        <tr><td>Gyumur</td><td class="pop">0.7 BTC</td><td>$14,104</td><td>24/12/17</td></tr>
        <tr><td>Boggy</td><td class="pop">12 BTC</td><td>$267,766</td><td>21/12/17</td></tr>
    </tbody>
</table>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接