隐藏HTML水平滚动条但不隐藏垂直滚动条

283

我有一个宽度固定但高度可变的HTML文本域。我希望设置overflow:scroll以显示垂直滚动条,但不显示水平滚动条。由于特定情况下无法使用overflow:auto,因此我不能使用它。

我知道在CSS2中无法仅显示垂直而不显示水平滚动条。是否可以使用JavaScript隐藏水平滚动条?


3
不,使用CSS2和overflow:scroll;,没有办法显示其中一个滚动条而隐藏另一个滚动条。这是overflow:scroll的固有属性;JavaScript只能做CSS允许的事情。但是,我猜想你可能可以使用overflow:auto,只是你不知道如何使用。能否详细说明一下“与您的情况相关的其他事项”? - tloflin
9个回答

585

您可以像这样使用CSS:

overflow-y: scroll;
overflow-x: hidden;

3
我看到这是CSS3,但在我测试时在Firefox上无法使用。同时我也发现这个属性只能在早期的IE浏览器中使用。 - William Jones
@wiliamjones - 这在火狐浏览器中有效...您有示例页面吗?可能是其他布局原因导致它无法工作。 - Nick Craver
@william - 这是一个完整的示例,可以在Firefox中进行测试 :) http://jsfiddle.net/qpZ8k/ - Nick Craver
你说得对,它在Firefox上可以工作,但是Prototype JavaScript库与该属性不兼容,不过这很容易解决。 这个属性在各种浏览器中是否普遍可靠? - William Jones
@williamjones - 是的,这通常是这些事情的情况,浏览器先实现它,然后作为事后的想法成为标准。 - Nick Craver
这个能和CSS中的word-wrap: break-word;一起使用吗? - Momoro

31

使用 CSS。它比 JavaScript 更简单、更快。

overflow-x: hidden;
overflow-y: scroll;

22

通过添加以下代码完全禁用水平滚动条。

body{
  overflow-x: hidden;
  overflow-y: scroll;
}

9

在HTML表单框中使用wrap=virtual可以去掉框底部的水平滚动条:

  <textarea name= "enquiry" rows="4" cols="30" wrap="virtual"></textarea>

在这里查看示例http://jsbin.com/opube3/2(已在FF和IE上测试)


3
selector{
 overflow-y: scroll;
 overflow-x: hidden;
}

以下是一个带有代码片段和jsfiddle链接的工作示例:https://jsfiddle.net/sx8u82xp/3/

在此输入图片描述

.container{
  height:100vh;
  overflow-y:scroll;
  overflow-x: hidden;
  background:yellow;
}
<div class="container">

<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>

<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>

<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>

<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>

</div>


2
<div style="width:100px;height:100px;overflow-x:hidden;overflow-y:auto;background-color:#000000">

2
.combobox_selector ul {
    padding: 0;
    margin: 0;
    list-style: none;
    border:1px solid #CCC;
    height: 200px;
    overflow: auto;
    overflow-x: hidden;
}

设置滚动条大小为200像素,overflow-x 隐藏任何水平滚动条。


0

对我来说:

.ui-jqgrid .ui-jqgrid-bdiv {
   position: relative;
   margin: 0;
   padding: 0;
   overflow-y: auto;  <------
   overflow-x: hidden; <-----
   text-align: left;
}

当然要去掉箭头


0
你可以这样做:
overflow-x:hidden;
overflow-y:auto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接