Html 保持表格宽度固定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3409995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:59:33  来源:igfitidea点击:

Maintain table width fixed

htmlhtml-table

提问by ozsenegal

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>,但它不起作用。

回答by ironsam

The following should work:

以下应该工作:

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

回答by Dave Markle

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>

回答by Johan

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>