指定div高度(Bootstrap)

3

我正在尝试使用Bootstrap制作一个酷炫的投资组合着陆页。

该模板包括一个固定的导航栏和一个粘性页脚。页面的其余部分是空白的,我想用基本上是巨大的背景来填充它,所以我添加了一个测试段落元素来尝试如何设置大小。我认为我可以添加CSS规则,使body的高度达到100%,但当我这样做时,就会出现溢出,我的页脚也不再在页面底部。相反,在底部出现了一个小溢出。

我该怎么办?我使背景色变成亮黄色以尝试说明我的意思。非常感谢任何帮助!

以下是HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Work in Progress</title>
</head>

<body>
<!--navbar-->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" 
                data-toggle="collapse" data-target="#navbar" 
                aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Portfolio</a>
    </div>
    <div id="navbar" class="navbar-collapse collapse" 
                aria-expanded="false" style="height: 1px;">
      <ul class="nav navbar-nav">
        <li><a href="#about">About</a></li>
        <li><a href="#contact">Contact</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" 
          role="button" aria-haspopup="true" 
          aria-expanded="false">Projects 
          <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Rails Projects</a></li>
            <li><a href="#">LAMP Projects</a></li>
            </ul>
        </ul>
    </div>
</div>    
</nav>


<div class="main">
<div class="container">
<p>Test text</p>
</div>
</div>



<!--footer-->
<footer class="footer">
<div class="container">
   <div class="row">
     <div class="col-md-4">
   <p>Contact info or whatever goes here.</p>
   </div>
   <div class="col-md-4 col-md-offset-4">
   <p>More info or whatever goes here.</p>
    </div>
    </div>
 </div>
</footer>


</body>
</html>

以下是CSS代码:

html, body{
height: 100%
}

.main{
height: 100%;
background-color: #FFFF00;
margin-top: 50px;
}

.footer{
position: absolute;
bottom: 0;
width: 100%;
height: 60px;
background-color: #000;
color: #9d9d9d;
}
2个回答

1
你可以使用以下CSS(在body上使用position: relative,并将.main的高度设为calc函数(100%减去页脚的高度):
html,
body {
  height: 100%;
  position: relative;
}
.main {
  height: calc(100% - 60px);
  background-color: #FFFF00;
  padding-top: 50px;
}
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #000;
  color: #9d9d9d;
}

所以我尝试运行了这段代码。它的确有效,因为现在底部没有空白了,但是现在它会强制用户必须使用滚动条才能看到底部的页脚。 - ricksanchez
我更新了代码,请使用 padding-top 替代 margin-top - max
1
结合您的想法和上述的想法使其正常工作。您正确指出应该是高度减去页脚。非常感谢! - ricksanchez

0
演示:https://fiddle.jshell.net/uvaef49h/1/ 您想使用以下CSS:
    html, body{
height: 100%;
padding: 0;
margin: 0;
}

.main{
height: calc(100% - 110px);
background-color: #FFFF00;
margin-top: 50px;
}

.footer{
position: absolute;
bottom: 0;
width: 100%;
height: 60px;
background-color: #000;
color: #9d9d9d;
overflow: hidden;
}

之前为什么不起作用?

主体高度为X,主要占据了100%。然而,主要被50px的边距向下推。在.main部分之后,您有页脚,它完全从视口中推出,因此我必须从.main的高度中再减去60px。页脚仍然存在溢出问题,因此我将其隐藏。

最后,浏览器想要添加一些随机的边距/填充,因此我也将其删除。


我尝试了这段代码。对我而言,唯一发生的事情是底部多余的空格变成了白色,而不是黄色。 - ricksanchez
好的,现在将calc更改为-60px而不是-110px,它可以正常工作了。非常感谢! - ricksanchez
@ricksanchez 如果有帮助,请接受这个答案。 - Slava Knyazev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接