-1

I am trying to position footer at the bottom of the page, but not sticky. I have a problem when there is not enough content to cover the screen height, then the footer is not at the bottom. I have tried to set it up in CSS like this:

body {
  position: relative;
  margin: 0;
  padding-bottom: 8rem;
  min-height: 100%;
  overflow-x: hidden;
}

footer {
  position: absolute;
  display: flex;
  align-items: center;
  height: 4rem;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: $gray;
}

With this footer is only at the bottom of the page when there is enough content on it. But, when there is not enough content then it is above the bottom. Here is the example: enter image description here

Here is the fiddle.

When I add to the css file, like suggested in the answers:

html, body {
  height: 100%;
}

Then, I have a problem with pages where there is alot of content, then the footer is at the bottom on page load, but when I start to scroll down, it goes up with the content, like shown in the image:enter image description here

Leff
  • 1,968
  • 24
  • 97
  • 201

3 Answers3

0

Bydefault body take height as per inner content. Just add following CSS for this

html, body {
    height: 100%;
}

html, body {
  height: 100%;
}
body {
  position: relative;
  margin: 0;
  min-height: 100%;
  overflow-x: hidden;
}

footer {
  position: absolute;
  display: flex;
  align-items: center;
  height: 4rem;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: $gray;
  background: red;
}
<footer>Footer Container</footer>
Super User
  • 9,448
  • 3
  • 31
  • 47
0

This may be related to display property: Try to add this in your footer css:

display: block;

So that your footer css becomes:

footer {
  position: absolute;
  display: block;
  align-items: center;
  height: 4rem;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: $gray;
}

If you don't want to get rid of display: flex; then try to wrap it in any parent div and then apply the display property.

0

Change position value to relative in footer css block.May be its because of absolute you are getting that problem. Correct me if I am wrong.

Shashank
  • 13
  • 7