具有渐变边框和单元格渐变边框的表格

7
我希望你能够制作带有渐变边框的表格,并使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>

1个回答

15

解决方案

您实际上可以在不使用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添加为表格background
  • 设置背景的起点,使其从表格的border-box开始。(有关background-origin的更多详细信息,请参见我的回答)。
  • 分隔表格单元格和行之间的边框(默认设置),以便通过它们之间的空间看到tablebackground
  • 在表格本身上添加额外的透明border。这是可选的,仅因为您图片中的表格边框似乎比单元格之间的边框更粗。

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>

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

enter image description here


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