在 HTML 中设置表格的高度无效

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

Setting the height of a table in HTML has no effect

htmlcsshtml-table

提问by Em Sta

Why does this table height not function?

为什么这个桌子高度不起作用?

<table border=1 bgcolor="green" width=80% height="30%">
    <tr>
        <td rowspan="2" >
            This is 1st row 1st column
        </td>
        <td >
            2
        </td>
    </tr>
</table>

http://jsfiddle.net/zQNS4/

http://jsfiddle.net/zQNS4/

回答by oezi

just add the following to your css:

只需将以下内容添加到您的 css 中:

html, body{
    height: 100%;
}

As other said, a tabledoesn't have a height-attriute, but most browsers intrepet that anyway. you can see the result on jsfiddle.

正如其他人所说, atable没有height-attriute,但无论如何大多数浏览器都会这样做。你可以在 jsfiddle 上看到结果

The reason you need to do this is that the parent element of anything that should have a height in % must have a height too (as Shadow Wizard said: "30% of what exactly?" - the parent has to have a height).

您需要这样做的原因是任何应该具有 % 高度的父元素也必须具有高度(正如 Shadow Wizard 所说:“究竟是什么的 30%?” - 父元素必须具有高度)。

回答by Kevin Bowersox

The tabletag does not contain a heightattribute. Try setting the height of the table using CSS styling.

table标签不包含height属性。尝试使用 CSS 样式设置表格的高度。

table{
   height: 30%;
}

回答by arvind

<div style="height: 200px; overflow-y: scroll;">
    <table border=1 bgcolor="green">
        <tr>
            <td rowspan="2" >
                This is 1st row 1st column
            </td>
            <td>
                 2
            </td>
        </tr>
   </table>
</div>

回答by GKirov

I just had the same issue. I have table inside a container ( div config-table), just setting heightdidn't work for me. I had to set overflow: auto( for my case autowas needed ) and now work

我只是有同样的问题。我在容器( div config-table)中有一张桌子,只是设置height对我不起作用。我必须设置overflow: auto(因为我的情况auto需要)现在工作

.config-table {
   height: 350px;
   overflow: auto;
}