在行中显示div

时间:2016-11-08 22:26:47

标签: html css

有没有办法将容器3内的div对齐?

<div class="container">
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>   
</div>

4 个答案:

答案 0 :(得分:3)

是的,flexbox是最好的方法

&#13;
&#13;
.container {
  display: flex;
  flex-wrap: wrap;
}
.container .product {
  flex: 1 0 33%;
  height: 50px;
  border: 1px solid black;
}
&#13;
<div class="container">
  <div class="product"></div>
  <div class="product"></div>
  <div class="product"></div>
  <div class="product"></div>
  <div class="product"></div>
  <div class="product"></div>
  <div class="product"></div>
  <div class="product"></div>
  <div class="product"></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

.container {
  width: 100%;
}
.product {
  width:33.33%;
  background: blue;
  min-height: 100px;
  float:left;
}
<div class="container">
      <div class="product">1</div>
      <div class="product">2</div>
      <div class="product">3</div>
      <div class="product">4</div>
      <div class="product">5</div>
      <div class="product">6</div>
      <div class="product">7</div>
      <div class="product">8</div>
      <div class="product">9</div>   
</div>

答案 2 :(得分:1)

Bootstrap解决方案:

https://jsfiddle.net/e76tknpq/

<div class="row">

        <div class="product col-md-4 col-lg-4 col-sm-4 col-xs-4"></div>
        <div class="product col-md-4 col-lg-4 col-sm-4 col-xs-4"></div>
        <div class="product col-md-4 col-lg-4 col-sm-4 col-xs-4"></div>
        </div>
<div class="row">


            <div class="product col-md-4 col-lg-4 col-sm-4 col-xs-4"></div>
            <div class="product col-md-4 col-lg-4 col-sm-4 col-xs-4"></div>
            <div class="product col-md-4 col-lg-4 col-sm-4 col-xs-4"></div>
</div>
<div class="row">


        <div class="product col-md-4 col-lg-4 col-sm-4 col-xs-4"></div>
        <div class="product col-md-4 col-lg-4 col-sm-4 col-xs-4"></div>
        <div class="product col-md-4 col-lg-4 col-sm-4 col-xs-4"></div> 

</div>

CSS

.product:nth-child(even){
  background: red;
  height:180px;
}

.product:nth-child(odd){
  background: green;
  height:180px;
}

答案 3 :(得分:1)

Flexbox来解决这个问题。另一个(旧的)方法是使用 display:inline-block (而不是 float。

.product {
  display: inline-block;
  width: 30%;
  height: 50px;
  border: 1px solid green;
}
<div class="container">
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>
      <div class="product"></div>   
</div>