如何使用flexbox将网页分成四个部分?

uit*_*waa 5 html css css3 flexbox responsive-design

如何使用弹性框将网页分成四个相等的部分?像这个网站的东西 - www.rileyjshaw.com

列应该能够在较小的视图端口上相互堆叠.

编辑/更新:

到目前为止,我已经尝试过这个 - 我应该改变线高吗?如何实现不同的块?

/* Grid */

.column {
  flex-basis: 100%;
  width: 50%;
  height: 50%;
  line-height: 200px;
}

@media screen and (min-width: 800px) {
  .row {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
  }
  .column {
    flex: 1;
  }
  ._25 {
    flex: 2.5;
  }
  ._5 {
    flex: 5;
  }
}


/* Style */

body {
  font-family: 'Lato', sans-serif;
  font-size: 1.3em;
  color: #ccc;
  background: #000;
  /*margin-bottom: 70px;*/
}

.column {
  /* padding: 15px;
  border: 1px solid #666;
  margin: 5px 0;*/
  background: #343436;
}

main {
  max-width: 1200px;
  margin: 0 auto;
}

h1,
h2 {
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="row">
  <div class="column">
    50%
  </div>
  <div class="column">
    50%
  </div>
</div>
<div class="row">
  <div class="column">
    50%
  </div>
  <div class="column">
    50%
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

LGS*_*Son 4

您可以简化代码并获得所需的输出。在这里,我删除了并使用了一个容器。这种结构的主要好处是,如果您认为有必要,可以更改列的顺序。

我还选择使用flex-basis而不是flex-grow让它们保持 50% 的宽度,无论内容大小如何。

在更宽的屏幕上,使用媒体查询,将列的宽度设置为 50%,container并将display: flex; flex-wrap: wrap;

在较窄的屏幕上,并且作为块元素,它们默认堆叠在彼此之上

html, body {
  height: 100%;
  margin: 0;
}
.container {
  height: 100%;
}
.column {
  height: 25%;
}

@media screen and (min-width: 600px) {
  .container {
    display: flex;
    flex-wrap: wrap;
  }
  .column {
    flex-basis: 50%;
    height: 50%;
  }
}


/* general styles */
body {
  font-family: 'Lato', sans-serif;
  font-size: 1.3em;
  color: #ccc;
  background: #000;
  /*margin-bottom: 70px;*/
}

.column {
  padding: 15px;
  /*border: 1px solid #666;*/
  box-sizing: border-box;
}

.column:nth-child(1) {
  background: #5c9;
}
.column:nth-child(2) {
  background: #fb0;
}
.column:nth-child(3) {
  background: #39f;
}
.column:nth-child(4) {
  background: #f33;
}

main {
  max-width: 1200px;
  margin: 0 auto;
}

h1,
h2 {
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="column">
    50%
  </div>
  <div class="column">
    50%
  </div>
  <div class="column">
    50%
  </div>
  <div class="column">
    50%
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


如果您仍然需要原始标记结构,这里也有一个示例

html, body {
  height: 100%;
  margin: 0;
}
.row {
  height: 50%;
}
.column {
  height: 50%;
}

@media screen and (min-width: 600px) {
  .row {
    display: flex;
  }
  .column {
    flex-basis: 50%;
    height: 100%;
  }
}


/* general styles */
body {
  font-family: 'Lato', sans-serif;
  font-size: 1.3em;
  color: #ccc;
  background: #000;
  /*margin-bottom: 70px;*/
}

.column {
  padding: 15px;
  /*border: 1px solid #666;*/
  box-sizing: border-box;
}

.row:nth-child(1) .column:nth-child(1) {
  background: #5c9;
}
.row:nth-child(1) .column:nth-child(2) {
  background: #fb0;
}
.row:nth-child(2) .column:nth-child(1) {
  background: #39f;
}
.row:nth-child(2) .column:nth-child(2) {
  background: #f33;
}

main {
  max-width: 1200px;
  margin: 0 auto;
}

h1,
h2 {
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="row">
  <div class="column">
    50%
  </div>
  <div class="column">
    50%
  </div>
</div>
<div class="row">
  <div class="column">
    50%
  </div>
  <div class="column">
    50%
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


根据评论更新

可以使用 ie 来将列内容居中