水平滚动绝对定位的元素?

时间:2017-08-04 22:43:34

标签: html css

以下代码有什么问题?所需的行为是#library { position: absolute; bottom: 0; left: 0; right: 0; height: 60px; overflow-x: scroll; overflow-y: hidden; } body { overflow: hidden; } .item { display: inline-block; width: 50px; height: 50px; margin: 5px; border: 1px solid gray; }元素跨越“屏幕”的底部并水平滚动。

<div id="library">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>
inp1

1 个答案:

答案 0 :(得分:3)

.item元素在到达#library的末尾时换行。由于overflow-y: hidden,您无法看到多行包装元素。将white-space: nowrap;添加到#library以防止包装物品。

#library {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  overflow: scroll;
  overflow-y: hidden;
  white-space: nowrap;
}

body {
  overflow: hidden;
}

.item {
  display: inline-block;
  width: 50px;
  height: 50px;
  margin: 5px;
  border: 1px solid gray;
}
<div id="library">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>