简体   繁体   English

带有 'overflow-y: auto' 的容器添加了额外的水平和垂直滚动条

[英]Container with 'overflow-y: auto' adds extra horizontal and vertical scrolling bar

I have a HTML and CSS files containing the following code:我有一个包含以下代码的 HTML 和 CSS 文件:

<div class="app">
   <div class="header"></div>
   <div class="main">
       <div class="container_1">
           <h1>Item</h1>
           <h1>Item</h1>
           <h1>Item</h1>
                ..
       </div>
       <div class="container_2"></div>
   </div>
</div>
html, body{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body{
    width: 100vw;
    height: 100vh;
}

.app{
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
}

.header{
    width: 100%;
    min-height: 50px;
    background-color: rgb(255, 235, 147);
}

.main{
    display: flex;
    width: 100%;
    height: 100%;
}

.container_1{
    display: flex;
    flex-direction: column;
    width: 200px;
    height: 100%;
    background-color: rgb(255, 147, 147);
    overflow-y: auto;
}

.container_2{
    width: 200px;
    height: 100%;
    background-color: rgb(147, 147, 255);
}

Which looks like this: page with header看起来像这样:带有标题的页面

When removing the div element which contains the header class, the extra scrolling bars are gone (which is what i want): page without header删除包含header类的div元素时,多余的滚动条消失了(这是我想要的):没有标题的页面

How can I get rid of the extra horizontal and vertical scrolling bars (not including the vertical one inside the div with the container_1 class) without removing the div containing the header class?如何在不删除包含header类的div的情况下摆脱额外的水平和垂直滚动条(不包括div内具有container_1类的垂直滚动条)?

I tried to recreate this on a Mac, but for me, it is working fine.我试图在 Mac 上重新创建它,但对我来说,它工作正常。 However, I would say, experiment using overflow: hidden on your elements.但是,我会说,尝试使用overflow: hidden在您的元素上。

You could try adding overflow: hidden to your body element and see how that goes.您可以尝试将overflow: hidden添加到您的body元素中,看看效果如何。
Also, don't forget we also have overflow-y and overflow-x which are both useful.另外,别忘了我们还有overflow-yoverflow-x ,它们都很有用。

mac上的这个html

display:grid is a better layout choice than flexbox for this: display:grid是比 flexbox 更好的布局选择:

 html, body{ margin: 0; padding: 0; } body{ width: 100vw; height: 100vh; } .app{ display: grid; grid-template-rows: auto 1fr; grid-template-columns: 200px 200px auto; height: 100%; } .header{ min-height: 50px; background-color: rgb(255, 235, 147); grid-column: 1 / span 3; } .main { display:contents; } .container_1{ background-color: rgb(255, 147, 147); overflow-y:auto; } .container_2{ background-color: rgb(147, 147, 255); }
 <div class="app"> <div class="header"></div> <div class="main"> <div class="container_1"> <h1>Item</h1> <h1>Item</h1> <h1>Item</h1> <h1>Item</h1> <h1>Item</h1> ... </div> <div class="container_2"></div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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