【问题标题】:Scale table width to fit columns, without resizing columns to fit table缩放表格宽度以适合列,而无需调整列大小以适合表格
【发布时间】:2014-06-19 06:57:39
【问题描述】:

我有一个放置在 div 包装器内的表格,它有 4 列,宽度如下:400 像素、300 像素、300 像素、300 像素(总宽度为 1300 像素)。我正在寻找的行为是表格水平缩放,从而触发其父包装器的溢出:

.table-wrapper {
    width: 800px;
    overflow-x: auto;
}

table {
    min-width: 100%;
    width: auto;
    border-spacing: 0px;
    table-layout: fixed;
    border-collapse: separate;
}

表格的 HTML 是:

<div class="table-wrapper">
    <table>
        <tr>
            <td width="400px">Col 1</td>
            <td width="300px">Col 2</td>
            <td width="300px">Col 3</td>
            <td width="300px">Col 4</td>
        </tr>
    </table>
</div>

它当前的行为是重新调整列,使它们都适合包装器的 800 像素宽度。我在网上搜索过类似的东西,但无济于事(到目前为止)。有人有什么想法吗?

【问题讨论】:

  • 你能在 jsfiddle.net 上创建一个 mock 吗?

标签: html css html-table resize overflow


【解决方案1】:

只需将表格的宽度设置为 1300 像素。

http://jsfiddle.net/4Eere/

.table-wrapper {
    width: 800px;
    overflow-x: auto;
    background: blue;
}

table {
    width: 1300px;
    border-spacing: 0px;
    table-layout: fixed;
    border-collapse: separate;
}

td { background: red;}

【讨论】:

  • 不幸的是,我不知道表格的宽度是多少,因为数据可以动态加载到列中。它最少有 3 列,但平均有 10 列。
猜你喜欢
  • 2019-05-08
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 2017-06-15
  • 1970-01-01
  • 2011-12-06
  • 2019-11-07
相关资源
最近更新 更多