我有一个简单的2列布局,带有一个脚注,可以清除标记中的左右div。我的问题是,我不能让页脚留在所有浏览器的页面底部。如果内容向下推页脚,它就会起作用,但情况并不总是如此。
当前回答
CSS:
#container{
width: 100%;
height: 100vh;
}
#container.footer{
float:left;
width:100%;
height:20vh;
margin-top:80vh;
background-color:red;
}
HTML:
<div id="container">
<div class="footer">
</div>
</div>
如果你正在寻找一个在页面底部对齐的响应性页脚,它总是保持视口高度的80%的上距,那么这个方法应该是有用的。
其他回答
保持<main>为min-height 90vh将永远解决您的问题。 下面是基本结构,它将帮助您遵循语义并覆盖整个页面。
第一步:除了页眉和页脚,所有内容都放在主标签内。
<body>
<header>
<!╌ nav, logo ╌>
</header>
<main>
<!╌ section and div ╌>
</main>
<footer>
<!╌ nav, logo ╌>
</footer>
</body>
步骤2:添加min-height: 90vh为主
main{
min-height: 90vh;
}
通常,页眉和页脚的最低高度是70px,所以这种情况下工作良好,尝试和测试!
jQuery跨浏览器自定义插件- $.footerBottom()
或者像我一样使用jQuery,并设置你的页脚高度为auto或fix,无论你喜欢什么,它都会工作。这个插件使用jQuery选择器,所以要使它工作,你必须包括jQuery库到你的文件。
下面是运行插件的方法。导入jQuery,复制这个自定义jQuery插件的代码,导入jQuery后再导入!这是非常简单和基本的,但很重要。
当你这样做的时候,你所要做的就是运行这段代码:
$.footerBottom({target:"footer"}); //as html5 tag <footer>.
// You can change it to your preferred "div" with for example class "footer"
// by setting target to {target:"div.footer"}
不需要将它放在document ready事件中。就这样,它会运行得很好。它会重新计算你的页脚的位置,当页面被加载,当窗口被调整大小。
下面是插件的代码,你不需要理解。只要知道如何实施就行了。它为你工作。但是,如果您想知道它是如何工作的,只需查看代码即可。我给你留言了。
//import jQuery library before this script
// Import jQuery library before this script // Our custom jQuery Plugin (function($) { $.footerBottom = function(options) { // Or use "$.fn.footerBottom" or "$.footerBottom" to call it globally directly from $.footerBottom(); var defaults = { target: "footer", container: "html", innercontainer: "body", css: { footer: { position: "absolute", left: 0, bottom: 0, }, html: { position: "relative", minHeight: "100%" } } }; options = $.extend(defaults, options); // JUST SET SOME CSS DEFINED IN THE DEFAULTS SETTINGS ABOVE $(options.target).css({ "position": options.css.footer.position, "left": options.css.footer.left, "bottom": options.css.footer.bottom, }); $(options.container).css({ "position": options.css.html.position, "min-height": options.css.html.minHeight, }); function logic() { var footerOuterHeight = $(options.target).outerHeight(); // Get outer footer height $(options.innercontainer).css('padding-bottom', footerOuterHeight + "px"); // Set padding equal to footer height on body element $(options.target).css('height', footerOuterHeight + "!important"); // Set outerHeight of footer element to ... footer console.log("jQ custom plugin footerBottom runs"); // Display text in console so ou can check that it works in your browser. Delete it if you like. }; // DEFINE WHEN TO RUN FUNCTION $(window).on('load resize', function() { // Run on page loaded and on window resized logic(); }); // RETURN OBJECT FOR CHAINING IF NEEDED - IF NOT DELETE // return this.each(function() { // this.checked = true; // }); // return this; }; })(jQuery); // End of plugin // USE EXAMPLE $.footerBottom(); // Run our plugin with all default settings for HTML5 /* Set your footer CSS to what ever you like it will work anyway */ footer { box-sizing: border-box; height: auto; width: 100%; padding: 30px 0; background-color: black; color: white; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- The structure doesn't matter much, you will always have html and body tag, so just make sure to point to your footer as needed if you use html5, as it should just do nothing run plugin with no settings it will work by default with the <footer> html5 tag --> <body> <div class="content"> <header> <nav> <ul> <li>link</li> <li>link</li> <li>link</li> <li>link</li> <li>link</li> <li>link</li> </ul> </nav> </header> <section> <p></p> <p>Lorem ipsum...</p> </section> </div> <footer> <p>Copyright 2009 Your name</p> <p>Copyright 2009 Your name</p> <p>Copyright 2009 Your name</p> </footer>
我自己有时也与此斗争,我总是发现所有这些div在彼此之间的解决方案是一个混乱的解决方案。 我只是胡乱摆弄了一下,我个人发现这是有效的,而且这当然是最简单的方法之一:
html {
position: relative;
}
html, body {
margin: 0;
padding: 0;
min-height: 100%;
}
footer {
position: absolute;
bottom: 0;
}
我喜欢这样做的原因是不需要应用额外的HTML。您可以简单地添加这个CSS,然后编写您的HTML
带有显示的粘性页脚:flex
解决方案的灵感来自菲利普沃尔顿的粘性页脚。
解释
此解决方案仅在以下情况下有效:
Chrome≥21.0 Firefox浏览器≥20.0 Internet Explorer浏览器≥10 Safari≥6.1
它基于伸缩显示,利用了flex-grow属性,该属性允许元素在高度或宽度上增长(当流动方向分别设置为列或行时),以占用容器中的额外空间。
我们还将利用vh单位,其中1vh被定义为:
视口高度的1/100
因此,100vh的高度是告诉一个元素跨越整个视口高度的简洁方法。
这是你如何组织你的网页:
----------- body -----------
----------------------------
---------- footer ----------
----------------------------
为了让页脚固定在页面底部,您希望正文和页脚之间的空间增加到将页脚推到页面底部所需的大小。
因此我们的布局变成:
----------- body -----------
----------------------------
---------- spacer ----------
<- This element must grow in height
----------------------------
---------- footer ----------
----------------------------
实现
身体{ 保证金:0; 显示:flex; flex-direction:列; 最小高度:100 vh; } .spacer { flex: 1; } /*为演示的目的使它可见*/ .footer { 高度:50 px; 背景颜色:红色; } 身体< > <div class="content">Hello World!< / div > < div class = "隔离" > < / div > <页脚类=“页脚”> < /页脚> 身体< / >
你可以在JSFiddle玩。
Safari怪癖
请注意,Safari对flex-shrink属性的实现有缺陷,该属性允许项缩小到显示内容所需的最小高度以上。 要解决这个问题,你必须在上面的例子中显式地将.content和页脚的flex-shrink属性设置为0:
.content {
flex-shrink: 0;
}
.footer {
flex-shrink: 0;
}
或者,将spacer元素的flex样式更改为:
.spacer {
flex: 1 0 auto;
}
这种3值简写样式相当于以下完整设置:
.spacer {
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
}
优雅的工作无处不在。
如果你不希望它使用位置固定,并在移动设备上烦人地跟随你,这似乎对我来说是有效的。
html {
min-height: 100%;
position: relative;
}
#site-footer {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
padding: 6px 2px;
background: #32383e;
}
只需要将html设置为min-height: 100%;位置是相对的,位置是绝对的;底部:0;左:0;在页脚。然后确保页脚是主体中的最后一个元素。
如果这对其他人不起作用,请告诉我,以及为什么。我知道这些乏味的风格技巧在我没有想到的各种情况下会表现得很奇怪。