0

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.

enter image description here

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;   
}
ashley g
  • 857
  • 3
  • 11
  • 21
  • why not try using a [flex approach](https://stackoverflow.com/questions/29069498/how-to-make-a-sticky-footer-using-css#answer-52178548) (also does your html appear inside your body tag or is that actually how it is?) – Pete Dec 11 '19 at 10:59
  • I have tried the flexbox approach and have had no luck. Yes, it is a angular based application and the 'app-root' is the second chunk of html you see – ashley g Dec 11 '19 at 11:02
  • Try position: fixed instead – Mathew Berg Dec 11 '19 at 11:05
  • i dont want the footer to be fixed i want it to be sticky – ashley g Dec 11 '19 at 11:06

4 Answers4

2

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;
}
ivaigult
  • 6,198
  • 5
  • 38
  • 66
raj
  • 21
  • 1
  • 1
  • 6
1

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.

Ash
  • 11,292
  • 4
  • 27
  • 34
  • Nice solution, The problem was happening due to me absolutely positioning an element with the content div . – ashley g Dec 11 '19 at 11:57
0

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;   
}
Lalji Tadhani
  • 14,041
  • 3
  • 23
  • 40
0

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;
}