列周围的样式表

时间:2018-09-26 09:33:33

标签: html css html5 css3 css-tables

Goal

我正在尝试设计一个表格,使其最终像图像一样。我需要将最后2列括起来以达到此效果。但是,我在Google或Stack上找不到任何内容。 HTML / CSS甚至有可能吗?还是我需要使用CSS创建叠加层并将其放置在最后2列上?

<table class="promo-table">
  <th>Product</th>
  <th>Modellen</th>
  <th>Referentie</th>
  <th>TariefPrijs 2018</th>
  <th class="promo">Promoprijs</th>

  <tr class="first-promo">
    <td>Set remblokken, vooras</td>
    <td>117, 176, 246</td>
    <td>A 000 420 29 02</td>
    <td>€75,81</td>
    <td class="promo">€38,70</td>
  </tr>

  <tr>
    <td>Set remblokken, vooras</td>
    <td>117, 176, 246</td>
    <td>A 000 420 29 02</td>
    <td>€75,81</td>
    <td class="promo">€38,70</td>
  </tr>

  <tr>
    <td>Set remblokken, vooras</td>
    <td>117, 176, 246</td>
    <td>A 000 420 29 02</td>
    <td>€75,81</td>
    <td class="promo">€38,70</td>
  </tr>

</table>

1 个答案:

答案 0 :(得分:1)

一种方法是通过设置相应单元格的边框来模拟该框。可以使用较大的顶部边框和伪元素::before创建标题。

仅当您在表格上设置border-collapse: collapse;时,此方法才有效,否则单元格的边框之间将存在间隙。

优点是,框的大小与表的内容直接成比例。如果您将该框创建为叠加层,则表的内容或列宽发生更改时将出现问题。

table {
  border-collapse: collapse;
}

th,
td {
  padding: 5px 10px;
  margin: 0;
}

table tr td:nth-child(4),
table tr th:nth-child(4) {
  border-left: 2px solid #17a4e4;
}

table tr td:nth-child(5),
table tr th:nth-child(5) {
  border-right: 2px solid #17a4e4;
}

table tr th {
  position: relative;
  border-top: 25px solid transparent;
}

table tr th:nth-child(4),
table tr th:nth-child(5) {
  border-top: 30px solid #17a4e4;
}

table tr:last-child td:nth-child(4),
table tr:last-child td:nth-child(5) {
  border-bottom: 2px solid #17a4e4;
}

table tr th:nth-child(4)::before {
  content: "Aanbieding";
  text-transform: uppercase;
  color: white;
  position: absolute;
  top: -23px;
  left: 50%;
}
<table class="promo-table">
  <tr>
    <th>Product</th>
    <th>Modellen</th>
    <th>Referentie</th>
    <th>TariefPrijs 2018</th>
    <th class="promo">Promoprijs</th>
  </tr>
  <tr class="first-promo">
    <td>Set remblokken, vooras</td>
    <td>117, 176, 246</td>
    <td>A 000 420 29 02</td>
    <td>€75,81</td>
    <td class="promo">€38,70</td>
  </tr>
  <tr>
    <td>Set remblokken, vooras</td>
    <td>117, 176, 246</td>
    <td>A 000 420 29 02</td>
    <td>€75,81</td>
    <td class="promo">€38,70</td>
  </tr>
  <tr>
    <td>Set remblokken, vooras</td>
    <td>117, 176, 246</td>
    <td>A 000 420 29 02</td>
    <td>€75,81</td>
    <td class="promo">€38,70</td>
  </tr>
</table>