如何使用CSS制作粘性页脚?

问题描述 投票:5回答:5

我想将页脚放在页面底部。我试试这个

position: absolute;
left: 0;
bottom: 0;
height: 100px;
width: 100%;

但我的页脚变得凌乱。我的网站是用WordPress制作的。如果可能的话,我不想为此使用任何插件。而且我只想使用纯CSS。

这里是CSS脚本

footer #bottom-footer{
background: none repeat scroll 0 0 #FFFFFF;
color: #000000;
border-top: 5px solid #F80000;
text-align: left;
padding: 9px;
font-size: 13px;
}
.footer-wrap a{
color:#000000;
}
.footer-wrap a:hover{
color:#F80000;
}
.footer-logo a {
margin-bottom: 6px;
display: block;
}
.footer-socials {
float: left;
line-height: 0px;
}
.footer-socials a {
border-radius: 100%;
color: #ffffff;
display: inline-block;
font-size: 14px;
height: 30px;
line-height: 30px;
margin-left: 3px;
text-align: center;
vertical-align: middle;
width: 30px;
}
.footer-socials a.facebook {
background: none repeat scroll 0 0 #1f69b3;
}
.footer-socials a.twitter {
background: none repeat scroll 0 0 #43b3e5;
}
.footer-socials a.gplus {
background: none repeat scroll 0 0 #d84734;
}
.footer-socials a.youtube {
background: none repeat scroll 0 0 #df2126;
}
.ak-contact-address .socials a.pinterest {
background: none repeat scroll 0 0 #ff3635;
}
.footer-socials a.linkedin {
background: none repeat scroll 0 0 #1a7696;
}
.footer-socials .socials a.flickr {
background: none repeat scroll 0 0 #e1e2dd;
}
.footer-socials .socials a.vimeo {
background: none repeat scroll 0 0 #7fdde8;
}
.footer-socials .socials a.instagram {
background: none repeat scroll 0 0 #c8c5b3;
}
.footer-socials .socials a.tumblr {
background: #395976;
}
.footer-socials .socials a.rss {
background: none repeat scroll 0 0 #fbc95d;
}
.footer-socials .socials a.github {
background: none repeat scroll 0 0 #383838;
}
.footer-socials .socials a.stumbleupon {
background: none repeat scroll 0 0 #e94c29;
}
.footer-socials .socials a.skype {
background: none repeat scroll 0 0 #09c6ff;
}
.footer-socials .social-icons span {
cursor: pointer;
display: inline-block;
}
.footer-socials .socials i {
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
backface-visibility: hidden;
}
.tagcloud a{
font-size: 13px !important;
background: rgba(0,0,0,0.4);
padding: 8px 10px;
margin: 0 2px 4px 0;
display: inline-block; 
line-height: 1;
}
.sidebar .tagcloud a{
background: #23A38F;
color: #FFF;
}

Website link

css sticky-footer
5个回答
9
投票

遵循从在线源不再可用的干净方法(死链接),您的页面应该需要的最小代码(注意 - 可能最好使用#bottom-footer而不是footer #bottom-footer来选择页脚 - 这可能是其中的一部分问题):

html {
    position: relative;
    min-height: 100%;
}
body {
    margin: 0 0 100px; /* bottom = footer height */
}
#bottom-footer {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 100px;
    width: 100%;
}

2
投票

这很完美。这是W3SCHOOLS的例子

https://www.w3schools.com/howto/howto_css_fixed_footer.asp

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.footer {
   position: fixed;
   left: 0;
   bottom: 0;
   width: 100%;
   background-color: red;
   color: white;
   text-align: center;
}
</style>
</head>
<body>

<h2>Fixed/Sticky Footer Example</h2>
<p>The footer is placed at the bottom of the page.</p>

<div class="footer">
  <p>Footer</p>
</div>

</body>
</html> 

1
投票

Bootstrap有一个你可以使用的粘性页脚。

http://getbootstrap.com/examples/sticky-footer/

或者你可以用CSS和jQuery来做:

https://css-tricks.com/snippets/css/sticky-footer/

希望有所帮助。


0
投票

一些使用flex box CSS和以下HTML结构的现代方法:

<body>
    <header></header>
    <main></main>
    <footer></footer>
</body>

方法1 :(固定高度页脚)将qazxsw poi和qazxsw poi应用于qazxsw poi。将display:flexflex-direction:column)应用于body元素。

主要元素将垂直生长以占据任何空白空间,从而使页脚粘到底部。

flex:1
flex-grow:1

方法2 :(固定高度页脚)将mainbody { display: flex; flex-direction: column; min-height: 100vh; margin:0; } header { background-color: #cffac7; height:50px; } main { background-color: #f8e2ff; flex:1; } footer { background-color: #fceec7; height:50px; }应用于<header></header> <main></main> <footer></footer>。应用display:flex flex-direction:column

你完成了,因为body,使页脚粘到底部。请注意,这不取决于具有任何高度或内容的主要部分。在这种情况下,我们根本没有给出主要的弹性规则,因此它得到了margin-top:autofooter)的默认值。

方法3 :(流体高度页脚)这与#1中的技术完全相同,但元素没有固有的高度。凭借给予竞争元素的(无单位)flex: 0 1 auto值之间的比率,body { display: flex; flex-direction: column; min-height: 100vh; margin:0; } header { background-color: #cffac7; height:50px; } main { background-color: #f8e2ff; } footer { background-color: #fceec7; height:50px; margin-top:auto; }将以<header></header> <main></main> <footer></footer>flex-grow的速率增长五倍。

main
header

-1
投票

我在这里找到了答案

footer

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  margin:0;
}

header {
  background-color: #cffac7;
  flex:1;
}

main {
  background-color: #f8e2ff;
  flex:5;
}

footer {
  background-color: #fceec7;
  flex:1 
}
© www.soinside.com 2019 - 2024. All rights reserved.