启动带3冲洗底部。不固定

我使用 Bootstrap 3为一个网站,我正在设计。

我想有一个这样的样本页脚。 样本

请注意,我不希望它固定,所以自举导航栏固定底部不解决我的问题。我只是希望它总是在底部的内容,也是响应。

任何导游都会非常感谢。


编辑:

抱歉我没说清楚。 现在发生的情况是,当内容主体没有足够的内容时。我的脚向上移动,然后在底部留下一个空位。

这是我现在的导航栏

<nav class="navbar navbar-inverse navbar-bottom" style="padding:0 0 120px 0">
<div class="container">
<div class="row">
<div class="col-sm-4">
<h5 id='footer-header'> SITEMAP </h3>
<div class="col-sm-4" style="padding: 0 0 0 0px">
<p>News</p>
<p>contact</p>
</div>
<div class="col-sm-4" style="padding: 0 0 0 0px">
<p>FAQ</p>
<p>Privacy Policy</p>
</div>
</div>
<div class="col-sm-4">
<h5 id='footer-header'> xxxx </h3>
<p>yyyyyyyyyyyyy</p>
</div>
<div class="col-sm-4">
<h5 id='footer-header'> xxxxx </h3>
<p>uuuuuuuuuuuuuuu</p>
</div>
</div>
</div>
</nav>

CSS

.navbar-bottom {
min-height: 300px;
margin-top: 100px;
background-color: #28364f;
padding-top: 35px;
color:#FFFFFF;
}
370979 次浏览

请看下面的例子。如果页面内容较少,这将使页脚位于底部; 如果页面内容较多,这将使页脚像正常的页脚一样工作。

CSS

* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: 100%;
margin: 0 auto -155px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
height: 155px; /* .push must be the same height as .footer */
}

超文本标示语言

<div class="wrapper">
<p>Your website content here.</p>
<div class="push"></div>
</div>
<div class="footer">
<p>Copyright (c) 2008</p>
</div>

更新 : 新版本的 Bootstrap 演示了如何在不添加包装的情况下添加粘性页脚。有关详细信息,请参阅 Jboy Flaga 的答案。

此方法使用最小标记。只要把你所有的内容放在一个。包装器,它的填充底部和负边距底部等于页脚高度(在我的例子中是100px)。

html, body {
height: 100%;
}


/* give the wrapper a padding-bottom and negative margin-bottom equal to the footer height */


.wrapper {
min-height: 100%;
height: auto;
margin: 0 auto -100px;
padding-bottom: 100px;
}
.footer {
height: 100px;
}


<body>


<div class="wrapper">
<p>Your website content here.</p>
</div>
<div class="footer">
<p>Copyright (c) 2014</p>
</div>


</body>

这里有一个来自 bootstrap 的简化解决方案(不需要创建新类) : http://getbootstrap.com/examples/sticky-footer-navbar/

打开该页面时,右键单击浏览器并“查看源代码”,然后打开粘贴脚注-导航栏. css 文件(http://getbootstrap.com/examples/sticky-footer-navbar/sticky-footer-navbar.css)

您可以看到您只需要这个 CSS

/* Sticky footer styles
-------------------------------------------------- */
html {
position: relative;
min-height: 100%;
}
body {
/* Margin bottom by footer height */
margin-bottom: 60px;
}
.footer {
position: absolute;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 60px;
background-color: #f5f5f5;
}

本 HTML

<html>
...
<body>
<!-- Begin page content -->
<div class="container">
</div>
...


<footer class="footer">
</footer>
</body>
</html>

Galen Gidman 已经发布了一个非常好的解决方案,来解决没有固定高度的响应式粘性页脚的问题。你可以在他的博客上找到他的完整解决方案: http://galengidman.com/2014/03/25/responsive-flexible-height-sticky-footers-in-css/

超文本标示语言

<header class="page-row">
<h1>Site Title</h1>
</header>


<main class="page-row page-row-expanded">
<p>Page content goes here.</p>
</main>


<footer class="page-row">
<p>Copyright, blah blah blah.</p>
</footer>

CSS

html,
body { height: 100%; }


body {
display: table;
width: 100%;
}


.page-row {
display: table-row;
height: 1px;
}


.page-row-expanded { height: 100%; }

更新: 这并不能直接完整地回答这个问题,但是其他人可能会发现这很有用。

这是用于响应页脚的 HTML

<footer class="footer navbar-fixed-bottom">
<div class="container">
</div>
</footer>

为了 CSS

footer{
width:100%;
min-height:100px;
background-color: #222; /* This color gets inverted color or you can add navbar inverse class in html */
}

注意: 在发布这个问题的时候,上面的代码行并没有把页脚推到页面内容的下面; 但是当页面上几乎没有内容时,它会阻止你的页脚在页面的中间爬行。有一个例子可以把页脚推到页面内容的下面,看看这里的 http://getbootstrap.com/examples/sticky-footer/

对于 Bootstrap:

<div class="navbar-fixed-bottom row-fluid">
<div class="navbar-inner">
<div class="container">
Text
</div>
</div>
</div>

这些解决方案中没有一个对我来说是完美的,因为我在页脚中使用了导航栏逆类。但是我得到了一个可行的解决方案,而且没有 Javascript。使用 Chrome 来帮助形成媒体查询。页脚的高度随着屏幕尺寸的变化而变化,因此您必须注意并相应地进行调整。页脚内容(我设置 id = “ footer”来定义我的内容)应该使用 position = Absolal 和 bottom = 0来保持在底部。宽度: 100% 。这是我的媒体查询 CSS。您必须调整最小宽度和最大宽度,并添加或删除一些元素:

#footer {
position: absolute;
color:  #ffffff;
width: 100%;
bottom: 0;
}
@media only screen and (min-width:1px) and (max-width: 407px)  {
body {
margin-bottom: 275px;
}


#footer {
height: 270px;
}
}
@media only screen and (min-width:408px) and (max-width: 768px)  {
body {
margin-bottom: 245px;
}


#footer {
height: 240px;
}
}
@media only screen and (min-width:769px)   {
body {
margin-bottom: 125px;
}


#footer {
height: 120px;
}
}

对于纯 CSS 解决方案,请在第2个 <hr>后滚动。第一个是最初的答案(回到2016年)


上述解决方案的主要缺陷是,他们有一个脚注 固定高度
在现实世界中,人们使用无数的设备,并且有这样一个坏习惯: 在你最不希望的时候旋转它们,而且 “噗”的一声! * * 在页脚后面就是你的页面内容!

在现实世界中,您需要一个函数来计算页脚的高度,并动态调整页面内容的 padding-bottom以适应该高度。 您需要在页面 loadresize事件以及页脚的 DOMSubtreeModified上运行这个小函数(以防您的页脚异步动态更新,或者它包含在与之交互时改变大小的动画元素)。

这里有一个概念的证明,使用 jQuery v3.0.0和 Bootstrap v4-alpha,但是没有理由它不应该在每个版本的低版本上工作。

jQuery(document).ready(function($) {
$.fn.accomodateFooter = function() {
var footerHeight = $('footer').outerHeight();
$(this).css({
'padding-bottom': footerHeight + 'px'
});
}
$('footer').on('DOMSubtreeModified', function() {
$('body').accomodateFooter();
})
$(window).on('resize', function() {
$('body').accomodateFooter();
})
$('body').accomodateFooter();
window.addMoreContentToFooter = function() {
var f = $('footer');
f.append($('<p />', {
text: "Human give me attention meow flop over sun bathe licks your face wake up wander around the house making large amounts of noise jump on top of your human's bed and fall asleep again. Throwup on your pillow sun bathe. The dog smells bad jump around on couch, meow constantly until given food, so nap all day, yet hiss at vacuum cleaner."
}))
.append($('<hr />'));
}
});
body {
min-height: 100vh;
position: relative;
}


footer {
background-color: rgba(0, 0, 0, .65);
color: white;
position: absolute;
bottom: 0;
width: 100%;
padding: 1.5rem;
display: block;
}


footer hr {
border-top: 1px solid rgba(0, 0, 0, .42);
border-bottom: 1px solid rgba(255, 255, 255, .21);
}
<link href="http://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="http://v4-alpha.getbootstrap.com/examples/starter-template/starter-template.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.2.0/js/tether.min.js"></script>
<script src="http://v4-alpha.getbootstrap.com/dist/js/bootstrap.min.js"></script>


<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse fixed-top">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>


<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>


<div class="container">
<div class="starter-template">
<h1>Feed that footer - not a game (yet!)</h1>
<p class="lead">You will notice the body bottom padding is growing to accomodate the height of the footer as you feed it (so the page contents do not get covered by it).</p><button class="btn btn-warning" onclick="window.addMoreContentToFooter()">Feed that footer</button>
<hr />
<blockquote class="lead"><strong>Note:</strong> I used jQuery <code>v3.0.0</code> and Bootstrap <code>v4-alpha</code> but there is no reason why it shouldn't work with lower versions of each.</blockquote>
</div>
</div>


<footer>I am a footer with dynamic content.
<hr />
</footer>

最初,我已经张贴了这个解决方案 给你,但我意识到它可能会帮助更多的人,如果张贴在这个问题。

注意: 我有意将 $([selector]).accomodateFooter()包装成一个 jQuery 插件,这样它就可以在任何 DOM 元素上运行,因为在大多数布局中,需要调整的不是 $('body')bottom-padding,而是一些带有 position:relative的页面包装元素(通常是 footer的直接父元素)。


稍后编辑(初次回答后3年以上) :

此时,我不再认为使用 JavaScript 在页面底部定位动态内容页脚是可以接受的。它可以实现与 CSS 单独使用,使用灵活方块,闪电快速,跨浏览器。

这就是:

// Left this in so you could inject content into the footer and test it:
// (but it's no longer sizing the footer)


function addMoreContentToFooter() {
var f = $('footer');
f.append($('<p />', {
text: "Human give me attention meow flop over sun bathe licks your face wake up wander around the house making large amounts of noise jump on top of your human's bed and fall asleep again. Throwup on your pillow sun bathe. The dog smells bad jump around on couch, meow constantly until given food, so nap all day, yet hiss at vacuum cleaner."
}))
.append($('<hr />'));
}
.wrapper {
min-height: 100vh;
padding-top: 54px;
display: flex;
flex-direction: column;
}


.wrapper>* {
flex-grow: 0;
}


.wrapper>main {
flex-grow: 1;
}


footer {
background-color: rgba(0, 0, 0, .65);
color: white;
width: 100%;
padding: 1.5rem;
}


footer hr {
border-top: 1px solid rgba(0, 0, 0, .42);
border-bottom: 1px solid rgba(255, 255, 255, .21);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>


<div class="wrapper">
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>


<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<main>
<div class="container">
<div class="starter-template">
<h1>Feed that footer - not a game (yet!)</h1>
<p class="lead">Footer won't ever cover the body contents, as its not fixed. It's simply placed at the bottom when the page should be shorter using `min-height:100vh` on container and using flexbox to push it down.</p><button class="btn btn-warning" onclick="addMoreContentToFooter()">Feed that footer</button>
<hr />
<blockquote class="lead">
<strong>Note:</strong> This example uses current latest versions of jQuery (<code>3.4.1.slim</code>) and Bootstrap (<code>4.4.1</code>) (unlike the one above).
</blockquote>
</div>
</div>
</main>
<footer>I am a footer with dynamic content.
<hr />
</footer>
</div>

对于仍在苦苦挣扎的人们来说,解决方案并不像你想的那样有效,这是我找到的最简单的解决方案。

包装您的主要内容,并为其分配一个最小视图高度。 将60调整到你的风格需要的任何值。

<div id="content" style="min-height:60vh"></div>
<div id="footer"></div>

就是这样,效果很好。

使用弹性箱,因为你可以使用它在你的处置。Bootstrap 4提供的解决方案仍然在搜索响应式布局中的重叠内容,例如: 它会在移动视图中崩溃,我遇到的最巧妙的技巧是使用 Flexbox 解决方案演示: (https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/)这样我们就不必处理固定高度的问题,这是一个过时的解决方案... 这个解决方案适用于 bootstrap 3和4,无论你使用哪个版本。

<body class="Site">
<header>…</header>
<main class="Site-content">…</main>
<footer>…</footer>
</body>


.Site {
display: flex;
min-height: 100vh;
flex-direction: column;
}


.Site-content {
flex: 1;
}

或者这个

<footer class="navbar navbar-default navbar-fixed-bottom">
<p class="text-muted" align="center">This is a footer</p>
</footer>

这就是使用 Flexbox对我有效的方法:

.body-class {
display: flex;
min-height: 100vh;
flex-direction: column;
}


.body-content {
flex: 1 0 auto;
width: 100%;
}


.footer {
width: 100%;
height: 60px;
background-color: #f5f5f5;
flex: none;
}

资料来源: https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/

使用任何类,并使其高度固定。它解决了我的问题。

<div class="container"></div>
<footer></footer>

CSS:

.container{
min-height: 60vh;
}