On fixed footer position only at the bottom of the page

Asked

Viewed 320 times

6

I have a layout with menu fixed to the left, the page content occupying the rest of the space and a footer also fixed:

* {
  margin: 0;
  padding: 0;
  text-align: center;
}

nav {
  z-index: 2;
  width: 25%;
  height: 100vh;
  background-color: red;
  position: fixed;
}

main {
  z-index: 1;
  width: 75%;
  margin-left: 25%;
  height: 100vh;
  background-color: green;
  margin-bottom: 200px;
}

footer {
  z-index: -1;
  width: 100%;
  height: 200px;
  background-color: blue;
  position: fixed;
  bottom: 0;
}
<nav> MENU </nav>

<main> CONTEÚDO </main>

<footer> RODAPÉ </footer>

Fixed footer is for the purpose of the text stopped when scrolling

Is it possible to do (preferably only with CSS) that the footer overlaps the menu, but only at the bottom of the page? like this:

* {
  margin: 0;
  padding: 0;
  text-align: center;
}

nav {
  z-index: -1;
  width: 25%;
  height: 100vh;
  background-color: red;
  position: fixed;
}

main {
  z-index: 1;
  width: 75%;
  margin-left: 25%;
  height: 100vh;
  background-color: green;
  margin-bottom: 200px;
}

footer {
  z-index: 2;
  width: 100%;
  height: 200px;
  background-color: blue;
  position: fixed;
  bottom: 0;
}
<nav> MENU </nav>

<main> CONTEÚDO </main>

<footer> RODAPÉ </footer>

Detail, the menu should have the z-index greater than content due to shadows

  • Okay, young man, I’ve edited my answer with a much closer solution than I think you need. The idea is to leave the fixed menu with "transparent" background and the element that is behind runs along with the main usually doing the same effect! If you have any questions, let me know.

3 answers

4


This option has a slightly different HTML structure than yours. And background is actually in main, made with a linear-gradiente to "split" the layout into two columns. If the contents of main does not occupy the entire screen footer is appearing. But if the content is footer is only revealed when the content is finished. I used the heigth of main thus min-height: calc(100vh - 200px);

Here in the Snippet doesn’t look so cool footer is just the height of snippet, but will hardly have a situation that will bugging so, only if your menu is too long and the screen height is too narrow.

To better understand see the example: Show in Full Screen for a better result

html, body {
    width: 100%;
    height: 100%;
    margin: 0px;
    padding: 0;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
footer {
  z-index: -1;
  width: 100%;
  height: 200px;
  background-image: url(https://www.placecage.com/200/200);
  background-size: cover;
  position: fixed;
  bottom: 0;
}

main {
    display: flex;
    margin-bottom: 200px;
    background-image: linear-gradient(to right, #ddd 0, #ddd 25%, #333 25%, #fff 26%, #fff 100%);
    min-height: calc(100vh - 200px);
}
section {
    margin-left: 25%;
    height: 200vh;
}
nav {
    width: 25%;
    position: fixed;
}
<main>
    <nav>
        menu
    </nav>
    <section>
        <h1>conteúdo</h1>
    </section>
</main>

<footer>footer</footer>

This option does not depend on the layout. It just depends on your menu has only one background color...

The trick here is to untie the menu of your background color... like, the menu will remain fixed but with transparent background! The background is actually another div, that one div in turn works like the main and climbs into the scroll. So the menu is always fixed, but the "whole fund" is free to go up revealing the footer.

But if the scroll the page is too big the background will fade behind the menu. Then I point out the second option that is just below... But I also have some reservations that I will speak below.

OBS: You can make the shade with linear gradiente in the main to make the "shadow" and not a drop-shadow in the menu about the main. You can use the property background-attachment: fixed; if you want to fix the pseudo-fund of menu when you do the scroll.

See how it looks:

    
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
* {
  margin: 0;
  padding: 0;
  text-align: center;
}

nav {
  z-index: 2;
  width: 25%;
  height: 100vh;
  background-color: transparent;
  position: fixed;

}
.debugg {
  z-index: 1;
  width: 25%;
  margin-left: 0;
  height: 100vh;
  /* background-color: red; */
  margin-bottom: 200px;
  float: left;
  background-image: url(https://placecage.com/200/600);
  background-size: cover;
  background-attachment: fixed;
}

main {
  z-index: 1;
  width: 75%;
  margin-left: 25%;
  height: 100vh;
  margin-bottom: 200px;
  background-image: linear-gradient(to right, #999 0, #ddd 16px, #ddd 100%);
}

footer {
  z-index: -1;
  width: 100%;
  height: 200px;
  background-image: url(https://placecage.com/500/200);
  background-size: cover;
  position: fixed;
  bottom: 0;
}
<nav> MENU </nav>

<div class="debugg"></div>

<main> CONTEÚDO </main>

<footer> RODAPÉ </footer>


Option depending on layout

With CSS depending on the design of your layout you can create a pseudo element who will be tied up in Main and go up over the Menu when you scrollar. However, the blue you see cover the menu is not the footer, is that element is the after of main rising, giving the impression that is the footer appearing :)

That’s why I made the remark saying it will depend a lot on the design of your page...

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
* {
  margin: 0;
  padding: 0;
  text-align: center;
}

nav {
  z-index: 2;
  width: 25%;
  height: 100vh;
  background-color: red;
  position: fixed;
}

main {
  z-index: 1;
  width: 75%;
  margin-left: 25%;
  height: 100vh;
  background-color: green;
  margin-bottom: 200px;
}

footer {
  z-index: -1;
  width: 100%;
  height: 200px;
  background-color: blue;
  position: fixed;
  bottom: 0;
}
main::after {
  content: "";
  z-index: 20;
  width: 25%;
  height: 200px;
  background-color: blue;
  position: absolute;
  left: 0;
  top: 100%;
}
<nav> MENU </nav>

<main> CONTEÚDO </main>

<footer> RODAPÉ </footer>

  • Wow, you understand perfectly, the image was too much at the end of the first example, I’ll try at night, but, assuming the two options work, which you think is best?

  • @I think the first option will work in 90% of cases. But without having the image of your layout becomes difficult to know if it will work... But you still have the option to put a Background in the menu and fix it not to run in scroll...

  • If you want to take a look... https://costamilam.github.io/Profile

  • @Guilhermecostamilam is giving error 404

  • @Guilhermecostamilam I edited again, now to get closer to the layout of the link that you potou. Note the Obs I left before the example. Option 1 is certainly the best for your case

  • Looking better now, I realized that, in the first option, the text is on the footer, so it would have to be like on Monday, at night I will test and see which fits best

  • @Guilhermecostamilam I think I understand! There comes a time when the whole background disappears and the menu is over the footer right! If the scroll is extensive it can happen even... Ai the second option would be the best since you only have solid colors! Even include this detail in an issue I did in the reply. But qq thing tells me that I help you

  • Thanks, but in the end I won’t even use, I tried to implement but very complicated, I ended up doing something else with the menu

  • @Guilhermecostamilam I understand, it turns out that these Caran solutions made on the basis of the "way", but stay there if you can help someone. Thanks for the strength that we’re in!

Show 4 more comments

3

I am sending an option with JS, hope it will be useful.

window.onscroll = function() {
  const scroll = document.body.scrollHeight;
  const alturaTotal = window.scrollY + window.innerHeight;
  const footer = document.querySelector("footer");

  footer.style["z-index"] = (alturaTotal >= scroll) ? "2" : "-1";
}
* {
  margin: 0;
  padding: 0;
  text-align: center;
}

nav {
  z-index: 2;
  width: 25%;
  height: 100vh;
  background-color: red;
  position: fixed;
}

main {
  z-index: 1;
  width: 75%;
  margin-left: 25%;
  height: 100vh;
  background-color: green;
  margin-bottom: 200px;
}

footer {
  z-index: -1;
  width: 100%;
  height: 200px;
  background-color: blue;
  position: fixed;
  bottom: 0;
}
<nav> MENU </nav>

<main> CONTEÚDO </main>

<footer> RODAPÉ </footer>

-1

Without moving too much in the structure you created and changing only the part of the footer, you can use as follows:

* {
  margin: 0;
  padding: 0;
  text-align: center;
}

nav {
  z-index: -1;
  width: 25%;
  height: 100vh;
  background-color: red;
  position: fixed;
}

main {
  z-index: 1;
  width: 75%;
  margin-left: 25%;
  height: 100vh;
  background-color: green;
  margin-bottom: 200px;
}

footer {
   position:fixed;
   left:0px;
   bottom:0px;
   height:30px;
   width:100%;
   background:#999;
}

/* IE 6 */
* html footer {
   position:absolute;
   top:expression((0-(footer.offsetHeight)+(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)+(ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop))+'px');
}
<nav> MENU </nav>
<main> CONTEÚDO </main>			
<footer> RODAPÉ </footer>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.