简体   繁体   English

如何在div上只有一个垂直滚动条

[英]How to have only a vertical scroll bar on a div

I have a table which is in a div. 我有一张表在div中。 The length of the table can vary and I use the div to enforce a height. 表的长度可以变化,我使用div来强制高度。

<div id='CamListDiv'>
<table>
    <thead>
        <th><input type='checkbox' id='selectAll'/></th>
        <th>Local Camera List</th>
    </thead>
    <tbody>
        <tr><td>//camera 1 data //</td></tr>
        <tr><td>...</td></tr>
        <tr><td>//camera x data //</td></tr>
    </tbody>
</table>
</div>

CSS
#CamListDiv {
    height: 340px;
    float: left;
    overflow: auto;
    overflow-y: hidden;
}

My problem is when the vertical scrollbar is needed, the space it occupies causes a horizontal scrollbar to appear. 我的问题是当需要垂直滚动条时,它占用的空间会导致出现水平滚动条。 I don't need the horizontal scrollbar and I tried to use the overflow-y: hidden; 我不需要水平滚动条,我试图使用overflow-y: hidden; css property to hide it but this causes both scrollbars to be hidden. 用于隐藏它的css属性,但这会导致隐藏两个滚动条。

Does anyone know why overflow-y:hidden; 有谁知道为什么overflow-y:hidden; isn't working as expected or how to stop the horizontal scrollbar from being visible? 没有按预期工作或如何阻止水平滚动条可见?

You are hiding the vertical scrollbar, and not the horizontal one. 您正在隐藏垂直滚动条,而不是水平滚动条。

Change overflow-y: hidden; 改变overflow-y: hidden; to overflow-x: hidden; overflow-x: hidden;


Heres a quick demo 这是一个快速演示

I've added content surpassing the height, and a div with a width greater than 340px inside #CamListDiv . 我已经添加的内容超越的高度,和div大于340px内的宽度#CamListDiv The content is only scrollable vertically. 内容只能垂直滚动。

By the way the overflow-y property does not work properly in IE8 and earlier. 顺便说一句,overflow-y属性在IE8及更早版本中无法正常工作。 http://www.w3schools.com/cssref/css3_pr_overflow-y.asp http://www.w3schools.com/cssref/css3_pr_overflow-y.asp

Try setting a style="width:100%;" 尝试设置样式=“宽度:100%;” to the table inside the div, that should hide the horizontal bar because it won't be larger than it's container. 在div内的表中,应该隐藏水平条,因为它不会比它的容器大。

您可以使用css2中的Overflow-y,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM