html - 将网格高度调整到没有滚动条的可用屏幕

标签 html css css-grid

我应该使用什么网格属性来将网格完美地保留在屏幕内?

.wrapper 上使用 height: 100vh; 调整高度,但它引入了滚动条。为了删除不需要的滚动条,我尝试设置 body{margin:0;} 但我希望在不产生滚动条的情况下在整个网格周围留出边距。我确信这很容易修复,但请帮助我!

代码笔:https://codepen.io/reiallenramos/pen/yzroxe

html,
body {
  height: 100%;
  width: 100%;
}

body {
  background-color: lightcyan;
}

.wrapper {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(6, 1fr);
  grid-gap: 8px;
  height: 100vh;
}

.wrapper>div {
  background-color: #eee;
}

.wrapper>div:nth-child(odd) {
  background-color: #ddd;
}

.item1 {
  grid-column: 1/2;
  grid-row: 1/5;
}

.item2 {
  grid-column: 2/3;
  grid-row: 1/3;
}

.item3 {
  grid-column: 3/5;
  grid-row: 1/3;
}

.item4 {
  grid-column: 2/4;
  grid-row: 3/5;
}

.item5 {
  grid-column: 4/5;
  grid-row: 3/6;
}

.item6 {
  grid-column: 1/3;
  grid-row: 5/7;
}

.item7 {
  grid-column: 3/4;
  grid-row: 5/7;
}

.item8 {
  grid-column: 4/5;
  grid-row: 6/7;
}
<div class="wrapper">
  <div class="item item1">1</div>
  <div class="item item2">2</div>
  <div class="item item3">3</div>
  <div class="item item4">4</div>
  <div class="item item5">5</div>
  <div class="item item6">6</div>
  <div class="item item7">7</div>
  <div class="item item8">8</div>
</div>

最佳答案

主要浏览器通常在 body 元素上设置默认边距。通常为 8px,如 recommended by the W3C .

因此,当将 body 元素或其他容器设置为 height: 100% 时,将呈现垂直滚动条,因为存在溢出条件:

100% + 8px > the viewport height

简单的解决方法是用您自己的规则覆盖浏览器的默认规则:

body { margin: 0; }

但是,在这种情况下,您需要在主容器周围留出间隙。你不需要滚动条。

然后只需将 margin 替换为 padding,并使用 box-sizing: border-box

使用 box-sizing: border-box,填充和边框(但不包括边距)被计入内容长度。

body {
  margin: 0;
  background-color: lightcyan;
}

/* NEW */
* {
  box-sizing: border-box;
}

.wrapper {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(6, 1fr);
  grid-gap: 8px;
  padding: 8px; /* NEW */
  height: 100vh;
}

.wrapper>div {
  background-color: #eee;
}

.wrapper>div:nth-child(odd) {
  background-color: #ddd;
}

.item1 {
  grid-column: 1/2;
  grid-row: 1/5;
}

.item2 {
  grid-column: 2/3;
  grid-row: 1/3;
}

.item3 {
  grid-column: 3/5;
  grid-row: 1/3;
}

.item4 {
  grid-column: 2/4;
  grid-row: 3/5;
}

.item5 {
  grid-column: 4/5;
  grid-row: 3/6;
}

.item6 {
  grid-column: 1/3;
  grid-row: 5/7;
}

.item7 {
  grid-column: 3/4;
  grid-row: 5/7;
}

.item8 {
  grid-column: 4/5;
  grid-row: 6/7;
}
<div class="wrapper">
  <div class="item item1">1</div>
  <div class="item item2">2</div>
  <div class="item item3">3</div>
  <div class="item item4">4</div>
  <div class="item item5">5</div>
  <div class="item item6">6</div>
  <div class="item item7">7</div>
  <div class="item item8">8</div>
</div>

关于html - 将网格高度调整到没有滚动条的可用屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46871193/

相关文章:

html - 如何使用 CSS 将文本定位在图像上

html - 将页脚保留在页面底部,仅在滚动后出现

css - Bootstrap 3 CSS 图像标题覆盖

html - 在 div 容器中带有滚动条的 CSS 网格

javascript - 使用 jquery 过滤选定的类别

html css 粘性等

jquery - 为什么这些转换不起作用? (CSS3/jQuery)

html - 在动画状态期间同步 div 高度

html - 用网格内容填充文本

CSS 网格 - 将所有子项保持在一行且间距相等