0

Using CodePen, I've been trying to style a fixed navigation bar with my username on the left side and a list of inline links on the right side. I haven't figured out how to properly format it because after putting the h1 or p element with my username, the list of links aren't even within the colored div that they're held in.

My question is: how do I format my navigation bar properly so everything is inline and neatly placed without covering up the next div while in the fixed position? Also any advice or tips is appreciated.

HTML:

<body>

<div id="fixed">
  <p>Springathing</p>
  <ul id="nav">
    <li><a href="#about">About</a></li>
    <li><a href="portfolio">Portfolio</a></li>
    <li><a href="contact">Contact</a></li>
  </ul>
</div>

<div id="overall">

  <div id="about">

  </div>

  <div id="portfolio">

  </div>

  <div id="contact">

  </div>

  <div id="pageinfo">

  </div>

  <div id="copyright">

  </div>

</div>

</body>

CSS:

body {
  margin: 0;
  padding: 0;
}
#nav {
  list-style: none;
  text-align: right;
}
#fixed {
  background-color: #4F7BF7;
  height: 60px;
  width: 100%;
  position: fixed;
}
#overall {
  background-color: #5D6363;
}
#about {
  background-color: #A2A2A2;
  width: 500px;
  height: 500px;
  margin: 0 auto;
}
#portfolio {
  background-color: #B8BBBB;
  border-bottom: 1px solid gray;
  width: 500px;
  height: 500px;
  margin: 0 auto;
}
#contact {
  background-color: #B8BBBB;
  width: 500px;
  height: 500px;
  margin: 0 auto;
}
#pageinfo {
  background-color: #A2A2A2;
  width: 100%;
  height: 100px;
}
#copyright {
  background-color: #4F7BF7;
  width: 100%;
  height: 50px;
}
springathing
  • 425
  • 3
  • 8
  • 28

3 Answers3

1

You're missing three things here:

  1. Your header's <p> tag needs display: inline-block.
  2. Your navigation <ul> needs to float: right.
  3. Your navigation list item <li> tags need to have display: inline-block.

I've updated your code to include these three additions, and have created a new fiddle demonstrating what I believe you're looking for here.

Note that you also might want to add a bit of margin-right to #nav, and a bit of margin-left to #fixed p.

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • What is the reason for me not being able to apply inline-block to "ul"? Since its the parent don't the child "li"s inherit the inline style? – springathing Jun 22 '17 at 22:50
  • It depends on which element you are trying to display inline. Essentially, you want your three links to display next to each other, so each one of the links (the `
  • ` elements) needs the `display: inline-block` attribute. You can't attach it to the `
      ` instead, as `display` is [**not inherited**](https://www.w3schools.com/cssref/pr_class_display.asp). Even if it was, you would need to **explicitly** state to inherit it (overriding the default `display: list-item`), **and** set it on the parent, creating twice as much code as setting it on the target element itself ;)
  • – Obsidian Age Jun 22 '17 at 23:46