我需要将页脚固定在页面底部并居中。页脚的内容可能随时更改,因此我不能仅通过margin-left居中:xxpx; 右边距:xxpx;
问题是由于某种原因这不起作用:
#whatever {
position: fixed;
bottom: 0px;
margin-right: auto;
margin-left: auto;
}
我爬网,一无所获。我试着做一个容器div和nada。我尝试了其他组合和gurnisht。我怎样才能做到这一点?
谢谢
我需要将页脚固定在页面底部并居中。页脚的内容可能随时更改,因此我不能仅通过margin-left居中:xxpx; 右边距:xxpx;
问题是由于某种原因这不起作用:
#whatever {
position: fixed;
bottom: 0px;
margin-right: auto;
margin-left: auto;
}
我爬网,一无所获。我试着做一个容器div和nada。我尝试了其他组合和gurnisht。我怎样才能做到这一点?
谢谢
position:relative
和padding
页脚的大小+内容,你想页脚之间的空间。接下来,只要有一个页脚DIVabsolute
和bottom:0
。动臂去炸药。
Answers:
您应该使用这样的粘性页脚解决方案:
* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -142px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
height: 142px; /* .push must be the same height as .footer */
}
还有其他类似的东西。
* {margin:0;padding:0;}
/* must declare 0 margins on everything, also for main layout components use padding, not
vertical margins (top and bottom) to add spacing, else those margins get added to total height
and your footer gets pushed down a bit more, creating vertical scroll bars in the browser */
html, body, #wrap {height: 100%;}
body > #wrap {height: auto; min-height: 100%;}
#main {padding-bottom: 150px;} /* must be same height as the footer */
#footer {position: relative;
margin-top: -150px; /* negative value of footer height */
height: 150px;
clear:both;}
/* CLEAR FIX*/
.clearfix:after {content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;}
.clearfix {display: inline-block;}
/* Hides from IE-mac \*/
* html .clearfix { height: 1%;}
.clearfix {display: block;}
与html:
<div id="wrap">
<div id="main" class="clearfix">
</div>
</div>
<div id="footer">
</div>
Daniel Kanis修改的代码:
只需在CSS中更改以下几行
.problem {text-align:center}
.enclose {position:fixed;bottom:0px;width:100%;}
并在html中:
<p class="enclose problem">
Your footer text here.
</p>
jQuery解决方案
$(function(){
$(window).resize(function(){
placeFooter();
});
placeFooter();
// hide it before it's positioned
$('#footer').css('display','inline');
});
function placeFooter() {
var windHeight = $(window).height();
var footerHeight = $('#footer').height();
var offset = parseInt(windHeight) - parseInt(footerHeight);
$('#footer').css('top',offset);
}
<div id='footer' style='position: fixed; display: none;'>I am a footer</div>
有时,实现JS比破解旧CSS容易。
问题出在position: static
。静态意味着完全不对该位置做任何改动。position: absolute
是你想要的。虽然居中元素仍然很棘手。以下应该工作:
#whatever {
position: absolute;
bottom: 0px;
margin-right: auto;
margin-left: auto;
left: 0px;
right: 0px;
}
要么
#whatever {
position: absolute;
bottom: 0px;
margin-right: auto;
margin-left: auto;
left: 50%;
transform: translate(-50%, 0);
}
但是我推荐第一种方法。我从以下答案中使用了居中技术: 如何在div中将绝对定位的元素居中?
我已经将``问题div包裹在另一个div中'',让我们将此div称为封闭div ...使CSS中的封闭div的宽度为100%,位置固定为0的底部...然后将问题div插入封闭div这就是它的外观
#problem {margin-right:auto;margin-left:auto; /*what ever other styles*/}
#enclose {position:fixed;bottom:0px;width:100%;}
然后在html中...
<div id="enclose">
<div id="problem">
<!--this is where the text/markup would go-->
</div>
</div>
有呀!
-超文本
根据@Michael的评论:
有一种更好的方法可以做到这一点。只需创建带有position:relative和padding的页脚大小+所需内容和页脚之间的空间的主体。然后,将页脚div设为绝对值,并且bottom:0。
我去寻找解释,归结为:
有关更多详细信息,请访问http://css-tricks.com/absolute-positioning-inside-relative-positioning/
这是使用CSS网格的示例。
html, body{
height: 100%;
width: 100%;
}
.container{
height: 100%;
display:grid;
/*we divide the page into 3 parts*/
grid-template-rows: 20px auto 30px;
text-align: center; /*this is to center the element*/
}
.container .footer{
grid-row: 3; /*the footer will occupy the last row*/
display: inline-block;
margin-top: -20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="header">
</div>
<div class="content">
</div>
<div class="footer">
here is the footer
</div>
</div>
</body>
</html>
您可以使用CSS网格:一个具体示例
我遇到了一个问题,其中典型的position: fixed
和bottom: 0
没有工作。发现了带有的简洁功能position: sticky
。请注意,它是“相对”新的,因此它不适用于IE / Edge 15及更早版本。
这是w3schools的示例。
<!DOCTYPE html>
<html>
<head>
<style>
div.sticky {
position: sticky;
bottom: 0;
background-color: yellow;
padding: 30px;
font-size: 20px;
}
</style>
</head>
<body>
<p>Lorem ipsum dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dlerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dlerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dlerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dlerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas </p>
<div class="sticky">I will stick to the screen when you reach my scroll position</div>
</body>
</html>