如何固定表格列宽?

问题描述 投票:3回答:4

我有一个有两列的表,第一列65%,第二列35%。它完全适合100%的屏幕,如果第一列中有足够的文本来填充65%,但如果它是空的(或少量文本) - 则第一列缩小,第二列扩展。

如何使用或不使用文本将第一列始终显示65%的屏幕?

html html-table
4个回答
2
投票

试试这个:

<table class="fixed_width">
    <col width="65%" />
    <col width="35%" />
    <tr>
        <td>your data</td>
        <td>your data</td>
    </tr>
</table>

和CSS:

table.fixed_width { table-layout:fixed; }
table.fixed_width td { overflow: hidden; }

0
投票
<table>
    <tr>
        <td style="width: 65%;">
            first column
        </td>
        <td style="width: 35%;">
            second column
        </td>
    </tr>
</table>

第一列将始终保持65%,除非你在里面放一些大于65%的东西(例如一张大图)


0
投票

尝试添加此CSS:

table {
    table-layout: fixed;
    empty-cells: show;
}

https://developer.mozilla.org/en-US/docs/CSS/empty-cells

https://developer.mozilla.org/en-US/docs/CSS/table-layout

@ kalpesh对<colgroup>的推荐也很好。

https://developer.mozilla.org/en-US/docs/HTML/Element/colgroup


0
投票

试试这个css:

.table {
      table-layout: fixed;
      border-collapse: collapse;
      width: 100%;
    }

    .td1 {
      border: 1px solid #000;
      width: 65%;
    }

    .td2{border: 1px solid #000;
      width: 35%;
    }

<table class="table">
    <tr>
        <td class="td1">data</td>
        <td class="td2">data</td>
    </tr>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.