【问题标题】:CSS: Gradients repeating in Chrome when set to tbody/theadCSS:当设置为 tbody/thead 时,渐变在 Chrome 中重复
【发布时间】:2011-06-15 11:16:03
【问题描述】:

这里显示了我遇到的问题。基本上,当我在thead 上放置渐变时,Chrome 会在每个单元格中重复该渐变...实际所需的结果是 firefox 产生的结果——整个thead 的实心渐变。

关于如何解决这个问题的任何想法?

这是我的 css:

thead.header {
    font-weight: bold;
    border-bottom: 1px solid #9C9C9C;
    background-repeat: no-repeat;
    background: #C6C6C6;
    background: -moz-linear-gradient(top, #DEDEDE 0%, #BDBDBD 80%, #BBB 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#DEDEDE), color-stop(80%,#BDBDBD), color-stop(100%,#BBB));
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#DEDEDE', endColorstr='#BBB',GradientType=0 );
}

如果有帮助,这里是html:

  <table style="width:100%" cellpadding="0" cellspacing="0">
    <thead class="header">
      <tr>
        <th width="200px">Actor</th>
        <th rowspan="3">Character</th>
      </tr>
      <tr>
        <th>Gender</th>
      </tr>
      <tr>
        <th>Age</th>
      </tr>
    </thead>

    <tbody class="odd">
      <tr>
        <td width="200px">Test</td> 
        <td rowspan="3">Test</table>
        </td>
      </tr> 
      <tr>
        <td>Male</td>
      </tr> 
      <tr>
        <td>25</td>
      </tr>
    </tbody>
  </table>

【问题讨论】:

  • 你能发布你应用那个 CSS 的 HTML sn-p 吗?
  • 我也遇到这个问题
  • 请将已发布的答案之一标记为正确,或提供更多信息,以便我们了解如何为您提供帮助。
  • 在 Chrome v80 中已解决,但在 Safari (iOS13) 中仍然存在错误

标签: html css google-chrome gradient


【解决方案1】:

在thead上设置background-attachment:fixed;,它会正常工作

【讨论】:

  • 这是最适合我的。无需重新定位任何 TH 元素。
  • 很棒的修复!谢谢!如此简单,就像一个魅力!
  • 这使渐变相对于视口,这使它看起来根本不是渐变,或者使渐变看起来根据屏幕大小而有所不同。为了让它看起来正常,你需要一个固定数量的色标(比如 50px)。 linear-gradient(black, white 50px)
【解决方案2】:

这是一个不幸的问题,但有一个适用于大多数布局的简单解决方法:

在表格本身上设置渐变,并给&lt;td&gt;s 一个纯色。

小提琴:http://jsfiddle.net/vudj9/

【讨论】:

    【解决方案3】:

    我认为这是因为 background 样式不能应用于 thead 元素。

    【讨论】:

    • 我不明白为什么不呢?
    【解决方案4】:

    将渐变应用于th 元素而不是thead 并使用rowspan 属性选择器似乎有效。如果两个标题行的高度是灵活的,因为您需要知道示例中的中间范围颜色#7E7E7E,这不会很好地工作。查看示例http://jsfiddle.net/wSWF9/2/

       table {
           border: 1px solid #ccc;
           border-collapse: collapse;
       }
    
       table thead th[rowspan="2"] {
          background-image: linear-gradient(to bottom, #ccc, #333);
          background-image: -webkit-linear-gradient(top, #ccc, #333);
          background-repeat: repeat-x;
       }
    
       table thead tr th {
          background-image: linear-gradient(to bottom, #ccc, #7E7E7E);
          background-repeat: repeat-x;
       }
    
       table thead tr + tr th {
          background-image: linear-gradient(to bottom, #7E7E7E, #333);
          background-repeat: repeat-x;
       }
    

    【讨论】:

      【解决方案5】:

      如果你给thead 一个值display: table-caption; 它可以工作。

      需要一些额外的 CSS 来定位 th 内容。

      thead.header {
        display: table-caption;
        font-weight: bold;
        border-bottom: 1px solid #9C9C9C;
        background-repeat: no-repeat;
        background: #C6C6C6;
        background: -moz-linear-gradient(top, #DEDEDE 0%, #BDBDBD 80%, #BBB 100%);
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #DEDEDE), color-stop(80%, #BDBDBD), color-stop(100%, #BBB));
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#DEDEDE', endColorstr='#BBB', GradientType=0);
      }
      
      thead.header th {
        text-align: center;
      }
      <table style="width:100%" cellpadding="0" cellspacing="0">
        <thead class="header">
          <tr>
            <th width="200px">Actor<br><br></th>
            <th rowspan="3">Character</th>
          </tr>
          <tr>
            <th>Gender</th>
          </tr>
          <tr>
            <th>Age</th>
          </tr>
        </thead>
      
        <tbody class="odd">
          <tr>
            <td width="200px">Test</td>
            <td rowspan="3">Test</td>
          </tr>
          <tr>
            <td>Male</td>
          </tr>
          <tr>
            <td>25</td>
          </tr>
        </tbody>
      </table>

      【讨论】:

      • 太棒了!我认为这是最好的解决方案。但是你的意思是需要一些额外的 CSS 来定位第 th 内容?
      • 至少在我的 chrome 中,表格不再是 100% 宽度的真实表格。您必须手动设置 ths 的宽度。
      • 对我来说不是解决方案。它破坏了表结构。
      【解决方案6】:

      嗯,一个规避问题的方法是不要使用单独的单元格,而是使用&lt;br /&gt;

      我意识到这不是一个很好的解决方法。

      您的代码:http://jsbin.com/ozuhi5

      您的代码已修复:http://jsbin.com/ozuhi5/2

      &lt;thead&gt;:

      <thead class="header">
        <tr>
          <th width="200">
            Actor<br />
              Gender<br />
              Age
          </th>
          <th>Character</th>
        </tr>
      </thead>
      

      【讨论】:

      【解决方案7】:

      这是 google chrome 上的一个错误,请在 this Google page about issues on Chrome 上阅读更多相关信息。

      我在尝试设置 tbody 元素的样式时确实遇到了同样的问题,这是我用来修复我的代码而不破坏其他浏览器支持的解决方案:

      table>tbody {
          background-image: -moz-linear-gradient(270deg, black, white); /* keep the right code     for other browsers */
          background-image: -ms-linear-gradient(270deg, black, white);
          background-image: -o-linear-gradient(270deg, black, white);
          background-image: linear-gradient(to bottom, black, white);
      
          background-color: transparent; /* needed for chrome*/
          background-image:-webkit-linear-gradient(0deg, rgba(0,0,0,0),rgba(0,0,0,0)); /*del the linear-gradient on google chrome */
      }
      table {  /*fix tbody issues on google chrome engine*/
          background-image: -webkit-linear-gradient(270deg, rgba(0,0,0,0) 39px, black 0px,white ); /* 359px is the calculated height of the thead elemx */
          border-spacing: 0; /*delete border spacing */
      }
      

      Fiddle上查看演示

      【讨论】:

      • 在 Chrome 中已修复,但问题仍然存在于 Safari。
      猜你喜欢
      • 2012-10-06
      • 1970-01-01
      • 2023-03-31
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多