【发布时间】:2014-01-28 09:18:25
【问题描述】:
我为个人网站创建新布局。
我正在使用 Twitter Bootstrap 3,我的初始布局是使用示例 “带有粘性页脚的引导程序”示例 (http://getbootstrap.com/examples/sticky-footer-navbar/)
这是我的html:
<body>
<!-- Wrap all page content here -->
<div id="wrap">
<!-- Begin page navigation -->
<nav id="nav-container" class="navbar navbar-default container" role="navigation">
<div class="container">
<!-- Here I put a very normal Bootstrap 3 navbar -->
</div>
</nav>
<!-- Begin page content -->
<div id="main-container" class="container">
<!-- All my content goes here! -->
</div>
</div>
<!-- Begin page footer -->
<footer id="footer" class="container">
<div class="container">
</div>
</footer>
</body>
粘性页脚 CSS:
html, body {
height: 100%;
/* The html and body elements cannot have any padding or margin. */
}
/* Wrapper for page content to push down footer */
#wrap {
min-height: 100%;
height: auto;
/* Negative indent footer by its height */
margin: 0 auto -100px;
/* Pad bottom by footer height */
padding: 0 0 100px;
}
/* Set the fixed height of the footer here */
#footer {
height: 100px;
}
以及我的布局的自定义样式:
body {
/* Body's background will be grey */
background-color: #C0C0C0;
}
#main-container {
/* A box where I'll put the content will be white */
background-color: #FFFFFF;
}
#wrap {
height: 100%;
min-height: 100%;
}
#main-container {
min-height: 100%;
height: 100%;
}
此代码生成此布局:
但是,如您所见,div #main-container 直到布局结束时才会增长。
div 跟他内容的高度保持一致。
我想要的是这个 div 总是填满整个页面,像这样:
互联网上的许多解决方案都说我将min-height 修复为一些测试值,但是这样
我将无法让我的网站保持响应(保持我的布局对我来说非常重要
始终响应,这是我使用 Bootstrap 3) 的主要原因。
其他解决方案是使用 javascript 计算 div 高度。个人不喜欢 这个解决方案。我希望我只能通过使用 CSS 来解决这个问题。
有人知道如何解决这个问题吗?
【问题讨论】:
-
您可以通过将#wrap 的背景颜色设置为与#main-container 相同的值来创建#main-container 增长到最后的错觉。
-
是的,我一遍又一遍地遇到同样的问题,但我相信至少不能用 CSS 和 HTML 做到这一点
-
大家好,感谢您的帮助! @AntonisGrigoriadis,您的解决方案非常完美!我所要做的就是将我的
#wrap设置为与#main-container相同的宽度并设置背景颜色。 -
我真的不敢相信设置
#main-containermin-height 不起作用...有人知道原因吗? -
好主意@AntonisGrigoriadis 谢谢!我的主容器 div 有一个半透明的背景,所以我必须为父 div 使用与主容器的宽度和位置相匹配的透明 png 背景图像。这显然需要在较小的屏幕宽度上进行调整,因为背景图像不会总是适应布局的其余部分。
标签: html css responsive-design twitter-bootstrap-3