简体   繁体   English

表与渐变边框和单元格渐变边框

[英]Table with gradient borders and cell gradient borders

I'd like to achieve a table with gradient on the border and div borders acting as is they were a whole item, by that I mean that the border color of the cells should be diferent. 我想在边框和div边界上实现一个渐变的表格,因为它们是一个完整的项目,我的意思是单元格的边框颜色应该是不同的。

表与渐变

That's what I've got so far: 这就是我到目前为止所得到的:

 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> 

Solution

You can actually achieve what you want without border-image property just by setting the following: 通过设置以下内容,您实际上可以在没有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 */
}

Browser Support: 浏览器支持:


Explanation 说明

In essence what we are doing here is the following: 实质上我们在这里做的是以下内容:

  • Add the linear-gradient as the background of the table. 添加linear-gradient作为表格的background
  • Set the origin of the background such that it starts from the border-box of the table. 设置背景的原点,使其从表格的border-box开始。 (For more details on background-origin , please refer my answer here ). (有关background-origin更多详情,请参阅我的答案 )。
  • Separate the border between the table cells & rows ( default setting) such that the background of the table is visible through the space in between. 分隔表格单元格和行之间的边界( 默认设置),以便通过其间的空间可以看到tablebackground
  • Add an extra transparent border to your table itself. 为表本身添加额外的透明border This is optional and is only required because the table border in your image seems thicker than the border between the cells. 这是可选的,仅需要,因为图像中的表格边框看起来比单元格之间的边框更粗。

 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> 

Note: I have used a red to blue gradient in the answer to make the effect more apparent to the eye. 注意:我在答案中使用了红色到蓝色渐变,使效果更明显。

在此输入图像描述

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

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