如何在必要时才能使div上的滚动条可见?

问题描述 投票:125回答:6

我有这个div:

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

即使文本没有溢出,滚动条也始终可见。我想让滚动条仅在必要时可见 - 也就是说,只有当盒子中有足够的文本需要它们时才可见。像textarea一样。我该怎么做呢?或者是我唯一的选择样式textarea所以它看起来像一个div?

css html scrollbar
6个回答
260
投票

使用overflow: auto。滚动条仅在需要时显示。

(旁注,您也可以仅指定x或y滚动条:overflow-x: autooverflow-y: auto)。


13
投票

试试这个:

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

6
投票

尝试

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

6
投票

尝试

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

0
投票

我发现有div的高度仍然显示,当它有文字或没有。因此,您可以使用它来获得最佳效果。

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

0
投票

您可以尝试下面的一个:

  <div style="width: 100%; height: 100%; overflow-x: visible; overflow-y: scroll;">Text</div>
© www.soinside.com 2019 - 2024. All rights reserved.