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

Is the top offset for fixed divs required in the CSS rule?

When Guil set up the CSS for the fixed div he added a top offset=0. Isn't that the default? Can it be omitted

Thanks, Jason

Hey Jason,

Can you post all of the code in question? If you haven't posted code here before, check out either the Markdown Cheatsheet or this helpful link to format your code: How To Post Code

Hi Marcus -- thanks for the response! Here's the code I'm looking at from the Box Model section of CSS Foundations:

<!-- <div class="fixed">fixed</div>

So, he simply creates a div, then he creates the CSS rule below to fix the div at the top of the viewport:

        .fixed {
    position: fixed;
    top: 0;
    width: 100%;
    height: 60px;
    background-color: #000;
}

I thought the default behavior of the fixed position started at the viewport's "origin" point for lack of a better word, so I don't understand why Guil added "top: 0;" to his CSS rule. Indeed, when I rem out that property, the div drops down about 60px below the top of the viewport.

I went back, found the course and skimmed through the video and found where you are talking about. When you originally use position: fixed, it is positioning the element using the containing element as its viewport. So, if there are no defined contained elements, it will use the body as the containing element, but it must still have specified position values in order to go where you desire. That's why Guil used the "top: 0;" property to position the .fixed div to the top of the screen.

For more information, check out this MDN article: Positioning#Fixed

Hi Marcus -- I don't totally understand what you mean (I'm a newb) when you say that position:fixed uses the containing element as it's viewport. Again, the way I understand it is that it uses the browser viewport as the default. I stole this from an article out on the web: "the viewport (browser/device window) positions the fixed element, as opposed to any parent element. " So, do you mean that it's using the body element as it's viewport? I'm not sure what defines an element as a containing element, unless you mean that it's the parent element of the child with the position:fixed property.

Sorry to be a pain about things! Thanks again.

2 Answers

Well, the reason why Guil had to use "top: 0;" is because browsers have default styling applied to pages which means that there are margins in play so that content has some separation from the top of the screen and doesn't look squished.

You can see what I'm talking about if you just do a simple HTML page with a paragraph on it. You'll notice it sits a little ways away from the top of the screen, and you didn't do anything to cause that.

So, when we want a fixed box, like Guil did, we have to use "top: 0;" in order to push the element all the way to the top and override those default browser styling. This will push past the margins set by the browser because author styles, aka your styles on the page/in external CSS file, always override the browser's default styling.

I hope that makes more sense! :)

Ah! Got it, I never took that into consideration. So there's something like margin-top: 60px going on for the browser default, so unless you force that back up, that's where the relative position is going to start.

Totally clear, and thanks so much for the help.

I'm glad it's making more sense to you, Jason! Always a pleasure to help out a fellow student. Good luck!