简体   繁体   English

将高度 100% 设置为 div 会导致垂直滚动条

[英]Setting height 100% to div causes vertical scrollbar

There are only 3 lines of text in a div in body.正文中的 div 中只有 3 行文本。 The background color was filling only up to those 3 lines of text.背景颜色仅填充到那 3 行文本。

As I wanted the color to fill up 100% vertical of the browser, I just set the CSS height properties of html & body to 100% .由于我希望颜色填充浏览器的 100% 垂直,我只是将 html 和 body 的 CSS height属性设置为100% But the vertical scrollbar shows up now.但是垂直滚动条现在出现了。 How can I hide/remove it?如何隐藏/删除它?

I tried overflow:hidden for html as well as div properties but no luck.我尝试了overflow:hidden为 html 以及 div 属性,但没有运气。 Using Firefox.使用火狐浏览器。

 * { margin: 0; padding: 0; } html, body { width: 100%; height: 100%; } .logodiv { width: 100%; background-color: #243640; height: 40px; color: #FF1442; } .content { /* Firefox 3.6+ */ background: -moz-radial-gradient(circle, #1a82f7, #2F2727); width: 100%; height: 100%; overflow: hidden; }
 <div class="logodiv"> ITEMS LIST </div> <div class="content"> line1<br> line2 <br> line3 <br> </div>

Use min-height: 100% instead and add a negative margin to .content to shift it up:改用min-height: 100%并向.content添加负边距以将其向上移动:

.logodiv {
  position: relative;
  z-index: 10;
}

.content {
  background-color: gold;
  min-height:100%;
  margin-top: -40px;
}

.content:before {
  content: ' ';
  display: block;
  height: 40px;
}

JSBin Demo #1 JSBin 演示 #1

Note: In order to push down the content of .content element, I used ::before pseudo-element selector, another option could be:注意:为了下推.content元素的内容,我使用了::before伪元素选择器,另一个选项可能是:

Using box-sizing: border-box and padding-top: 40px CSS declarations:使用box-sizing: border-boxpadding-top: 40px CSS 声明:

.content {
  background-color: gold;
  min-height:100%;
  margin-top: -40px;

  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;

  padding-top: 40px;
}

JSBin Demo #2 JSBin 演示 #2

PS: Nowadays, All major modern browsers support ::before pseudo-element and/or box-sizing property. PS:如今,所有主要的现代浏览器都支持::before伪元素和/或box-sizing属性。 But if you're looking for the traditional way which all browsers support, you can create a .spacer division, as the first child of .content element and set height: 40px;但是如果你正在寻找所有浏览器都支持的传统方式,你可以创建一个.spacer分区,作为.content元素的第一个子元素并设置height: 40px; to .spacer ..spacer

JSBin Demo #3 JSBin 演示 #3

Make logodiv absolutely positioned:使logodiv绝对定位:

http://jsfiddle.net/Gcduf/ http://jsfiddle.net/Gcduf/

.logodiv
{
    width:100%;
    background-color:#243640;
    height:40px;
    color:#FF1442;
    position: absolute;
    top: 0;
}

Use the calc() function.使用calc()函数。

Make this adjustment to your code:对您的代码进行此调整:

.content { height: calc(100% - 40px); }

 .logodiv { height: 40px; background-color: #243640; color: #FF1442; } .content { height: calc(100% - 40px); } body { height: 100vh; } * { margin: 0; padding: 0; }
 <div class="logodiv"> ITEMS LIST </div> <div class="content"> line1<br> line2 <br> line3 <br> </div>


You've got .logodiv with height: 40px .你有.logodivheight: 40px

And its sibling .content with height: 100% .和它的兄弟.contentheight: 100%

Add these two together and they overflow the height of their container ( body ).将这两个加在一起,它们会溢出容器( body )的高度。 That's the reason for the vertical scrollbar.这就是垂直滚动条的原因。

With the calc() function you can set up mathematical expressions using addition (+), subtraction (-), multiplication (*), and division (/) as component values.使用calc()函数,您可以使用加法 (+)、减法 (-)、乘法 (*) 和除法 (/) 作为分量值来设置数学表达式。

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

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