开发者

Help with footer always to bottom

I know this has been discussed here many times, but none of the answers I found here, seem to address my problem.

I have this variable (in height) layout, and wnat the footer to always stick to the bottom.

I have used the min-height: 100%; to the container div, and got it somehow to always be in the bottom. trouble is, it's sinking too low to the bottom.

I've put an example here:

http://jsbin.com/erono3

As you can see, my footer is at the bottom, but will go too far in the bottom, and even though there's space on the page to display it, it's creating a scroll bar.

Also, I'd like the main container to to be shown as big as the content is (i.e开发者_如何学运维. closing the square), but right now, it looks like the container is going all the way to the bottom, and my footer is covering it.

What am I doing wrong there?

Thanks in advance


You should take a look at the link by Ben Lee again :). I have used that in your layout to achieve the effect you want. See it here: http://jsbin.com/erono3/2

The important thing is for the footer to be part of the container. The container has a min-height of 100%. So it occupies the whole screen always. The header is normal what ever it is inside.

Then you should have an inner container element (important), where your main content resides. In the link above, it has the id #body. This would have a padding-bottom (to give space to the footer.

The footer is absolutely positioned with a bottom:0px meaning it is always going to be at the bottom of the container (the container has to be position:relative).

EDIT (in response to the comment)

To make your footer span the entire page, but keep everything else centered, just do this: remove the width off of the #containter, #container spans the whole page. Provide a width to the #body element in the link above and center it, using margin: 0px auto. You get the effect you wanted.

New link: http://jsbin.com/erono3/5


Here's a simplified version of this, which is worth reading for the explanation. See if you can adapt yours to fit.

CSS:

  html, body, div {
    margin: 0;
    border: 0;
    padding: 0;
  }
  html, body {
    height: 100%;
  }
  #wrap {
    position: relative;
    height: auto !important;
    height: 100%;
    min-height: 100%;
  }
  #footer {
    position: absolute;
    bottom: 0px;
    width: 100%;
    background-color: #aaa;
  }

and HTML:

<div id="wrap">
  <div id="content">Stuff goes here.</div>
  <div id="footer">FOOTER</div>
</div>


The problem is you have a min-height of 100% on your container div. That means that the container will be 100% the height of its parent, which is the body tag which has a height of 100%. So if your viewport is 600px, then your body will be 600px, then your container will be 100% of that which is 600px, and then it will stick the footer after the container div which is why it goes below the veiwport.

So one thing you can do is just absolutely position your footer inside the body. Do this by changing your position to be absolute, and bottom:0px. It will float at the bottom.

You might want to put it in your container as well depending on what style you are going for and position it absolute in that and at the bottom.


Your problem is not that the footer is too low, but by making the body 100% it pushes the footer below the bottom of the page.

Consider putting the footer div inside the container div and getting rid of the margin-top: -5.5em and position: relative and it will work just fine.


http://ryanfait.com/sticky-footer/

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

/*

Sticky Footer by Ryan Fait
http://ryanfait.com/

*/


This is particularly for anyone using ASP.NET master pages but also in general, if your content is also wrapped in a <form> element you will need to change

html, body {
    height: 100%;
}

to

html, body, form {
    height: 100%;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜