Adjust grid height to available screen without scr

2019-08-05 10:09发布

What grid property should I use to keep the grid perfectly inside the screen?

Using height: 100vh; on .wrapper adjusts the height, but it introduces the scrollbars. To remove the unwanted scrollbars, I tried setting body{margin:0;} but I want to have a margin surrounding the entire grid without producing the scrollbars. I'm sure this is an easy fix, but please help me!

Codepen: 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>

3条回答
女痞
2楼-- · 2019-08-05 10:39

You have to subtract extra margin from your wrapper

calc(100vh - 16px)

No need to use 100% on body then.

查看更多
混吃等死
3楼-- · 2019-08-05 10:40

Other than setting the margin on html, body, to 0, the only thing I found that would work is this:

html, body {
  height: 96vh;
}
查看更多
女痞
4楼-- · 2019-08-05 10:44

Major browsers normally set a default margin on the body element. It's usually 8px, as recommended by the W3C.

Therefore, when setting the body element or another container to height: 100%, a vertical scrollbar will render because there is an overflow condition:

100% + 8px > the viewport height

The simple workaround is to override the browser's default rule with your own:

body { margin: 0; }

However, in this case, you want the gap around the main container. You don't want the scrollbar.

Then simply replace margin with padding, and use box-sizing: border-box.

With box-sizing: border-box, padding and borders (but not margins) are factored into the content length.

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>

查看更多
登录 后发表回答