Skip to content Skip to sidebar Skip to footer

Adjust Grid Height To Available Screen Without Scrollbar

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 u

Solution 1:

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;
}
<divclass="wrapper"><divclass="item item1">1</div><divclass="item item2">2</div><divclass="item item3">3</div><divclass="item item4">4</div><divclass="item item5">5</div><divclass="item item6">6</div><divclass="item item7">7</div><divclass="item item8">8</div></div>

Solution 2:

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

html, body {
  height: 96vh;
}

Solution 3:

You have to subtract extra margin from your wrapper

calc(100vh - 16px)

No need to use 100% on body then.

Post a Comment for "Adjust Grid Height To Available Screen Without Scrollbar"