I am having great difficulty making my footer stick to the bottom of the page in my angular application. I have tried a number of different things but cant seem to figure out what i am doing wrong. I have defined the height of the container div so i know the viewport size therefore the footer should be able to identify the bottom of the viewport and stay there. However as the content grows the footer does not grow with the content.
HTML:
<body style="margin:0; padding:0; height:100%;">
<app-root></app-root>
</body>
app-root html:
<div class="container">
<app-header id="header"></app-header>
<div id="body">
<router-outlet></router-outlet>
</div>
<app-footer id="footer"></app-footer>
</div>
CSS:
html,
body {
margin:0;
padding:0;
height:100%;
}
#container {
min-height:100%;
height:100%;
position:relative;
}
#header {
padding-bottom:10px;
}
#body {
padding:10px;
padding-bottom:10px;
}
#footer {
position: absolute;
bottom:0;
width:100%;
height:60px;
}
Put your app-footer in separate div and add the following class:
<div class="fixed-bottom">
<app-footer></app-footer>
</div>
in style:
.fixed-bottom {
position: fixed;
right: 0;
bottom: 0;
left: 0;
z-index: 1030;
}
Using flexbox is probably going to offer the cleanest implementation and has good browser support.
Here is what I've done in the past, noting that the content is just there so the snippet displays as intended.
The structure below is similar to your question. You may need to break it down into components as suited for your app. Note my usage of class selectors instead of id selectors, as another answer noted there was a typo in yours for #container and .container.
html, body {
height: 100%;
margin: 0;
}
.container {
height: 100%;
display: flex;
flex-direction: column;
}
.header {
background-color: powderblue;
}
.content {
flex: 1 0 auto;
background-color: salmon;
}
.footer {
flex-shrink: 0;
background-color: orchid;
}
<body>
<app-root>
<div class="container">
<header class="header">
<app-header>header content</app-header>
</header>
<main class="content">
<router-outlet>main content</router-outlet>
</main>
<footer class="footer">
<app-footer>footer content</app-footer>
</footer>
</div>
</app-root>
</body>
This answer is based on a snippet from Sticky Footer, Five Ways using the flexbox option. You can view alternatives in that article.
1) typo error css assign to id #container HTML use to class
2) Remove height #container
3) Add padding-bottom #body is footer height;
changes CSS
html,
body {
margin:0;
padding:0;
height:100%;
}
#container {
min-height:100%;
/*height:100%;*/ /*Remove this*/
position:relative;
}
#header {
padding-bottom:10px;
}
#body {
padding:10px;
padding-bottom:60px; /* Add Padding footer Height*/
}
#footer {
position: absolute;
bottom:0;
width:100%;
height:60px;
}
Using flexbox this should be quite simple.
<body>
<header>...</header>
<main class="main-content">
...
...
</main>
<footer>...</footer>
</body>
Whatever is your main content container (in this example it is .main-content class, apply this flex style to force it takes max height pushing the footer to the bottom.
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.content {
flex: 1 0 auto;
}
Related
So I made a contact page and I want the footer div to be sticking to the bottom of the page not right after the contact form.
But if I put everything to a container div with height:100%; and make footer bottom:0; then the page will be "too long", you have to scroll, etc...
My css so far:
#footer{
background-color:#fff;
font:bold 14px;
color:#1E88E5;
width:100%;
height:auto;
padding:1%;
border-top:1px solid #1E88E5;
}
Footer is just a normal full width div with some centered text atm.
You can probably use position: fixed to achieve this.
.footer {
position: fixed;
bottom: 0;
}
With this you will need to offset the bottom of the page so would suggest adding a padding-bottom to .main that is the height of the footer.
.main {
padding-bottom: 30px /*whatever the height of your footer is*/
}
Pritesh Gupta's solution works really well for me:
I'm copy+pasting the code in case their site goes down:
Here's the HTML:
<!DOCTYPE html>
<html>
<head>
<title>Sticky Footer</title>
</head>
<body>
<main>stuff</main>
<footer>© 2016</footer>
</body>
</html>
Here's the CSS:
body {
margin: 0;
}
main {
min-height: calc(100vh - 4rem);
}
footer {
height: 4rem;
}
I don't know if it works in old browsers but I'm not so worried about that myself.
It also depends on you knowing the height of your footer, although it's worth pointing out that you don't necessarily have to set the height manually like in the code above since you can always figure out what it is if you know how much vertical padding and line-height the contents have...
Hope this helps, I spent most of the morning trying every single sticky footer tutorial on the web before stumbling across this technique and whilst other techniques do work this one requires minimal effort.
If you need sticky footer you can make it with 2 solutions.
Solution 1:
HTML:
<div class="wrap">
Content
</div>
<div class="footer">
Sticky Footer
</div>
CSS:
body, html, .wrap{
height:100%;
}
body > .wrap{
height:auto;
min-height:100%;
}
.wrap:after {
content: "";
display: block;
height: 100px;
}
.footer{
background:#662e8c;
margin-top:-100px;
height:100px;
color:#fff;
position:relative;
line-height:180%;
padding:0 10px;
}
Example: https://jsfiddle.net/ta1amejn/
Solution 2 (With table properties):
HTML:
Content
Footer
CSS:
body{
display:table;
width: 100%;
}
html, body{
height:100%;
}
.main{
height:100%;
width:100%;
padding-bottom:20px;
background:#eee;
display:table-row;
}
.footer{
/*height:30px;*/
line-height:30px;
width:100%;
background:#00f0ad;
display:table-row;
}
Example: https://jsfiddle.net/zbtaoq1b/
If you want a fixed footer use this solution:
.footer{
position: fixed;
bottom: 0;
}
You can do that easily with the display: flex.
You don't care about height body or wrapper tag.
Example: Please change the height of main tag any value if you want, footer always sticky to bottom(not position: fixed).
https://codepen.io/tronghiep92/pen/dzwRrO
HTML markup
<div id="wrapper">
<header>my header</header>
<main>main content, please change height</main>
<footer>
my footer
</footer>
</div>
CSS Solution
#wrapper {
display: flex;
flex-direction: column;
min-height: 100vh;
}
header {
height: 100px;
background: yellow;
}
footer {
height: 50px;
background: red;
margin-top: auto; /* this is the solution */
}
main {
height: 100px
}
Or you can:
#wrapper {
display: flex;
flex-direction: column;
justify-content: space-between;
min-height: 100vh;
}
header {
height: 100px;
background: yellow;
}
footer {
height: 50px;
background: red;
}
main {
flex: 1;
height: 100px;
}
I found other posts here but none of them had an explanation of the problem. Here is a basic example of what I'm talking about: https://jsfiddle.net/p1e5krn6/.
HTML
<nav>
<ul>
<li>About</li>
</ul>
</nav>
</header>
<div id="content">
Lorem ipsum dolor sit amet.
<div id="one">asdasd</div>
<div id="two">asdasdasd</div>
</div>
<footer>footer</footer>
</body>
CSS
html, body {
height: 100%;
background-color: yellow;
}
header,footer{ background:blue; }
header {
width: 100%;
height: 100%;
}
header nav ul li {
display: inline-block;
padding: 0 30px 0 0;
}
#content {
background-color: pink;
width: 60%;
height:100%;
margin: 0 auto;
}
#one, #two {
height:100%;
}
footer {
width: 100%;
}
What I would really like to know is why that footer goes up like that. I know how to solve the problem, I could put the footer inside the content div but that would be semantically wrong. What I would like to have is not a solution but a reason that would explain me the behavior of that footer.
This is happening because you have given 100% height to the div #one and #two along with 100% height to content. Logically if they are part of the content only, then why give then complete height as well. Removing that property from those two divs solves the problem. See the fiddle: "https://jsfiddle.net/p1e5krn6/1/"
Remove the following style:
#one, #two{
height:100%;
}
Can you try this one? demo
*{
float:left;
}
I need a layout like header content and footer.
My requirements are...
The default position of footer Div is at bottom even no content in content Div.
When the content expand of content Div then whole body(including header,footer) need to be expand based on the content Div
I don't need position:fixed for the footer Div....
I already tried some code in my project......
CSS:
body
{
height:100%;
margin-left: 0;
margin-top: 0;
padding:0;
width:100%;
position: relative;
display:block;
}
.container
{
display:table;
width:100%;
height:100%;
}
.header
{
top:0px;
height:75px;
width:100%;
}
.content
{
width:100%;
height:auto;
position: relative;
}
.footer
{
top:5px;
bottom:0px;
height:45px;
overflow:hidden;
width:100%;
}
CODE:
<div>
<div class="header">
Header Div
</div>
<div class="content">
Content Div
</div>
<div class="footer">
Footer Div(Need it,default at bottom position)
</div>
</div>
Any Idea?
Note:I need to run this code in IE also......
add the following style to your body tag or content div element:
div {
min-height: 500px;
height:auto !important;
height: 500px;
}
This works because IE treats "height" how "min-height" is supposed to be treated.
html{
height: 100%;
}
body {
min-height: 100%;
}
I want my Footer to be aligned to bottom of browser if there is not enough data on page.Above code works for HTML and body but it fails for form tag in ASPX pages (as discussed here). How can we force form tag to have 100% height? adding bellow is not working.
body, form {
min-height: 100%;
}
Look at here may solve your problem and get you the understanding of min-height
AND FIDDLE
The CSS
.maindiv {
overflow:hidden; border:#000 1px solid;
/*changes*/
height:100%;
}
.left_inner {
min-height:100%; background:#00CC33;
/*changes*/
width:100%;
display:inline-block;
vertical-align:top;
}/*changes*/
html,
body{
height:100%;
}
.footer
{
background:red;
}
The HTML
<div class="maindiv">
<div class="left_inner">Left Block content</div>
</div>
<div class="footer">footer</div>
I have the following html:
<body>
<div id="page">
<div id="header"></div>
<div id="content"></div>
</div>
</body>
and css:
html,body {
margin:0;
padding:0;
height:100%;
}
#page {
height:100%;
margin:auto;
left:0;
right:0;
width:500px;
}
#header {
height:100px;
width:500px;
}
#content {
width:500px;
height:100%;
}
The problem is that content div is the height of the window + the height of the header.
How can i make it to be the height of the window - the height of the header, I mean to stretch horizontally all over the remaining window. ??
In case you don't need to support IE7 and below - you can use a useful trick with
position: absolute
for #header and
padding-top: 100px;
box-sizing: border-box;
for #content.
Check out this fiddle: http://jsfiddle.net/Jx4sC/2/
Details regarding box-sizing support: http://caniuse.com/#feat=css3-boxsizing
You could use calc() in modern browsers and let the browser calculate the height of your content box:
#content {
width:500px;
height: calc(100% - 100px);
}
Similarly you could use some JavaScript to do the same. But then make sure to update your calculations each time the browser height gets changed.
This is supprisingly hacky to get going and you may not have to do it. for example, say you wanted to give #content a background-color, put it on #page instead.
html,body {
margin:0;
padding:0;
/* body should have height:100% by default */
}
#page {
height:100%;
margin: 0 auto;
width:500px;
/* use page as you would have used #content*/
}
#header {
height:100px;
}
#content {
}
edit: but if you really need to do this, you can do it like so
#page {
position: relative;
}
#content{
position: absolute;
top: 100px;
bottom: 0px;
left: 0px;
right: 0px;
}
this is not ideal because if you wanted to make #header bigger you need to remember to update #content, you can no longer use the normal page layout