如何通过CSS创建3x3网格?

18 html css css3 flexbox

div一个接一个给出9 秒,我想通过CSS创建一个3X3网格.

我怎么做?

.cell {
  height: 50px;
  width: 50px;
  background-color: #999;
  display: inline-block;
}

.cell:nth-child(3n) {
  background-color: #F00;
  /* what property should I use to get a line break after this element? */
}

/* this doesn't work; at least not in safari */
.cell:nth-child(3n)::after {
  display: block;
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid">
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
</div>
Run Code Online (Sandbox Code Playgroud)


注意:我不想要float/clear解决方案.重点是CSS,而不是HTML重组.

如果我添加content: '\A'; white-space: pre;::after输出出来是丑陋的.

.cell {
  height: 50px;
  width: 50px;
  background-color: #999;
  display: inline-block;
}

.cell:nth-child(3n) {
  background-color: #F00;
  /* what property should I use to get a line break after this element? */
}

.cell:nth-child(3n)::after {
  display: inline;
  content: '\A';
  white-space: pre;
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid">
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

如何在3X3行列布局中获取所有div?

Mic*_*l_B 15

使用CSS flexbox,这种布局很简单.没有对HTML的更改.

.grid {
  display: flex;                       /* establish flex container */
  flex-wrap: wrap;                     /* enable flex items to wrap */
  justify-content: space-around;
  
}
.cell {
  flex: 0 0 32%;                       /* don't grow, don't shrink, width */
  height: 50px;
  margin-bottom: 5px;
  background-color: #999;
}
.cell:nth-child(3n) {
  background-color: #F00;
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid">
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
</div>
Run Code Online (Sandbox Code Playgroud)


flexbox的好处:

  1. 最小代码; 效率很高
  2. 垂直和水平对中,简单易行
  3. 等高柱简单易行
  4. 用于对齐flex元素的多个选项
  5. 它的反应灵敏
  6. 与浮动和桌子不同,浮动和桌面提供有限的布局容量,因为它们从未用于建筑布局,flexbox是一种现代(CSS3)技术,具有广泛的选项.

要了解有关flexbox的更多信息,请访


浏览器支持:

除了IE <10之外,所有主流浏览器都支持Flexbox .最近的一些浏览器版本,例如Safari 8和IE10,需要供应商前缀.要快速添加前缀,请使用Autoprefixer.这个答案的更多细节.


And*_*man 9

现在,CSS网格非常 非常 良好的支持,我想我会补充回答一个更现代化的解决方案.

.grid {
  display: grid;
  grid-gap: 1px;
  grid-template-columns: repeat(3, 1fr);
}

div > div {
  padding: 10px;
  background-color: #ccc;
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid">
  <div>item</div>
  <div>item</div>
  <div>item</div>
  <div>item</div>
  <div>item</div>
  <div>item</div>
  <div>item</div>
  <div>item</div>
  <div>item</div>  
</div>
Run Code Online (Sandbox Code Playgroud)


dip*_*pas 7

除了@Michael_B使用的优秀答案flexbox,你可以使用

  • CSS表格

    要么

  • 向左飘浮

    两者都支持旧的浏览器,如IE 8/9,flexbox不支持.

注意 IE8不支持nth-child但支持first/last-child

Option1(CSS Tables):对HTML进行更改,将每个3个单元格连续包装.

.grid {
  display: table;
  border-spacing: 5px
}
.row {
  display: table-row
}
.cell {
  width: 50px;
  height: 50px;
  background: grey;
  display: table-cell;
}
.row div:last-child {
  background: red
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid">
  <div class="row">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
  <div class="row">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
  <div class="row">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


更新

Option2(float:left):对HTML没有任何更改,clear:left在每个(第4个)n项使用.

.cell {
  width: 50px;
  height: 50px;
  background: grey;
  float: left;
  margin: 5px
}
.cell:first-child + div+ div,
.cell:first-child + div+ div + div + div + div,
.cell:first-child + div+ div + div + div + div + div + div + div {
  background: red
}
.cell:first-child + div+ div + div,
.cell:first-child + div+ div + div + div + div + div,
.cell:first-child + div+ div + div + div + div + div + div + div + div {
  clear: left
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid">
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
</div>
Run Code Online (Sandbox Code Playgroud)