代码之家  ›  专栏  ›  技术社区  ›  Aram Becker

在firefox中设置表的td高度不起作用

  •  0
  • Aram Becker  · 技术社区  · 7 年前

    我有一张桌子,里面的细胞完全填满了。我给了内容固定的宽度和 height 属于 100% 所以那些变大的元素仍然能够生长细胞。通过简单的 高度 属性使内容的100%高度具有效果。在chrome和edge中,一切正常,但在firefox中,细胞不生长:

    铬:

    Chrome table

    Firefox:

    enter image description here

    如果你想试试:

    table {
      border-spacing: 8px;
      font-size: 14px;
    }
    
    td {
      height: 50px;
    }
    
    td div {
      height: 100%;
      width: 200px;
      background-color: green;
    }
    <table>
      <tr>
        <td>
          <div>
            This div is normal sized.
          </div>
        </td>
        <td>
          <div>
            This div is normal sized.
          </div>
        </td>
      </tr>
      <tr>
        <td>
          <div>
            This div is also normal sized but should size accordingly.
          </div>
        </td>
        <td>
          <div>
            This div is very very big so it gets higher and should affect other divs in the same row. But not in Firefox apparently.
          </div>
        </td>
      </tr>
    </table>

    不确定这是firefox的bug还是chrome的特性,但我理解表大小的方式是表元素不能有固定的大小。而是他们的 width 高度 属性用作 min-width / min-height 它们根据自己的内容成长。是否有快速的解决方法,或者我应该在flexbox布局中重建表?

    更新

    顺便说一句,当我设置一个 高度 在行上 tr height: 100%; 在td上,它在firefox中工作。但后来它被铬破坏了…

    1 回复  |  直到 6 年前
        1
  •  1
  •   Aram Becker    6 年前

    我设法找到了解决办法。我加了几行

    @-moz-document url-prefix() {
        tr {
            height: 50px; // Your min height
        }
        td {
            height: auto;
        }
    }
    

    对我的css来说,现在它似乎在firefox和chrome中正确显示了。不是很干净,但很管用。

    很明显,chrome在50版中采用了firefox的表行为,但由于它破坏了太多的布局,所以恢复了。技巧运用 height: 100%; 因为所有百分比大小都自动转换为 auto . 那就更有意义了。