提问者:小点点

我应该如何在Flex上做这个网格?


我应该如何在Flex上做这个网格?如果我将justify-content:space-between给父级,那么底线将有很大的空格(中间有很大的空格,中间有空格),如果我删除justify-content:space-between并使用margy-right,我将在所有的右块,所有的行都有margy-right。

null

.list {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  border: 2px solid red;
}

.item {
  width: 23%;
  height: 150px;
  margin-bottom: 10px;
  background: blue;
}
<div class="list">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

null


共1个答案

匿名用户

我试过很多flex box的情况,似乎不能得到你想要的。我使用了一个边距右,但从最后一个块中删除了它,如下所示:

.list {
  display: flex;
  flex-wrap: wrap;
  border: 2px solid red;
}
.item {
  width: 23%;
  height: 150px;
  margin-bottom: 10px;
  background: blue;
  margin-right: 10px;
}
.item:last-child {
  margin-right: 0px;
} 

但我也测试了以下内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .flex-container {
  /* We first create a flex layout context */
  display: flex;
  
  /* Then we define the flow direction 
     and if we allow the items to wrap 
   * Remember this is the same as:
   * flex-direction: row;
   * flex-wrap: wrap;
   */
  flex-flow: row wrap;
  
  /* Then we define how is distributed the remaining space */
  justify-content: space-around;
  
  padding: 0;
  margin: 0;
  list-style: none;
}

.flex-item {
  background: tomato;
  padding: 5px;
  width: 200px;
  height: 150px;
  margin-top: 10px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
}
    </style>
</head>
<body>
    <ul class="flex-container">
  <li class="flex-item">1</li>
  <li class="flex-item">2</li>
  <li class="flex-item">3</li>
  <li class="flex-item">4</li>
  <li class="flex-item">5</li>
  <li class="flex-item">6</li>
</ul>
</body>
</html>

这给了你想要的...但是当你在浏览器Winbow中达到某个断点时...甚至上面的都给了你上两个之间的巨大差距。

看来任何低于1065px的东西都会给你问题。移动超过855px将使它们再次正确对齐。

所以我相信您有正确的CSS,但是您可能需要使用媒体查询来处理一些断点?

我可能是错误的,但这些是我的发现。