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>
You have to subtract extra margin from your wrapper
calc(100vh - 16px)
No need to use 100% on body then.
Other than setting the margin on html, body, to 0, the only thing I found that would work is this:
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 toheight: 100%
, a vertical scrollbar will render because there is an overflow condition:The simple workaround is to override the browser's default rule with your own:
However, in this case, you want the gap around the main container. You don't want the scrollbar.
Then simply replace
margin
withpadding
, and usebox-sizing: border-box
.With
box-sizing: border-box
, padding and borders (but not margins) are factored into the content length.