html - 如何在html css中将滚动条添加到父div?

标签 html css

我想向父 div 添加一个垂直滚动条,因为在下面的设置中, child 的高度远大于父 div 的高度。我在 W3 学校读过,默认情况下 overflow: auto;因此,如果子 div 的高度超过父 div 的高度,它将添加滚动条。但下面的设置并不能证明它是合理的。帮助我理解这一点。
根据 W3 学校 CSS 文档,

If the height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow. How the container will handle the overflowing content is defined by the overflow property.


我们如何证明上述说法是正确的?

.parentDiv {
  background-color: coral;
  height: 50px;
  width: 200px;
}

.childDiv {
  background-color: crimson;
  height: 100px;
  width: 200px;
}
<div class="parentDiv">
  <div class="childDiv">

  </div>
</div>

@Edited:默认情况下 overflow: visible; ,它不是自动的。我读错了。

最佳答案

.parentDiv {
  background-color: coral;
  height: 50px;
  width: 200px;
  overflow-y: auto; /* new stuff */
}

.childDiv {
  background-color: crimson;
  height: 100px;
  width: 200px;
}
<div class="parentDiv">
  <div class="childDiv">

  </div>
</div>

关于html - 如何在html css中将滚动条添加到父div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65473561/

相关文章:

javascript - 虚拟机上的跨浏览器测试 - 问题?

HTML <th> 列属性

html - 垂直 UL,文字位于 LI 的左侧

javascript - 当我调整浏览器大小时,所有元素都会改变它们的位置

php打开电子邮件客户端

jQuery* 从其他表中单独计算多个值

javascript - 第 49 行的 fatal error : Call to a member function fetch_assoc() on a non-object in/home/u161146050/public_html/application/views/test1. php

css - 当使用高度百分比时,这些额外的像素来自哪里?

html - 列表项 li 悬停样式在页面上不起作用

css - 导航栏上的 Bootstrap 'affix' 打破了右边距