如何在flexbox中每行显示3个项目?

时间:2018-01-26 15:29:40

标签: css css3

我有一个列表,我希望水平显示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;
}

5 个答案:

答案 0 :(得分:15)

Flex容器:

  • 您可能希望使用display: flex而不是inline-flex
  • 添加flex-wrap: wrap以允许换行到多行。
  • 如果您希望整个空间可用,请移除width: 33%

Flex项目:

  • 每行3个项目添加flex: 0 0 33.333333%

&#13;
&#13;
.serv ul {
  display: flex;
  flex-wrap: wrap;
  padding-left: 0;
}

.serv ul li {
  list-style: none;
  flex: 0 0 33.333333%;
}
&#13;
<div class="serv">
  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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

答案 2 :(得分:0)

使用那个:在li部分为33%,而不是在ul部分。

答案 3 :(得分:0)

body {
  margin: 0px;
}
.parent {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
.flex-item{
   /* 
   All you need to do this play with this number (343) according to aviable width and item width 
   Formula: (ParentWidth / 3) - margin (ParentWidth divide by 3 subtracte by margin[if you want e.g in my case i set 10] )
   
   Stackoverflow Run Code Snippet width = 633.82px (not sure)
   Margin = 10px
   
   */
   flex: 0 0 192px; /* play with this number */
   background: red;
   margin: 10px;
}
<div class="parent">
  <div class="flex-item">1</div>
  <div class="flex-item">2</div>
  <div class="flex-item">3</div>
  <div class="flex-item">4</div>
  <div class="flex-item">5</div>
  <div class="flex-item">6</div>
  <div class="flex-item">7</div>
  <div class="flex-item">8</div>
  <div class="flex-item">9</div>
  <div class="flex-item">10</div>
</div>

答案 4 :(得分:-1)

我遇到了同样的问题,但是选择的正确答案不起作用,原因是我的子项需要具有 fix width ,所以这是我显示X的解决方案 DISPLAY:FLEX 上固定宽度的项目。

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