Html表中的最大高度

时间:2013-11-29 21:19:13

标签: html css

我创建了一个表格,我希望height800px

如果它溢出,我想制作滚动条,但标题不会滚动。

所以我尝试添加这个CSS:

overflow: scroll;
max-height:800px;

但这对我不起作用。这是整个html文件。问题是什么?

CSS

table{
    overflow: scroll;
   text-align: center;
   margin: auto;
   max-height:800px;
}
table, td, th
{
border:1px solid green;

}
th
{
background-color:green;
color:white;
}

和HTML

<table >
  <tr>
   <th>field a</th>
   <th>field b</th>
   <th>field c</th>
   <th>field e</th>
  </tr>
  <tr>
   <td>3534534</td>
   <td>עו"ש</td>
   <td>6,463</td>
   <td>4,000</td>
  </tr>
  <tr>
   <td>3534534</td>
   <td>עו"ש</td>
   <td>6,463</td>
   <td>4,000</td>
  </tr>
</table>

感谢!!!

4 个答案:

答案 0 :(得分:37)

您可以将表格包装在div中,并将max-heightoverflow: scroll提供给该div

<div class="pane">
    <table>
        <tr>
            <th>field a</th>
            <th>field b</th>
            <th>field c</th>
            <th>field e</th>
        </tr>
        <tr>
            <td>3534534</td>
            <td>עו"ש</td>
            <td>6,463</td>
            <td>4,000</td>
        </tr>
        ...
    </table>
</div>
.pane {
    display: inline-block;
    overflow-y: scroll;
    max-height:200px;
}
table {
    text-align: center;
    margin: auto;
}

JSFiddle

Pure CSS Scrollable Table with Fixed Header还有另一个没有包装器div的解决方案,但行周围还有theadtbody。您将thead和tbody设置为display: block并向tbody提供max-heightoverflow-y。这样可以在滚动行时使标题保持原位。但与往常一样,它带有价格标签。您必须指定列宽,因为由于display: block

,thead和tbody不同步
<table>
    <thead>
        <tr>
            <th>field a</th>
            <th>field b</th>
            <th>field c</th>
            <th>field e</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>3534534</td>
            <td>עו"ש</td>
            <td>6,463</td>
            <td>4,000</td>
        </tr>
        ...
    </tbody>
</table>
thead {
    display: block;
}
tbody {
    display: block;
    overflow-y: scroll;
    max-height:200px;
}
thead th, tbody td {
    width: 70px;
}

JSFiddle

更新

在该网站的源代码中,有关于IE和IE6的评论。它设置

thead tr {
    position: relative
}

并使用

定义表的宽度
table {
    float: left;
    width: 740px
}

这与IE10有关,我不知道。

答案 1 :(得分:4)

使用另一个元素来包装整个表,并将这些属性提供给他:

#table-limiter {
max-height:800px;
overflow: scroll;
}

HTML示例:

<div id="table-limiter">
   <table>
      ...
   </table>
</div>

造型表有时是一个麻烦:(

答案 2 :(得分:2)

这是一种方法。

使用CSS添加包装div:

#set-height {
    display: inline-block;
    height:800px;
    overflow: scroll;
}

小提琴:http://jsfiddle.net/JmLFz/

答案 3 :(得分:0)

将表格包裹在div标签中,并将div的高度设置为800px,并将溢出设置为auto。

<div style="height:800px; overflow:auto;">
//Table goes here
</div>

然后,如果您的桌子高度超过800px,则div中会出现一个滚动条,允许您滚动表格。

相关问题