-2

I'm trying to achieve a fixed sidebar where the footer should not interfere with the fixed sidebar.

<body>
    <div>
        <div class="sidebar"> <!-- Fixed sidebar -->
        </div>
        <div class="content">
            <!-- Long article -->
        </div>
    </div>
    <div class="footer">
    </div>
</body>
Saral
  • 1,087
  • 1
  • 8
  • 18

2 Answers2

1

If you inspect Google's code for this page, you'll see that the element is set to position: fixed once you've scrolled a certain amount as well as a top value set to the header's height.

It's max-height is lowered as the window's height lowers.

If you're satisfied with its browser support you could try position: sticky although I would advise not too as it's still only truly compatible with Firefox & Safari's latest versions.

You'll have to set your sidebar to position: fixed and adjust its position values using JavaScript on scroll. Or set it to fixed from the get-go and use z-index on your footer to cover it.

Jake
  • 1,398
  • 1
  • 9
  • 19
1

You can use this code to fixate elements to viewport.

.sidebar {
  position: fixed;
  top: 100px; /* below the header */
  height: calc(100% - 200px); /* substract the height of the header and footer */
}

You should have a look at this answer for the CSS3 calc-function: https://stackoverflow.com/a/14101451

Screenload
  • 431
  • 2
  • 14