内联块元素的水平滚动

18

我有一些 inline-block 的 div,但当它们到达屏幕的末尾时,它们会换行而不是水平滚动。有人能帮帮我吗?这是一个示例

<div class="m_wrap">
<div class="dx">
    <div class="xc">1</div>
    <div class="xc">2</div>
    <div class="xc">3</div>
    <div class="xc">4</div>
    <div class="xc">5</div>
    <div class="xc">6</div>
    <div class="xc">7</div>
    <div class="xc">8</div>
    <div class="xc">9</div>
    <div class="xc">10</div>

   </div>
  </div>

层叠样式表

.m_wrap{
width:100%;
height:100px;
}
.dx{
   width:100%;
height:100px;
overflow:scroll;
}
.xc{
display:inline-block;
width:100px;
height:80px;
border:1px solid;
line-height:80px;
text-align:center;
margin-bottom:4px;
 }
3个回答

48

dx 类上使用 white-space: nowrap;

Fiddle

.dx{
    width:100%;
    height:100px;
    overflow:scroll;
    white-space: nowrap;
}

5
隐藏y轴溢出并使用nowrap。
.dx {
    height: 100px;
    overflow-y: hidden;
    white-space: nowrap;
    width: 100%;
}

FIDDLE


1

如果使用transform,可以得到更先进的解决方案。

HTML

<div class="horizontal-scroll-wrapper">
  <div>item 1</div>
  <div>item 2</div>
  <div>item 3</div>
  <div>item 4</div>
  <div>item 5</div>
  <div>item 6</div>
  <div>item 7</div>
  <div>item 8</div>
  <div>item 9</div>
  <div>item 10</div>
</div>

CSS
div {
  box-sizing: border-box;
}

.horizontal-scroll-wrapper {
  background: #abc;
  display: block;
  max-height: 500px;
  overflow-y: auto;
  overflow-x: hidden;
  padding-top: 60px;
  position: absolute;
  transform: rotate(-90deg) translateY(-80px);
  transform-origin: right top;
}

.horizontal-scroll-wrapper > div {
  background: #cab;
  height: 60px;
  margin: 10px;
  padding: 5px;
  transform: rotate(90deg);
  transform-origin: right top;
  width: 60px;
}

CodeSandbox,受到https://css-tricks.com/pure-css-horizontal-scrolling/的启发。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接