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 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!