Html 将表格列宽调整为内容大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2537285/
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 02:35:18  来源:igfitidea点击:

Adjust table column width to content size

htmlcsscss-tables

提问by Rui Carneiro

Form(with firebug highlight hover the td):

表单(用萤火虫高亮悬停 td):

example

例子

Label column CSS:

标签列 CSS

width: auto;
padding: 0;
margin: 0;

The problem:

问题

Why my left columns have that invisible "padding-right"? It's not possible to shrink columns to fit their content?

为什么我的左列有那个不可见的“填充右”?不可能缩小列以适应其内容?

--EDIT--

- 编辑 -

The problem was the table itself. I defined table with "width: 100%". Removed that and the problem is gone.

问题是桌子本身。我用“宽度:100%”定义了表格。删除它,问题就消失了。

回答by Rui Carneiro

The problem was the table width. I had used width: 100%for the table. The table columns are adjusted automatically after removing the width tag.

问题是表格宽度。我曾用于width: 100%桌子。移除宽度标签后,表格列会自动调整。

回答by Nick Jameson

If you want the table to still be 100% then set one of the columns to have a width:100%; That will extend that column to fill the extra space and allow the other columns to keep their auto width :)

如果您希望表格仍为 100%,则将其中一列设置为宽度:100%;这将扩展该列以填充额外空间并允许其他列保持其自动宽度:)

回答by fl00r

maybe problem with margin?

也许保证金有问题?

width:auto;
padding: 0px;
margin: 0px