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

Fixed navigation

Hi,

I'm trying to make a fixed navigation (in codepen.io) but when I try my background-image on the header jumps up and covers the navigation-background (I can still look the text).

<div class="container-fluid">
  <div class="main-nav">
    <ul class="nav">
      <li class="name">Henrik Christensen | Web Developer</li>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Portfolio</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </div>

  <div class="heading">
    <img src="http://s19.postimg.org/cyuyif6j7/profile.jpg" alt="Picture of Henrik Christensen" id="profile-photo">
    <h1 class="tag">Hello, I'm Henrik</h1>
    <p class="tag description">I'm a Full Stack Web Developer from Denmark</p>
  </div>
</div>
* {
  margin: 0;
  padding: 0;
}

body {
  font-family: Monospace;
  background: #6fc;
}

.main-nav {
  width: 100%;
  background: black;
}

.nav {
  padding: 30px;
  display: flex;
  justify-content: space-around;
  position: fixed;
  z-index: 10;
}

.nav li {
  display: inline-block;
  font-weight: bold;
}

.nav a {
  margin-left: 30px;
  color: #6fc;
  font-size: 1.2em;
  text-decoration: none;
  transition: all .5s;
}

.nav a:hover {
  color: white;
  text-decoration: underline;
}

.nav .name {
  margin-right: auto;
  font-size: 1.5em;
  color: white;
}

.heading {
  background: url("http://s19.postimg.org/4s2yquggj/Full_Size_Render.jpg") no-repeat top center;
  background-size: cover;
  min-height: 443px;
  width: 100%;
  text-align: center;
}

#profile-photo {
  width: 150px;
  margin-top: 50px;
  border: 4px solid white;
  border-radius: 50%;
  transition: all .5s;
}

#profile-photo:hover {
  transform: scale(1.2);
}

.tag {
  background: #efefef;
  display: table;
  color: black;
  padding: 10px;
  border-radius: 5px;
  margin: 20px auto;
  font-weight: bold;
}

I don't know if it is a bug from codepen because when I try use the "position: fixed" in my text-editor I never get the same problem :-/

You're applying the background color to the .main-nav class, but setting the .nav class position to fixed.

Applying the fixed position and z-index to the .main-nav class instead of .nav will sort the issue.

.main-nav {
  width: 100%;
  background: black;
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
}

.nav {
  padding: 30px;
  display: flex;
  justify-content: space-around;
}

You may also want to apply a top padding to the body or .container-fluid div to compensate for the fixed nav, otherwise .main-nav will overlap your profile photo at the top of the page.

1 Answer

Thank you very much - You just saved my day! :-D