About fixed footer position only at bottom of page

6

I have a layout with a fixed menu on the left, the content of the page 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>

The fixed footer is for the effect of the text being stopped when scrolling

Is it possible (preferably with CSS only) 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 must have z-index greater than the content due to the shadows

    
asked by anonymous 28.09.2018 / 18:35

3 answers

4

This option has the HTML structure a bit different from yours. And background is actually in main , done with linear-gradiente to "split" the layout into two columns. If the content of main does not occupy the entire screen, footer is displayed. But if the content is extensive the footer is only revealed when the content ends. For this I used heigth of main this way min-height: calc(100vh - 200px);

Here in Snippet does not look so cool because footer has just the height of the snippet , but will hardly have a situation that will buggar in> so, only if your menu is too long and the screen height too narrow.

To better understand the example: Display Full Screen to have 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 layout. It only depends if your menu has only one background color ...

The trick here is to undo%% of your background color ... So, menu will remain fixed, but with a transparent background! The background is actually another menu , this div in turn works like div and rises in scroll . Thus main is always fixed, but the "whole fund" is free to raise revealing menu .

But if the footer of the page is too large, the background will disappear behind the scroll . Then I indicate the second option that is below ... But you have some caveats that I will tell below.

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

Here's how:

    
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 the layout

With CSS depending on your layout design you can create a pseudo element that will be attached in menu and go up % of% when you scroll . But the blue that you see covers Main is not Menu , this element is menu footer rising, giving the impression that it is after appearing :)

So I made the remark saying that 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>
    
28.09.2018 / 19:32
3

I'm sending an option with JS, I hope it's 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>
    
28.09.2018 / 19:10
-1

Without messing around with the structure you created and changing only the footer part, you can use it 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>
    
28.09.2018 / 19:46