Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align footer (div) to the bottom of the page? [duplicate]

Can anyone explain how to align a footer div to the bottom of the page. From the examples I've seen, they all show how to make the div stay visible at the bottom, no matter where you've scrolled the page. Although I don't want it like that. I want it fixed at the bottom of the page, so it doesn't move. Appreciate the help!

like image 313
Joey Morani Avatar asked Aug 19 '10 19:08

Joey Morani


People also ask

How do I align footer to bottom of page?

To do this just remove margin top from footer and set margin top and bottom to auto margin: auto 0; on your main content (in my case article element) or margin: auto; to center it on both direction (vertically and horizontally) and it will make content align to center. Your CSS should look something like this.

How do you make a div align to the bottom of the page?

Try position:fixed; bottom:0; . This will make your div to stay fixed at the bottom.

How do I stick footer to the bottom when page content is less?

Quick answer: Add “display:flex; flex-direction:column; min-height:100vh;” to body or appropriate layout body element, then add “flex:1;” to content wrapper element/section. I will explain few ways to keep or stick the footer to the bottom of the page if there is not enough content.


1 Answers

UPDATE

My original answer is from a long time ago, and the links are broken; updating it so that it continues to be useful.

I'm including updated solutions inline, as well as a working examples on JSFiddle. Note: I'm relying on a CSS reset, though I'm not including those styles inline. Refer to normalize.css

Solution 1 - margin offset

https://jsfiddle.net/UnsungHero97/ur20fndv/2/

HTML

<div id="wrapper">   <div id="content">     <h1>Hello, World!</h1>   </div> </div> <footer id="footer">   <div id="footer-content">Sticky Footer</div> </footer> 

CSS

html, body {   margin: 0px;   padding: 0px;   min-height: 100%;   height: 100%; }  #wrapper {   background-color: #e3f2fd;   min-height: 100%;   height: auto !important;   margin-bottom: -50px; /* the bottom margin is the negative value of the footer's total height */ }  #wrapper:after {   content: "";   display: block;   height: 50px; /* the footer's total height */ }  #content {   height: 100%; }  #footer {   height: 50px; /* the footer's total height */ }  #footer-content {   background-color: #f3e5f5;   border: 1px solid #ab47bc;   height: 32px; /* height + top/bottom paddding + top/bottom border must add up to footer height */   padding: 8px; } 

Solution 2 - flexbox

https://jsfiddle.net/UnsungHero97/oqom5e5m/3/

HTML

<div id="content">   <h1>Hello, World!</h1> </div> <footer id="footer">Sticky Footer</footer> 

CSS

html {   height: 100%; }  body {   display: flex;   flex-direction: column;   min-height: 100%; }  #content {   background-color: #e3f2fd;   flex: 1;   padding: 20px; }  #footer {   background-color: #f3e5f5;   padding: 20px; } 

Here's some links with more detailed explanations and different approaches:

  • https://css-tricks.com/couple-takes-sticky-footer/
  • https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/
  • http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page

ORIGINAL ANSWER

Is this what you mean?

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

This method uses only 15 lines of CSS and hardly any HTML markup. Even better, it's completely valid CSS, and it works in all major browsers. Internet Explorer 5 and up, Firefox, Safari, Opera and more.

This footer will stay at the bottom of the page permanently. This means that if the content is more than the height of the browser window, you will need to scroll down to see the footer... but if the content is less than the height of the browser window, the footer will stick to the bottom of the browser window instead of floating up in the middle of the page.

Let me know if you need help with the implementation. I hope this helps.

like image 142
Hristo Avatar answered Oct 17 '22 06:10

Hristo