如何使div中的滚动条只在必要时可见?

268

我有这个 div:

<div style='overflow:scroll; width:400px;height:400px;'>here is some text</div>

即使文本没有溢出,滚动条始终可见。我想让滚动条仅在必要时可见 - 也就是说,只有当框中有足够的文本需要它们时才可见。就像textarea一样。我该如何做?或者我的唯一选择是对textarea进行样式设置,以使其看起来像一个div?

滚动条仅在必要时可见,当框中有足够的文本需要它们时才显示,而不是始终可见。类似textarea的行为。如何实现此效果?或者将textarea样式更改为类似于div的外观。


这个怎么样?只有在鼠标悬停时才显示滚动条。不确定这对你是否有用。https://dev59.com/gGw05IYBdhLWcg3wnjPC - Ryan Beaulieu
http://codepen.io/anon/pen/QwLeMG 希望这能帮到你 - vishalkin
3
overflow: auto; 在安卓上不起作用 :-( - aioobe
9个回答

510

使用 overflow: auto。只有在需要时才会出现滚动条。

(另外,您还可以仅指定 x 或 y 滚动条:overflow-x: autooverflow-y: auto)。


18
请注意,只有在指定了“max-height”属性时,“overflow-y”才起作用。 - Black
3
overflow-y 不需要 max-height。我从来没有在 overflow-y 中使用过 max-height,但每次都能正常工作。 - Sumafu
2
@Sumafu,根据情况你可能需要它,就像我现在所经历的那样。 - David
2
如果您在溢出元素上使用absolutefixed定位,则需要设置heightmax-height,以防止元素根据页面或视口边界调整大小。 - Scott Schupbach

17

试一下这个:

<div style='overflow:auto; width:400px;height:400px;'>here is some text</div>

11

将CSS blockoverflow属性更改为auto

overflow: auto;

它将仅在需要时自动向左侧添加滚动条。

9

尝试

<div style='overflow:auto; width:400px;height:400px;'>here is some text</div>

5
尝试
<div id="boxscroll2" style="overflow: auto; position: relative;" tabindex="5001">

2

我发现当 div 有或没有文本时,它的高度仍然会显示。因此,你可以使用以下方法获得最佳结果。

<div style=" overflow:auto;max-height:300px; max-width:300px;"></div>

1
overflow : auto 是一个神奇的代码。

<!DOCTYPE html>
<html>
<head>
<style>
div.ex1 {
  background-color: lightblue;
  width: 110px;
  height: 110px;
  overflow: scroll;
}

div.ex2 {
  background-color: lightblue;
  width: 110px;
  height: 110px;
  overflow: hidden;
}

div.ex3 {
  background-color: lightblue;
  width: 110px;
  height: 110px;
  overflow: auto;
}

div.ex4 {
  background-color: lightblue;
  width: 110px;
  height: 110px;
  overflow: clip;
}

div.ex5 {
  background-color: lightblue;
  width: 110px;
  height: 110px;
  overflow: visible;
}
</style>
</head>
<body>

<h1>The overflow Property</h1>

<p>The overflow property specifies whether to clip content or to add scrollbars when an element's content is too big to fit in a specified area.</p>

<h2>overflow: scroll:</h2>
<div class="ex1">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>

<h2>overflow: hidden:</h2>
<div class="ex2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>

<h2>overflow: auto:</h2>
<div class="ex3">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>

<h2>overflow: clip:</h2>
<div class="ex4">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>

<h2>overflow: visible (default):</h2>
<div class="ex5">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>

</body>
</html>


0
你可以尝试以下代码:
  <div style="width: 100%; height: 100%; overflow-x: visible; overflow-y: scroll;">Text</div>

谢谢,这很有帮助。如果您没有特定的高度/宽度(更动态的用户界面),则此解决方案最有效。 - DoomGoober

0

Stackblitz Playgrond

的内容与编程有关。
<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
    #scrollable-content {
        background: #eee;
        height: 150px;
        width: 400px;
        overflow-y: scroll;
    }
    </style>
</head>

<body>
    <div id="original-content"> 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. </div>
    <br />
    <div id="scrollable-content"> 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. </div>
</body>

</html>

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