html - div内的可滚动div
问题描述
我有两个 div (content-title
和content-items
) 嵌入在一个固定的 div ( content
) 中。我希望content
div 隐藏任何溢出它的内容,将content-title
div 固定到位但可content-items
滚动。
在下面的代码片段中,最终<a>
元素不可见,并且content-items
不会垂直滚动。请问这是为什么?
.content {
background: grey;
max-height: 75px;
overflow: hidden;
}
.content-title {
color: blue;
}
.content-items {
color: green;
overflow-y: scroll;
}
.content-items a {
display: block;
}
.content-items a:hover {
background: green;
color: grey;
}
<div class="content">
<div class="content-title">
select a city:
</div>
<div class="content-items">
<a>Foo</a>
<a>Bar</a>
<a>Baz</a>
</div>
</div>
解决方案
- 设置
max-height
为div
您需要可滚动内容的内容。 - 在你的情况下,你设置
max-height
为content
但你设置overflow-y
为content-items
. - 设置
max-height
为content-items
而不是content
.content {
background: grey;
overflow: hidden;
}
.content-title {
color: blue;
}
.content-items {
color: green;
overflow-y: scroll;
max-height: 75px;
}
.content-items a {
display: block;
}
.content-items a:hover {
background: green;
color: grey;
}
<div class="content">
<div class="content-title">
select a city:
</div>
<div class="content-items">
<a>Foo</a>
<a>Bar</a>
<a>Baz</a>
<a>Foo</a>
<a>Bar</a>
<a>Baz</a>
</div>
</div>
推荐阅读
- java - HTTP 状态 404 -> 请求的资源不可用
- c - C语言中的加载库
- javascript - firestore.where() 同时在两个字段上
- android - 通过 TabLayout 向 ViewPager 传输数据(适配器:FragmentPagerAdapter)
- c# - 对 Xamarin.Mac 的 gRPC 支持
- javascript - Sinon 在 stubbing 之后调用原始方法
- javascript - sildeUp 不是 VueJS 中的函数
- r - eval(substitute(expr), data, enclos = parent.frame()) 中的错误:找不到对象'a',这个错误意味着什么?
- kdb - iasc 的真实世界用例和 q kdb 中的排名
- java - 将 TableView 的滚动条向左移动