Html 引导行中的水平可滚动 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38901166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:41:32  来源:igfitidea点击:

Horizontal scrollable div's in a bootstrap row

htmlcsslisttwitter-bootstrap-3scrollable

提问by Codehan25

I'm trying the make a horizontal scrollable bootstrap row. The row contains customer reviews wrapped in div's. The width of each testimonial div is 33.333%.

我正在尝试制作一个水平可滚动的引导行。该行包含包裹在 div 中的客户评论。每个推荐 div 的宽度为33.333%.

white-space: nowrapand display: inline-blockdoesn't work.

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

What am I doing wrong?

我究竟做错了什么?

<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>

回答by Gleb Kemarsky

Please check. Is it what you want to achieve?

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

horizontal scrolling

水平滚动

CodePen •  JSFiddle

CodePen •  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; }
.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>

回答by Martin K.

In Bootstrap 4 you will need to add

在 Bootstrap 4 中,您需要添加

flex-wrap: nowrap;

to

.testimonial-group > .row

in addition to Gleb's answer

除了 Gleb 的回答

回答by Vally Pepyako

Try this:

尝试这个:

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

Look at the spec here.

看看这里的规范

回答by Abhinash Majhi

put these lines in your CSS file

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

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

the x is for horizontal and y is for vertical, apply class code in parent div in which many div's are nested.

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

回答by Nandeep Barochiya

The Flexbox Method

弹性盒方法

/* 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>

回答by Smolikas

I don't understand why so much struggling with all these no-wrap and flex attributes which actually only work on child elements with no text. In case there is text inside the child elements, the

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

white-space: nowrap;

attribute makes the whole text expand in one line having as a result text to overlap everything else and break the whole layout.

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

The solution is much simpler. Simply, give to the parent element the following

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

display: flex;
overflow-x: auto;

and then to the child elements the following

然后到子元素以下

min-width: 25%;

Note: 25% for col size 3, 33.33333% for col size 4, 50% for col size 6 and so on. And there you have it. A horizontally scrollable div based on bootstrap cols for the layout.

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

Here is a clean sample. Fiddle

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