引导行中的水平可滚动 div

问题描述 投票:0回答:8

我正在尝试制作一个水平可滚动的引导程序行。该行包含包装在 div 中的客户评论。每个推荐div的宽度是

33.333%
.

white-space: nowrap
display: inline-block
不起作用。

我做错了什么?

<div class="row">
    <div class="col-lg-12 text-center">
        <div class="section-title">
           <div class="testimonial_group">
               <div class="testimonial">...</div>
               <div class="testimonial">...</div>
               <div class="testimonial">...</div>
               <div class="testimonial">...</div>
               ...
           </div>
        </div>
    </div>
</div>
html css list twitter-bootstrap-3 scrollable
8个回答
139
投票

请检查。这是你想要达到的目标吗?

horizontal scrolling

带有内联块的 Bootstrap 3

代码笔JSFiddle

/* The heart of the matter */
.testimonial-group > .row {
  overflow-x: auto;
  white-space: nowrap;
}
.testimonial-group > .row > .col-xs-4 {
  display: inline-block;
  float: none;
}

/* Decorations */
.col-xs-4 { color: #fff; font-size: 48px; padding-bottom: 20px; padding-top: 18px; text-align: center; }
.col-xs-4:nth-child(3n+1) { background: #c69; }
.col-xs-4:nth-child(3n+2) { background: #9c6; }
.col-xs-4:nth-child(3n+3) { background: #69c; }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<div class="container testimonial-group">
  <div class="row">
    <div class="col-xs-4">1</div><!--
 --><div class="col-xs-4">2</div><!--
 --><div class="col-xs-4">3</div><!--
 --><div class="col-xs-4">4</div><!--
 --><div class="col-xs-4">5</div><!--
 --><div class="col-xs-4">6</div><!--
 --><div class="col-xs-4">7</div><!--
 --><div class="col-xs-4">8</div><!--
 --><div class="col-xs-4">9</div>
  </div>
</div>

带 flexbox 的 Bootstrap 4

正如Hector评论的那样,内联框解决方案也适用于Bootstrap 4,但需要为

display: block;
添加
.testimonial-group > .row

但是 Bartimaeus 正确地指出 对于 Bootstrap 4,这个结果更适合在 flexbox 的帮助下实现,添加为此提供的样式类:

flex-nowrap
overflow-auto
.

https://codepen.io/glebkema/pen/xxOgaMm

/* Decorations */
.col-4 { color: #fff; font-size: 48px; padding-bottom: 20px; padding-top: 18px; text-align: center; }
.col-4:nth-child(3n+1) { background: #c69; }
.col-4:nth-child(3n+2) { background: #9c6; }
.col-4:nth-child(3n+3) { background: #69c; }
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.2/css/bootstrap.min.css">

<div class="container">
  <div class="row flex-nowrap overflow-auto">
    <div class="col-4">1</div>
    <div class="col-4">2</div>
    <div class="col-4">3</div>
    <div class="col-4">4</div>
    <div class="col-4">5</div>
    <div class="col-4">6</div>
    <div class="col-4">7</div>
    <div class="col-4">8</div>
    <div class="col-4">9</div>
  </div>
</div>


36
投票

在 Bootstrap 4 中,您需要添加

flex-wrap: nowrap;

.testimonial-group > .row

除了Gleb的回答


11
投票

Flexbox 方法

/* The heart of the matter */
.testimonial-group > .row {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
}
.testimonial-group > .row > .col-xs-4 {
  flex: 0 0 auto;
}

/* Decorations */
.col-xs-4 { color: #fff; font-size: 48px; padding-bottom: 20px; padding-top: 18px; }
.col-xs-4:nth-child(3n+1) { background: #c69; }
.col-xs-4:nth-child(3n+2) { background: #9c6; }
.col-xs-4:nth-child(3n+3) { background: #69c; }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="container testimonial-group">
  <div class="row text-center">
    <div class="col-xs-4">1</div><!--
 --><div class="col-xs-4">2</div><!--
 --><div class="col-xs-4">3</div><!--
 --><div class="col-xs-4">4</div><!--
 --><div class="col-xs-4">5</div><!--
 --><div class="col-xs-4">6</div><!--
 --><div class="col-xs-4">7</div><!--
 --><div class="col-xs-4">8</div><!--
 --><div class="col-xs-4">9</div>
  </div>
</div>


5
投票

试试这个:

section-title {
  width: 100%; 
  overflow-x: scroll(or auto);
} 

看看这里的规范.


5
投票

您可以使用:

class="text-nowrap overflow-auto"

考虑这个例子:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<div class="list-group list-group-horizontal text-nowrap overflow-auto">
  <a href="#" class="list-group-item list-group-item-action active">
    Cras justo odio
  </a>
  <a href="#" class="list-group-item list-group-item-action">Dapibus ac facilisis in</a>
  <a href="#" class="list-group-item list-group-item-action">Morbi leo risus</a>
  <a href="#" class="list-group-item list-group-item-action">Porta ac consectetur ac</a>
  <a href="#" class="list-group-item list-group-item-action disabled" tabindex="-1" aria-disabled="true">Vestibulum at eros</a>
</div>

要在没有任何 css 或所有不需要的 div 的情况下完成预期的结果:getbootstrap.com/list-group.


3
投票

将这些行放入您的 CSS 文件中

.section-title {
  overflow: scroll;
  overflow-x: scroll;
  overflow-y:hidden
}

x代表水平,y代表垂直,在嵌套了很多div的父div中应用类代码。


1
投票

我不明白为什么所有这些 no-wrap 和 flex 属性实际上只适用于没有文本的子元素。如果子元素中有文本,

white-space: nowrap;

属性使整个文本在一行中扩展,结果文本与其他所有内容重叠并破坏整个布局。

解决方案就简单多了。简单地,给父元素以下

display: flex;
overflow-x: auto;

然后到子元素以下

min-width: 25%;

注:col size 3 为 25%,col size 4 为 33.33333%,col size 6 为 50%,依此类推。 你有它。基于 bootstrap cols 的水平滚动 div 布局。

这是一个干净的样品。 小提琴


0
投票

我正在使用 Bootstrap 4,

<div class="row overflow-auto flex-nowrap">
      <div class="col-auto"></div>
</div>

我只需将 overflow-auto 和 flex-nowrap 类添加到带有类行的 div 中。这将防止 div 换行并溢出。通过此修改,类为 col-auto 的 div 将在类为 row 的 div 中保持可见,水平滚动条将出现以允许用户滚动浏览溢出的内容。

© www.soinside.com 2019 - 2024. All rights reserved.