<分区>
我需要创建一个水平滚动的容器类,以便在非常大的应用程序中通用。
我想到了这个:
.horizontal-scroller {
background-color: #ccc;
display: flex;
overflow-x: auto;
width: 100%;
}
button {
white-space: nowrap;
}
button::before {
content: 'A button with a label';
}
<div class="horizontal-scroller">
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
</div>
<p>Scrollable Container</p>
<div class="horizontal-scroller">
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
</div>
<p>Scrollable Container with no overflow</p>
现在对于第二种情况(没有溢出)我需要容器将元素右对齐,这就是我添加的原因
justify-content: flex-end;
.horizontal-scroller {
background-color: #ccc;
display: flex;
justify-content: flex-end;
overflow-x: auto;
width: 100%;
}
button {
white-space: nowrap;
}
button::before {
content: 'A button with a label';
}
<div class="horizontal-scroller">
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
</div>
<p>Scrollable Container</p>
<div class="horizontal-scroller">
<button type="button"></button>
<button type="button"></button>
<button type="button"></button>
</div>
<p>Scrollable Container with no overflow</p>
不幸的是,这会消除溢出容器的可滚动性。
问题:为什么会发生这种情况,我该如何通过尽可能少的更改解决这个问题?
我需要坚持使用 display: flex;
。