我想为卡片类实现一个水平滚动条,如本例所示。我试过overflow-x:scroll
,但没有幸运。任何人都可以建议我如何实现滚动条,使卡片水平流动?
感谢
.card {
float: left;
width: 33.333%;
padding: .75rem;
margin-bottom: 2rem;
border: 0;
}
.card > img {
margin-bottom: .75rem;
}
.card-text {
font-size: 85%;
}
<div class="container">
<div class="row">
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
</div>
</div>
答案 0 :(得分:4)
通过添加这些属性来修改您的.row
课程,或者如果您不想篡改.row
课程,请创建一个新课程。
.row {
display: flex;
overflow-x: scroll;
}
有关Flex
的更多信息:https://css-tricks.com/snippets/css/a-guide-to-flexbox/
我将上述属性作为内联样式添加到HTML代码段<div class="row" style="display: flex; overflow-x: scroll">
.card {
float: left;
width: 33.333%;
padding: .75rem;
margin-bottom: 2rem;
border: 0;
}
.card > img {
margin-bottom: .75rem;
}
.card-text {
font-size: 85%;
}
&#13;
<div class="container">
<div class="row" style="display: flex; overflow-x: scroll">
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
</div>
</div>
&#13;
答案 1 :(得分:2)
使用FlexBox
制作水平滚动条非常容易。如果父级为display: flex;
,则float
中的effect
将不会child element
。检查代码段的工作原理。
.row{
align-items: stretch;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
overflow-x: auto;
overflow-y: hidden;
}
.card {
/*float: left;*/
max-width: 33.333%;
padding: .75rem;
margin-bottom: 2rem;
border: 0;
flex-basis: 33.333%;
flex-grow: 0;
flex-shrink: 0;
}
.card > img {
margin-bottom: .75rem;
}
.card-text {
font-size: 85%;
}
<div class="container">
<div class="row">
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
</div>
</div>
答案 2 :(得分:1)
我添加了.card-wrap
类来包装card
并添加了css:
.card-wrap {
overflow-x: scroll;
display: flex;
}
.card {
float: left;
width: 33.333%;
padding: .75rem;
margin-bottom: 2rem;
border: 0;
}
.card > img {
margin-bottom: .75rem;
}
.card-text {
font-size: 85%;
}
.card-wrap {
overflow-x: scroll;
display: flex;
}
<div class="container">
<div class="row">
<div class="card-wrap">
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card">
<img src="http://placehold.it/200x150">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
</div>
</div>
</div>
答案 3 :(得分:0)
我修改了我的代码.overlay-content类,并添加了.overlay-content的css
<!-- language: lang-html -->
<div class="container">
<div class="row">
<div class="overlay-content">
<div class="card">
<img src = "http://placehold.it/200x150" >
< p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.This content is a little bit longer.</p>
</div>
<div class="card">
<img src = "http://placehold.it/200x200" >
< p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.This content is a little bit longer.</p>
</div>
<div class="card">
<img src = "http://placehold.it/200x200" >
< p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.This content is a little bit longer.</p>
</div>
<div class="card">
<img src = "http://placehold.it/200x200" >
< p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.This content is a little bit longer.</p>
</div>
</div>`
</div>
</div>
<!-- end snippet -->