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 Layout Techniques Float Layout Creating a Horizontal Menu

gary peart
gary peart
6,496 Points

Can't keep my floated horizontal menu on a single line when using % margin - any ideas?

Hi,

I had a go at creating a floated horizontal menu. All is generally fine, but when I try to apply a margin-right % value on my horizontal menu I can't seem to keep the <li> elements on the same line. It is only if I apply a px value instead that all the <li> elements stay in line.

Is there anyway to resolve the issue while still applying a % value to the <li>?

CSS

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  list-style:none;
  text-decoration: none;
}

header,
footer{
  background: seagreen;
}

footer {
  height: 20px;
}

.wrapper{
  height: calc(100vh - 20px);
}

header{
  width:100%
}

.container{
  margin: 0 auto;
  width: 70%;
  max-width: 1000px;
  overflow: auto;
}

.logo{
  float: left;
  background: red;
  padding: 5px;
  margin-right: 100px;
}

.main-nav,
.main-nav li{
  float: left;
}

.main-nav li{
  background: orange;
  margin-right: 3%;
}

.main-nav{
  background: blue;
}

HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>A floated navigation</title>
  <link rel="stylesheet" href="floatnavigation.css">
</head>
<body>
  <div class="wrapper">
  <header>
    <div class="container">
    <h3 class="logo">Logo Here</h3>
    <ul class="main-nav">
      <li><a href="">Link 1</a></li>
      <li><a href="">Link 2</a></li>
      <li><a href="">Link 3</a></li>
      <li><a href="">Link 4</a></li>
      <li><a href="">Link 5</a></li>
    </ul>

    </div>
  </header>
  <main>
    <div class="container">
    <h1>title of subject here</h1>
    <p>This is a paragraph</p>
    <p>This is a paragraph</p>
    <p>This is a paragraph</p>
    <p>This is a paragraph</p>
  </main>
  </div>
  </div>
  <footer>
    <div class="container">
      <p>&copy; 2016</p>
    </div>
  </footer>
</body>
</html>

2 Answers

Kallil Belmonte
Kallil Belmonte
35,561 Points

You have two options:

1- Defining a width to your .main-nav

2- Or removing the float: left; from your .main-nav

Kallil Belmonte
Kallil Belmonte
35,561 Points

If you choose the option 1, obviously the menu items must fit at your defined width.

gary peart
gary peart
6,496 Points

Thanks Kallil!