html - 如何让文章占满浏览器高度

标签 html css

我想第一个显示在左上角,第二个显示在右上角,第三个显示在左下角,第四个显示在左下角。

目前显示为:Output

但它并没有占据全高。我想将前两篇文章显示为高度的 50%,其余 50% 显示在底部文章中。

body {
  height: 100%;
  padding: 0;
  margin: 0;
}

article:first-child {
  height: 50%;
  width: 50%;
  float: left;
  position: static;
  background-color: red;
}

article:nth-child(2){
  height: 50%;
  width: 50%;
  float: left;
  position: static;
  background-color: yellow;
}

article:nth-child(3){
  height: 50%;
  width: 50%;
  float: left;
  position: static;
  background-color: green;
}

article:nth-child(4){
  height: 50%;
  width: 50%;
  float: left;
  position: static;
  background-color: blue;
}
<article>First</article>
<article>Second</article>
<article>third</article>
<article>fourth</article>

最佳答案

您可以使用Flexboxviewport 单位,其中50vw50vh 表示 viewport widthheight50%:

body {
  display: flex; /* displays childen inline by default */
  flex-wrap: wrap; /* enables wrapping */
  width: 100vw; /* added */
  height: 100vh; /* modified */
  padding: 0;
  margin: 0;
}

article:first-child {
  height: 50vh;
  width: 50vw;
  /*float: left;*/
  /*position: static;*/
  background-color: red;
}

article:nth-child(2) {
  height: 50vh;
  width: 50vw;
  /*float: left;*/
  /*position: static;*/
  background-color: yellow;
}

article:nth-child(3) {
  height: 50vh;
  width: 50vw;
  /*float: left;*/
  /*position: static;*/
  background-color: green;
}

article:nth-child(4) {
  height: 50vh;
  width: 50vw;
  /*float: left;*/
  /*position: static;*/
  background-color: blue;
}
<article>First</article>
<article>Second</article>
<article>third</article>
<article>fourth</article>

注意:不需要 float 和默认情况下的 position: static

关于html - 如何让文章占满浏览器高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48007596/

相关文章:

javascript - 如何在页面加载时选中单选按钮时隐藏 div?

html - 我的 iFrame 如何在 2 或 3 列中显示?

html - IE中png文件的透明度

css - rails - 如何在样式标签中将图像显示为背景?

CSS 中类似于 Android ICS 的输入字段

javascript - 如何将 "connect"PHP "network"转换为 .svg 图像

html - 如何摆脱图像下方和下一个文本之前的空白

javascript - Easeljs 中动画的奇怪行为

html - 我需要 3 个 div 来实现吗?

html - 为什么这个 css 菜单与 IE7 中的横幅重叠