仅水平滚动
我正在尝试创建一个水平滚动条,当溢出时它将在页面顶部水平滚动。我已经尝试了与溢出值的不同组合,但 div 中具有水平设置的 inline-div 元素只会创建一个换行符,这意味着水平条毫无意义。这是我的代码:
CSS:
#files_scroll {
width: 100%;
height: 120px;
background-color: #FFFFFF;
overflow-x: scroll;
overflow-y: hidden;
}
.file_icon {
height: 80px;
margin: 7px;
padding-left: 10px;
display: inline-block;
padding: 5px;
float: left;
background-color: #CCCCCC;
opacity: 0.5;
border-radius: 10px;
width: 90px;
}
<div id="main_content">
<div id="files_scroll">
<a class="file_icon">1</a>
<a class="file_icon">2</a>
<a class="file_icon">3</a>
<a class="file_icon">4</a>
<a class="file_icon">5</a>
<a class="file_icon">6</a>
<a class="file_icon">7</a>
</div>
</div>
I'm trying to create a horizontal scrolling bar which when overflowed will scroll horizontally at the top of the page. I have tried different combinations of this with overflow values but the inline-div elements that I have in the div with the horizontal settings just creates a line break meaning that the horizontal bar is pointless. This is my code:
CSS:
#files_scroll {
width: 100%;
height: 120px;
background-color: #FFFFFF;
overflow-x: scroll;
overflow-y: hidden;
}
.file_icon {
height: 80px;
margin: 7px;
padding-left: 10px;
display: inline-block;
padding: 5px;
float: left;
background-color: #CCCCCC;
opacity: 0.5;
border-radius: 10px;
width: 90px;
}
<div id="main_content">
<div id="files_scroll">
<a class="file_icon">1</a>
<a class="file_icon">2</a>
<a class="file_icon">3</a>
<a class="file_icon">4</a>
<a class="file_icon">5</a>
<a class="file_icon">6</a>
<a class="file_icon">7</a>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要水平滚动,则需要内容变得比容器大。浮动元素或内联元素是不可能的:当没有足够的空间时,它们落在“下一行”。
2 个解决方案:
将您的内容放在一个超大元素中。
使用 CSS:
使用非浮动/非内联元素。
它更容易,但是......它使用单行表。可能还有其他选择,但我现在看不到。
我根本没有碰你的CSS,这有效:
If you want horizontal scrolling, you need your content to become larger than the container. It is not possible with floating element or inline element : They fall on the "next line" when there is not enough space left.
2 solutions :
Put your content in an extra-large element.
with CSS:
Use non-floating/non-inline element.
It is easier but... it use a single row table. There are probably alternatives, but I can't see one right now.
I didn't touch your CSS at all and this works :