适用于所有屏幕尺寸的Flexbox自适应产品网格

时间:2019-09-03 04:40:49

标签: css flexbox cross-browser responsive

我有一个产品网格,其中每个网格的宽度为200像素。我希望它能在所有屏幕尺寸上工作。

我当前的屏幕尺寸是1366像素。移至以上屏幕尺寸将在右侧保留空白。

flexbox justify-content: flex-start

#content {
  display: flex;
  flex-wrap: wrap;
}

.tile {
  height: 100px;
  background: pink;
  width: 200px;
}
<div id="content">
  <div class="tile">1</div>
  <div class="tile">2</div>
  <div class="tile">3</div>
  <div class="tile">4</div>
  <div class="tile">5</div>
  <div class="tile">6</div>
  <div class="tile">7</div>
  <div class="tile">8</div>
  <div class="tile">9</div>
  <div class="tile">10</div>
  <div class="tile">11</div>
  <div class="tile">12</div>
  <div class="tile">13</div>
  <div class="tile">14</div>
  <div class="tile">15</div>
  <div class="tile">16</div>
</div>

Flex

1 个答案:

答案 0 :(得分:1)

如果要使产品网格填充屏幕的水平宽度,而不考虑屏幕分辨率,则可以选择“流体”网格。

“流体”网格将保留其结构,并“拉伸以适应”父容器的宽度(或屏幕分辨率),无论如何:

#content {
  display: flex;
  flex-wrap: wrap;
}

.tile {
  height: 100px;
  background: pink;

  /* 
  Specify percentage based with causes tile width to update dynamically
  based on current width of parent. A width of 25% causes four tiles per 
  row.
  width: 25%;
  */

  /* For 6 tiles per row */
  width: 16.6%;
 
}
<div id="content">
  <div class="tile">1</div>
  <div class="tile">2</div>
  <div class="tile">3</div>
  <div class="tile">4</div>
  <div class="tile">5</div>
  <div class="tile">6</div>
  <div class="tile">7</div>
  <div class="tile">8</div>
  <div class="tile">9</div>
  <div class="tile">10</div>
  <div class="tile">11</div>
  <div class="tile">12</div>
  <div class="tile">13</div>
  <div class="tile">14</div>
  <div class="tile">15</div>
  <div class="tile">16</div>
  <div class="tile">17</div>
  <div class="tile">18</div>
</div>