Table有渐变边框和单元格渐变边框

Table with gradient borders and cell gradient borders

我想实现 table 边框渐变和 div 边框作为一个完整的项目,我的意思是单元格的边框颜色应该与众不同。

这就是我目前所知道的:

table tr:first-child td {
  border-top: 0;
}
table tr:last-child td {
  border-bottom: 0;
}
table tr td:last-child {
  border-right: 0;
  border-left: 0;
}
table tr td:first-child {
  border-left: 0;
}
td {
  border-right: 2px solid #bebebe;
  border-bottom: 2px solid #bebebe;
}
td {
  border-collapse: collapse;
}
table {
  /*border-collapse: collapse;*/
  border-style: solid;
  border-width: 20px 20px;
  border-image-source: linear-gradient(to bottom, #eee 0%, #bebebe 100%);
  border-image-slice: 20;
  border-image-repeat: stretch;
  box-shadow: 0px 10px 10px black;
}
body {
  background-color: #eee;
}
<table class="tablagradiente" align="center" width="41%">
  <tr>
    <td>
      <p>Sesiones Ordinarias</p>
    </td>
    <td>
      <p>Sesiones Extraordinarias</p>
    </td>
  </tr>
  <tr>
    <td>
      <p>&nbsp;</p>
    </td>
    <td>
      <p>Primera Sesión Extraordinaria 2015</p>
    </td>
  </tr>
</table>

解决方案

你实际上可以在没有 border-image 属性 的情况下实现你想要的,只需设置以下内容:

table {
  background-image: linear-gradient(to bottom, red 0%, blue 100%); /* the gradient */
  background-origin: border-box; /* set background to start from border-box */
  border-spacing: 5px; /* space between each cell */
  border: 5px solid transparent; /* optional */
}

浏览器支持:


说明

本质上我们在这里做的是以下内容:

  • linear-gradient 添加为 table 的 background
  • 设置背景的原点,使其从 table 的 border-box 开始。 (关于background-origin的更多详情,请参考)。
  • 分隔 table 单元格和行之间的边框(默认 设置),这样 tablebackground 可以通过中间的space。
  • 为您的 table 本身添加一个额外的透明 border。这是可选的,只是因为图像中的 table 边框看起来比单元格之间的边框粗。

table {
  background-image: linear-gradient(to bottom, red 0%, blue 100%);  /* the gradient */
  background-origin: border-box;  /* set background to start from border-box */
  border-spacing: 5px;  /* space between each cell */
  border: 5px solid transparent;  /* optional */
}
body {
  background-color: #eee;
}

/* Just for demo */

table {
  width: 500px;
}
td {
  background: white; /* if not set cell would also be transparent and show the gradient */
}
<!-- prefix free lib for older browsers -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>

<table class="tablagradiente" align="center" width="41%">
  <tr>
    <td><p>Sesiones Ordinarias</p></td>
    <td><p>Sesiones Extraordinarias</p></td>
  </tr>
  <tr>
    <td><p>&nbsp;</p></td>
    <td><p>Primera Sesión Extraordinaria 2015</p></td>
  </tr>
  <tr>
    <td><p>&nbsp;</p></td>
    <td><p>Primera Sesión Extraordinaria 2015</p></td>
  </tr>
  <tr>
    <td><p>&nbsp;</p></td>
    <td><p>Primera Sesión Extraordinaria 2015</p></td>
  </tr>
</table>

注意:我在答案中使用了红色到蓝色的渐变,使效果更明显。