CSS CSS Layout Basics Getting Started with CSS Layout Creating a Sticky Footer

Alexander D
Alexander D
8,522 Points

Sticky/Fixed header

Hello community!

I am trying to create a Sticky header (that stays on top when you scroll down) using the "position: fixed" method. When I declare this style, the header suddenly covers part of the body. After researching online, it seems that it's an expected behavior, however i am not quite sure how to fix this.

Don't mind the footer, it's just an experiment following some Youtube videos.

The header works well like this (optimized for IE and other browsers). It follows the directions of the page and stays always on top. I used "position:sticky":

.main-header {
position:-webkit-sticky;
position:sticky;
top: 0;
width: 100%;
margin-bottom: 50px;
}

When I change it to the following, it breaks. The header starts covering part of the body:

.main-header {
position: fixed;
top: 0;
width: 100%;
margin-bottom: 50px;
}

Why not using just "Sticky" method in the first place? What is the difference?

For your convenience, the CodePen is the following:

https://codepen.io/DeMichieli/pen/mYbZqd

Thanks, Alex

1 Answer

Alexander D
Alexander D
8,522 Points

As always I am jumping ahead! The solution is posted here:

https://teamtreehouse.com/library/fixed-positioning

Although it would be great to have some help understanding the difference between Fixed and Sticky if applied to a header, or a footer

Thanks!