【发布时间】:2017-04-18 22:14:36
【问题描述】:
我正在尝试为网站构建响应式标题组件。标题有position: fixed,所以它不会滚动,但包含一个水平滚动的选项列表。
我使用this tutorial 构建了水平滚动的内部结构,以便可以滚动标题项列表但不会显示滚动条。查看下面的代码,看看它在 fiddle 中的工作情况。
HTML
<body>
<main class="fixed-container">
<div class="scroll-container">
<ul class="scroll-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</main>
</body>
CSS
.fixed-container {
position: fixed;
height: 75px;
overflow: hidden;
right: 0;
left: 0;
border-bottom: 2px solid black;
}
.scroll-container {
height: 100%;
overflow-x: auto;
overflow-y: hidden;
box-sizing: content-box;
padding-bottom: 17px;
}
.scroll-list {
list-style: none;
display: flex;
height: 100%;
width: 1000px;
margin: 0;
padding-left: 0;
}
.scroll-list li {
flex: 1 0;
text-align: center;
padding-top: 2em;
border-bottom: 2px solid red;
margin-left: 5px;
margin-right: 5px;
}
<div class="scroll-container">,与height: 100%,按预期填充<main class="fixed-container"> 的高度。然而,div 中的<ul> 也有height: 100%,并没有填充其父级的高度。 <li>s 底部的红色边框和<main class="fixed-container"> 底部的黑色边框之间有空格。
请注意,如果从 <div class="scroll-container"> 中删除了 overflow-x: auto 和 overflow-y: hidden,或者如果从 <main class="fixed-container"> 中删除了 right: 0,则高度的行为符合预期,并且红色边框与顶部齐平黑色边框。
如何让<ul> 适合其父级的高度,以便红色边框直接位于黑色边框的顶部?
【问题讨论】:
-
overflow-x: overlay;怎么样 -
奇怪的行为。看来差距是由于浏览器为滚动条轨道从
overflow上的.scroll-container规则腾出空间造成的。 -
overflow: overlay不在规范中。我不会依赖它。 w3.org/TR/CSS22/visufx.html#overflow -
请在问题本身的minimal reproducible example 中提供所有相关代码,而不仅仅是在第三方网站上。
-
谢谢,@MikeMcCaughan!我已将相关代码添加到问题中。