html - 使用 CSS 有两个(或更多)显示为 : table-cell fill the entire screen, 的 div,无论其中的内容如何

标签 html css

我想要 2 个 div,彼此堆叠,使用 display: table-cell;

我还希望每个 div 的高度和宽度都占屏幕的 100%。

我已经尝试过这样做,但我无法完全做到,这是我的代码。

body,
html {
  height: 100%;
}
body {
  margin: 0;
}
.height {
  min-height: 100%;
}
.width {
  width: 100%;
}
.wrapper {
  position: relative;
  height: 100%;
  display: table;
  margin: 0 auto;
}
.in-wrap {
  display: table-row;
}
.align {
  position: relative;
  text-align: center;
  display: table-cell;
  vertical-align: middle;
}
.black {
  background-color: black;
  color: white;
}
<div class="width wrapper">
  <div class="in-wrap">
    <div class="align black">
      <h3>Lorem ipsum dolor sit amet,</h3>
      <p>
        <button>consectetur adipiscing elit</button>
      </p>
    </div>
  </div>
  <div class="in-wrap">
    <div class="align">
      <h3>Donec auctor ornare augue vel lobortis.</h3>
      <p>Nulla pellentesque urna vitae felis congue aliquet.</p>
      <button id="projects">Nullam vitae pharetra nisl.</button>
    </div>
  </div>
</div>

最佳答案

.in-wrap 100vh 的高度并去掉所有的 height: 100%

.in-wrap {
  height: 100vh;
  display: table-row;
}

例子....

body {
  margin: 0;
}

.width {
  width: 100%;
}
.wrapper {
  position: relative;
  display: table;
  margin: 0 auto;
}
.in-wrap {
  height: 100vh;
  display: table-row;
}
.align {
  position: relative;
  text-align: center;
  display: table-cell;
  vertical-align: middle;
}
.black {
  background-color: black;
  color: white;
}
<div class="width wrapper">
  <div class="in-wrap">
    <div class="align black">
      <h3>Lorem ipsum dolor sit amet,</h3>
      <p>
        <button>consectetur adipiscing elit</button>
      </p>
    </div>
  </div>
  <div class="in-wrap">
    <div class="align crisscross">
      <h3>Donec auctor ornare augue vel lobortis.</h3>
      <p>Nulla pellentesque urna vitae felis congue aliquet.</p>
      <button id="projects">Nullam vitae pharetra nisl.</button>
    </div>
  </div>
</div>

关于html - 使用 CSS 有两个(或更多)显示为 : table-cell fill the entire screen, 的 div,无论其中的内容如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39350845/

相关文章:

html - 谷歌如何将边框厚度减半但仍设置为 1 像素

javascript - AngularJS {{ 值 |编号 :1 }} not rounding to 1 decimal place

css - 如何使 JPG 仅在悬停时加载?

jquery - 如何向下滑动容器中的文本

html - Ionic 2 中的类样式不起作用

html - 设置下拉菜单位置(CSS)

html - 如何使用 CSS 制作带有固定标题的可滚动表

css - 尽管溢出设置为自动,但水平滚动条不会出现在 DIV 中

javascript - Jquery 在悬停时将对象放置在其他地方

jquery - Bootstrap Center col-sm div inside row 类