简体   繁体   English

保持表格宽度固定

[英]Maintain table width fixed

I have a table with dynamic data. 我有一张动态数据表。

Depending on data, it expands or collapses. 根据数据,它会扩展或折叠。

I don't want it. 我不想要它。 I want a fixed width that never expand or collapse. 我想要一个永不膨胀或折叠的固定宽度。

How can I do that? 我怎样才能做到这一点?

I've already tried <table width="300px"></table> , but it doesn't work. 我已经尝试过<table width="300px"></table> ,但它不起作用。

以下应该有效:

<table style="width:300px;table-layout:fixed"></table>

You'd set the width of each column explicitly like so: 您可以明确地设置每列的宽度,如下所示:

td.a {
   width:100px;
}
td.b { 
   width:200px;
}
...

<table>
<tr><td class='a'>A</td><td class='b'>B</td></tr>
<tr><td class='a'>A</td><td class='b'>B</td></tr>
<tr><td class='a'>A</td><td class='b'>B</td></tr>
<tr><td class='a'>A</td><td class='b'>B</td></tr>
</table>

Either old ugly way 要么是丑陋的丑陋方式

<table width="300"></table>

or the CSS way 或CSS方式

<table style="width:300px"></table>

Of course, the best way is to use a separate stylesheet file, call it for example style.css : 当然,最好的方法是使用单独的样式表文件,例如style.css调用它:

table {
  width: 300px;
}

and your html document: 和你的HTML文件:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style.css"/>
  </head>
  <body>
    <table>
    ...
    </table>
  </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM