CSS:固定到底部并居中

时间:2009-06-09 16:24:58

标签: css positioning sticky-footer

我需要将页脚固定到页面底部并使其居中。页脚的内容可能会一直发生变化,因此我无法通过margin-left将其居中:xxpx; margin-right:xxpx;

问题在于,由于某些原因,这不起作用:

#whatever {
  position: fixed;
  bottom: 0px;
  margin-right: auto;
  margin-left: auto;
}

我抓了网,一无所获。我试着制作一个容器div和nada。我尝试了其他组合和gurnisht。我怎样才能做到这一点?

由于

9 个答案:

答案 0 :(得分:49)

您应该使用粘性页脚解决方案,例如:

* {
    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>

答案 1 :(得分:37)

修订code by Daniel Kanis

只需更改CSS中的以下行

即可
.problem {text-align:center}
.enclose {position:fixed;bottom:0px;width:100%;}

并在html中:

<p class="enclose problem">
Your footer text here.
</p>

答案 2 :(得分:26)

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更容易。

http://jsfiddle.net/gLhEZ/

答案 3 :(得分:5)

问题在于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);
}

但我推荐第一种方法。 我使用了这个答案的居中技巧: How to center absolutely positioned element in div?

答案 4 :(得分:3)

我看到有两个潜在的问题:

1 - IE在位置上遇到了麻烦:过去已修复。如果您使用的IE7 +具有有效的doctype或非IE浏览器,则这不是问题的一部分

2 - 如果希望页脚对象居中,则需要指定页脚的宽度。否则它默认为页面的整个宽度,左侧和右侧的自动边距设置为0.如果您希望页脚栏占据宽度(如StackOverflow通知栏)并将文本居中,那么您需要将“text-align:center”添加到您的定义中。

答案 5 :(得分:3)

我把'问题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>

你去! - Hypertextie

答案 6 :(得分:1)

根据@Michael的评论:

  

有一种更好的方法可以做到这一点。只需创建身体   position:相对和填充页脚大小+空格   在你想要的内容和页脚之间。然后只需制作一个页脚div   绝对和底部:0。

我去挖掘解释,归结为:

  • 默认情况下,底部的绝对位置:0px将其设置为窗口底部...而不是页面底部。
  • 相对定位元素会重置其子项的绝对位置范围...所以通过将主体设置为相对定位,底部的绝对位置:0px现在真正反映了页面的底部。

http://css-tricks.com/absolute-positioning-inside-relative-positioning/

的更多详情

答案 7 :(得分:0)

这是一个使用css网格的例子。

&#13;
&#13;
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;
}
&#13;
<!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>
&#13;
&#13;
&#13;

你可以使用css grid:一个具体的例子

答案 8 :(得分:0)

我遇到了一个问题,其中典型的position: fixedbottom: 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>