CSS 如何在 flexbox 中每行显示 3 个项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48464444/
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
How to display 3 items per row in flexbox?
提问by Youssef Boudaya
I have a list and I want to display my li
elements horizontally and 3 per row. I've been trying to get what I want, but no luck. Is there a solution?
我有一个列表,我想li
水平显示我的元素,每行 3 个。我一直试图得到我想要的,但没有运气。有解决办法吗?
<div class="serv">
<ul>
@foreach(App\Http\Controllers\HomeController::getservice($service->id) as
$key => $value)
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
@endforeach
</ul>
</div>
.serv ul {
display: inline-flex;
margin: 0;
padding: 0;
width: 33%;
text-align: left;
float: left;
}
.serv ul li {
list-style: none;
display: inline-block;
padding: 0;
}
.serv ul li span {
padding: 0;
}
回答by Stickers
Flex container:
弹性容器:
- You probably want to use
display: flex
notinline-flex
. - Add
flex-wrap: wrap
to allow wrapping onto multiple lines. - Remove
width: 33%
if you wish it to take entire space avaiable.
- 您可能想使用
display: flex
notinline-flex
。 - 添加
flex-wrap: wrap
以允许换行到多行。 width: 33%
如果您希望它占用整个可用空间,请将其删除。
For 3 items per row, add on the flex items:
对于每行 3 个项目,添加弹性项目:
flex-basis: 33.333333%
- You can also use the
flex
's shorthand like the following:flex: 0 0 33.333333%
=> which also meansflex-basis: 33.333333%
.
flex-basis: 33.333333%
- 您也可以
flex
像下面这样使用的简写:flex: 0 0 33.333333%
=> 这也意味着flex-basis: 33.333333%
.
.serv ul {
display: flex;
flex-wrap: wrap;
padding-left: 0;
}
.serv ul li {
list-style: none;
flex: 0 0 33.333333%;
}
<div class="serv">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
</div>
回答by Johannes
Erase the width for the ul
element and add a width (between 26 and 33%) to li
. In addition, use flex-wrap: wrap;
to allow them to wrap to new lines:
擦除ul
元素的宽度并将宽度(介于 26 和 33% 之间)添加到li
。此外,使用flex-wrap: wrap;
允许它们换行:
.serv ul {
display: inline-flex;
flex-wrap: wrap;
margin: 0;
padding: 0;
text-align: left;
}
.serv ul li {
list-style: none;
display: inline-block;
padding: 0;
border: 1px solid #ccc;
width: 30%;}
.serv ul li span {
padding: 0px;
}
<div class="content-wrap">
<section id="section-linemove-{{$service->id}}">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="big-title3 small-area">
<h4>{{$service->title2}}</h4>
</div>
<div>
{!!$service->description!!}
</div>
</div>
<div class="serv">
<ul>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
<li>
<span class="h3-service">{{$value->title}}</span>
<p>{!!$value->description!!}</p>
</li>
</ul>
</div>
</section>
</div>
回答by Tazwar Utshas
use that witdh:33% at li section, not in ul section.
在 li 部分使用该 witdh:33%,而不是在 ul 部分。
回答by T04435
I had this same issue, but the selected correct answer did not work cause the my child itemsneeded to have a fix widthso here is my solution to show X items of fix width on DISPLAY: FLEX.
我遇到了同样的问题,但选择的正确答案不起作用,导致我的子项需要有固定宽度,所以这里是我的解决方案,用于在DISPLAY: FLEX上显示X 项固定宽度。
// Flex item width required: 215px
// Flex item margin 20px on each side: 215px + 20 + 20 = 255px
// I needed 3(your item per row) so: 255px * 3
// Then to (100% of parent-flex minus 255 * 3) / 2 padding on each side
// So it stays in the center.
padding: 40px calc((100% - (255px * 3)) / 2);
*, *::before, *::after {
box-sizing: border-box;
}
.parent-flex {
display: flex;
flex-wrap: wrap;
justify-content: center;
background-color: tomato;
padding: 40px calc((100% - (255px * 3)) / 2);
}
.flex-item {
height: 100px;
flex: 0 0 215px;
margin: 1em 20px;
border: 2px blue solid;
}
<div class="parent-flex">
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>