Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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

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!