简体   繁体   中英

How to set 100 % height without having a vertical scrollbar?

Context

I have a navbar with a fixed height. I want the space underneath to fill the rest of the screen vertically. I also need to have a fixed height because I have a container inside the page that has a list that is scrollable but without scrolling the whole page overflow: hidden

The Problem

When I set a height on all parent elements of 100% I get a vertical scrollbar. I found some answers on SO about "margin collapse" but nothing that could solve my problem. 100vh also won't work without having a scrollbar.

Here is the css for setup the height (#__next is just a div where next.js renders the page):

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

The navbar is just a fixed pixel height, and the space below has height: 100%

Here is a screenshot that shows the vertical scrollbar:

在此处输入图像描述

I can't find any problems on the chrome inspector.

This is how it should look (design file):

在此处输入图像描述

Do you know how to solve this? I need to have both containers from screen "SippetPanel" and "SnippetContent" to take the remaining height without adding a scrollbar. It should also work to have a inner scrollbar with overflow hidden (later on when there are many items in the list like from design file)

Be aware that percentual heights refer to the height of the parent. You can use calc() to solve your issue:

#__next{
    height: calc(100% - navbarpx);
    ...
}

calc()

For the padding issue you can look into border-box .

I usually just try different vh values, that means 90vh, 95.5vh etc. so it all sits perfectly. You can try to meddle with body position: absolute etc., but that would push everything into the navbar, so then you would need to fix it with additional margin-top.

So the best solution I see is to try different vh values for the height and find the sweet spot. You will need to do the same for different phone types as well with media queries, but it shoudn't really be hard.

One of the ways is to use flex-box, it allows you to explicitly say(take all available height.

 .body { display: flex; flex-direction: column; }.navbar { flex: 30px 0 0; /* 30px height and do not grow or shint */ background: red; }.content { flex-grow: 1; /* take all available space */ background: blue; }.body, html, body { width: 100%; height: 100%; }
 <div class="body"> <div class="navbar"></div> <div class="content"></div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM