如何并排对齐div

时间:2018-11-04 23:03:40

标签: css

我已经尝试了所有使这些div对齐的方法,在桌面上它们是完美的,每行对计算机都有4 div可视化,现在在android中按行显示3 div,但是没有效果很好,并且代码和桌面使用的相同,我只是将显示号更改为3

enter image description here

<div class="cartaz">
    <ul>
        <li><a href=""><div class="mini-box"></div></a></li>
        <li><a href=""><div class="mini-box"></div></a></li>
        <li><a href=""><div class="mini-box"></div></a></li>
        <li><a href=""><div class="mini-box"></div></a></li>
        <li><a href=""><div class="mini-box"></div></a></li>
        <li><a href=""><div class="mini-box"></div></a></li>
        <li><a href=""><div class="mini-box"></div></a></li>
        <li><a href=""><div class="mini-box"></div></a></li>
        <li><a href=""><div class="mini-box"></div></a></li>
    </ul>
</div>

css桌面

.cartaz{
   float: left;
   margin: 0% 2% 0% 0%;
   padding: 0% 0% 2% 0%;
   width: 78%;
  border-bottom: 1px solid #ebebeb;
}
li .mini-box{
   float: left;
   margin: 0% 2% 0% 0%;
   width: 23.5%;
   height: 230px;
   background-color: #4DAE52;
   border-radius: 3px;
}
li:nth-child(-n+4) .mini-box{
   margin: 0 2% 0 0;
}
li:nth-child(4n) .mini-box{
   margin: 0 0 2% 0;
}
li:last-child .mini-box{margin: 0%;}

css android

@media only screen and (max-width: 360px){
   li .mini-box{
      margin: 0 2% 0 0;
      width: 31.9%;
      background-color: purple
   }
   li:nth-child(-n+3) .mini-box{
      margin: 0 2% 0 0;
      background-color: orange
   }
   li:nth-child(3n) .mini-box{
      margin: 0 0 2% 0;
      background-color: gray
   }
   li:last-child .mini-box{margin: 0%;background-color: red}
}

1 个答案:

答案 0 :(得分:0)

您可以像下面那样减少代码:

ul {
 list-style:none;
}

.cartaz {
  float: left;
  margin: 0% 2% 0% 0%;
  padding: 0% 0% 2% 0%;
  width: 78%;
  border-bottom: 1px solid #ebebeb;
}

li .mini-box {
  float: left;
  margin: 0% 2% 0% 0%;
  width: 23.5%;
  height: 230px;
  background-color: #4DAE52;
  border-radius: 3px;
}

li:nth-child(4n) .mini-box {
  margin: 0 0 2% 0;
}

li:last-child .mini-box {
  margin: 0%;
}

@media only screen and (max-width: 800px) {
   /*reset the style to avoid issue*/
   li:nth-child(4n) .mini-box {   
     margin: 0 2% 0 0;
   }
   /**/
  li .mini-box {
    width: 31.9%;
    background-color: purple
  }
  li:nth-child(3n) .mini-box {
    margin: 0 0 2% 0;
    background-color: orange
  }
  li:last-child .mini-box {
    background-color: red
  }
}
<div class="cartaz">
  <ul>
    <li>
      <a href="">
        <div class="mini-box"></div>
      </a>
    </li>
    <li>
      <a href="">
        <div class="mini-box"></div>
      </a>
    </li>
    <li>
      <a href="">
        <div class="mini-box"></div>
      </a>
    </li>
    <li>
      <a href="">
        <div class="mini-box"></div>
      </a>
    </li>
    <li>
      <a href="">
        <div class="mini-box"></div>
      </a>
    </li>
    <li>
      <a href="">
        <div class="mini-box"></div>
      </a>
    </li>
    <li>
      <a href="">
        <div class="mini-box"></div>
      </a>
    </li>
    <li>
      <a href="">
        <div class="mini-box"></div>
      </a>
    </li>
    <li>
      <a href="">
        <div class="mini-box"></div>
      </a>
    </li>
  </ul>
</div>