CSS:无法为滚动设置躯干高度(%)

n a*_*n a 5 html css overflow

我正在尝试在保持标题固定的同时滚动表内容的CSS解决方案。

CSS代码1:

<style type='text/css'>
    *   {
    padding:0;
    margin:0;
    border:0;
    }
    #ListData   {
        display:block;
        width:100%;
        height:100%;
        float:left;
        background:#ffffff;
    }
    #ListData table {
                    width:100%;
                }
    #ListData thead {
                    position:relative;
                    width:100%;
                    height:20px;
                    overflow:auto;
                    background:#0099cc;
                }
    #ListData tbody {
                    position:relative;  
                    width:100%;
                    height:300px;
                    overflow:auto;
                }               
    </style>
Run Code Online (Sandbox Code Playgroud)

此样式的输出在这里:http : //riotdesign.in/test.php

这在Firefox中很好用(我现在不在乎IE。)

但是,如果我使用百分比而不是px或em:

<style type='text/css'>
    *   {
    padding:0;
    margin:0;
    border:0;
    }
    #ListData   {
        display:block;
        width:100%;
        height:100%;
        float:left;
        background:#ffffff;
    }
    #ListData table {
                    width:100%;
                }
    #ListData thead {
                    position:relative;
                    width:100%;
                    height:10%;
                    overflow:auto;
                    background:#0099cc;
                }
    #ListData tbody {
                    position:relative;  
                    width:100%;
                    height:50%;
                    overflow:auto;
                }               
    </style>
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:http : //riotdesign.in/test2.php

我究竟做错了什么?:)

Rob*_*len 3

在某些时候,您的对象需要有一个block具有确定大小的级别父元素才能获得您想要的滚动。

具体来说,在您的情况下,要么包含 DIV ListData,要么ListData table需要具有特定的高度。

添加高度和宽度的百分比值时,请确保您知道“x% 的...?”的答案。并沿着链向上跟踪该问题,直到遇到具有硬边界的问题。如果你达到了Body元素但仍然没有硬边界,你将不会得到你想要的效果。

我在 Firefox 3.5.xx 中测试了这两个并滚动tbody

#ListData
{
    display:block;
    width:100%;
    height:200px;
    float:left;
    background:#ffffff;
}
Run Code Online (Sandbox Code Playgroud)

或者

#ListData table {
   width:100%;
   height: 200px;
}
Run Code Online (Sandbox Code Playgroud)

这也有效:

body
{ 
  width: 100%;
  height:600px;
}
Run Code Online (Sandbox Code Playgroud)