Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS CSS Foundations The Box Model Positioning Properties

Cheryl Hurley
Cheryl Hurley
2,250 Points

Fixed nav bar assuming margin?

I was messing around with some of the lesson files and adding a fixed horizontal nav bar at the top. For some reason, there is about 22px worth of margin added at the top of the "nav bar" but I have no idea why... I thought that fixed positioning is relative to the viewport?

Here's my CodePen: http://codepen.io/anon/pen/Kydsc

Thanks!

2 Answers

Keith Kelly
Keith Kelly
21,326 Points

That is due to a browser default that puts a top and bottom margin on all ul items. If you use a css reset it would be a cross browser efficient way to correct the issue moving forward. If you would only like it for the fixed nav then set margin to 0 on the nav class.

.nav {
  background: #fff;
  color: #000;
  margin: 0;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 30;
}
Cheryl Hurley
Cheryl Hurley
2,250 Points

Makes total sense. Thanks!