douyouqian8550 2015-05-24 10:22
浏览 56
已采纳

向CSS表添加样式

I'm echoing data out of my database into a <table>, however I'm trying to add a gradient line under each output, to show in-between each <tr> output row. But I can't get the CSS right on the <tr>.

I've managed to put together the design I want on a <hr> here: http://jsfiddle.net/ghrw3k8e/.

But I want it in-between my table rows (not columns).

My PHP output data:

echo " <tr> 
<td align='center'>".$1." </td> 
<td align='center'>".$2."</td> 
<td align='center'>".$3."</td>
 </tr> ";
  • 写回答

2条回答 默认 最新

  • doulu4534 2015-05-24 14:03
    关注

    Just use pseudo-elements. You will have to put that "border" on one <td> of each <tr>, so its width should be equal to 100 × number_of_columns % if they are all the same width:

    table {
      width: 100%;
      border-collapse: collapse; 
    }
    
    td {
      position:relative;   
      width: 33.333333%;
      border: 1px solid transparent;
      text-align:center;
    }
    
    tr:not(:last-child) > td:first-child::after {
      content:"";
      position:absolute;
      bottom:-1px;
      left:0;
      height: 1px;
      width:300%;
      background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(176,0,0,.8), rgba(0,0,0,0));
      background-image:    -moz-linear-gradient(left, rgba(0,0,0,0), rgba(176,0,0,.8), rgba(0,0,0,0));
      background-image:     -ms-linear-gradient(left, rgba(0,0,0,0), rgba(176,0,0,.8), rgba(0,0,0,0));
      background-image:      -o-linear-gradient(left, rgba(0,0,0,0), rgba(176,0,0,.8), rgba(0,0,0,0));
    }
    <table>
      <tr><td>A</td><td>B</td><td>C</td></tr>
      <tr><td>D</td><td>E</td><td>F</td></tr>
      <tr><td>G</td><td>H</td><td>I</td></tr>
    </table>

    Although it may seem more logical to have it on the <tr>, it won't get positioned correctly, as you can read from the spec: http://www.w3.org/TR/CSS21/visuren.html#propdef-position

    The effect of 'position:relative' on table-row-group, table-header-group, table-footer-group, table-row, table-column-group, table-column, table-cell, and table-caption elements is undefined.

    Here's the incorrect code. You can see that the ::after element is positioned at the very bottom of the page:

    table {
      width: 100%;
      border-collapse: collapse;
    }
    
    
    tr{
      position:relative;    
    }
    
    td {
      width: 33.333333%;
      border: 1px solid transparent;
      text-align: center;
    }
    
    
    tr:not(:last-child)::after {
      content:"";
      position:absolute;
      bottom:-1px;
      left:0;
      height: 1px;
      width:300%;
      background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(176,0,0,.8), rgba(0,0,0,0));
      background-image:    -moz-linear-gradient(left, rgba(0,0,0,0), rgba(176,0,0,.8), rgba(0,0,0,0));
      background-image:     -ms-linear-gradient(left, rgba(0,0,0,0), rgba(176,0,0,.8), rgba(0,0,0,0));
      background-image:      -o-linear-gradient(left, rgba(0,0,0,0), rgba(176,0,0,.8), rgba(0,0,0,0));
    }
    <table>
      <tr><td>A</td><td>B</td><td>C</td></tr>
      <tr><td>D</td><td>E</td><td>F</td></tr>
      <tr><td>G</td><td>H</td><td>I</td></tr>
    </table>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog