0

so my issue is that my navbar wont display in the center of the screen (horizontally) and I dont understand why, this is something I have regular issues with so if you could help it would be greatly appreciated. Heres a link to the code

body {
  width: 100%;
  margin: 0;
  padding: 0;
}

/*******************
HEADER
*******************/
#logo {
  display: block;
  margin: 0 auto;
  height: 14em;
}

#name {
  text-align: center;
}

/*******************
NAV BAR
*******************/

nav ul {
  list-style-type: none;
  overflow: hidden;
  text-align: center;
}

nav li {
  float: left;
  display: inline-block;
}

nav li a {
  display: block;
  text-align: center;
  text-decoration: none;
}
  <body>
    <header>
      <img id="logo" src="img/under-construction.png" />
      <h1 id="name">Team Kangoo Anywhere</h1>
      <nav>
        <ul>
          <li><a href="home.html"></a>Home</li>
          <li><a href="about-us.html"></a>About Us</li>
          <li><a href="about-the-rally.html"></a>About The Rally</li>
          <li><a href="our-car.html"></a>Our Car</li>
          <li><a href="charities.html"></a>Charities</li>
          <li><a href="sponsors.html"></a>Sponsors</li>
          <li><a href="contact-us.html"></a>Contact Us</li>
        </ul>
      </nav>
    </header>
james
  • 101
  • 1
  • 8

4 Answers4

1

Ideally you should have some header css to center it's contents, then you could align the nav li s any which way you want. I created a fiddle (same as snippet) to demonstrate, and added padding to the li elements (or else they'd have been all squished together)

Hope this helps.

body {
  width: 100%;
  margin: 0;
  padding: 0;
}


/*******************
HEADER
*******************/

header {
  display: block;
  margin: 0 auto max-height: 20em;
}

#logo {
  display: block;
  margin:auto;
  height: 14em;
}

#name {
  text-align: center;
}


/*******************
NAV BAR
*******************/
/*nav{text-align:center;}*/
nav ul {
  list-style-type: none;
  overflow: hidden;
  text-align: center;
}

nav li {
  float: left;
  display: inline-block;
  padding-right: 7px;
}

nav li a {
  display: block;
  text-align: center;
  text-decoration: none;
}
<header>
  <img id="logo" src="img/under-construction.png" />
  <h1 id="name">Team Kangoo Anywhere</h1>
  <nav>
    <ul>
      <li>
        <a href="home.html"></a>Home</li>
      <li>
        <a href="about-us.html"></a>About Us</li>
      <li>
        <a href="about-the-rally.html"></a>About The Rally</li>
      <li>
        <a href="our-car.html"></a>Our Car</li>
      <li>
        <a href="charities.html"></a>Charities</li>
      <li>
        <a href="sponsors.html"></a>Sponsors</li>
      <li>
        <a href="contact-us.html"></a>Contact Us</li>
    </ul>
  </nav>
</header>
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
0

Change

nav li {
  float: left;
  display: inline-block;
}

to

nav li {
  // Remove float
  display: inline-block;
}
thisiskelvin
  • 4,136
  • 1
  • 10
  • 17
0

Add a wrapper to the nav using a div tag, make the nav display inline and use text-align on the div.

<div style="text-align:center"><nav style="display:inline-block">

... and then google Bootstrap

Kirk Powell
  • 908
  • 9
  • 14
  • If you look at their existing code, none of this is necessary. They already have `text-align: center` on the nav elements' parent, and `display: inline-block` on the nav elements themselves. They floated the nav elements `left` so removing that `float` is all that needs to change and their nav is centered. – Michael Coker Mar 21 '17 at 21:16
  • I assumed his fix needed to preserve the ```float:left``` for the ```nav li``` – Kirk Powell Mar 21 '17 at 21:23
  • 1
    ah gotcha, roundabout but it works! But they thought the only way to make li's display side by side was to float them. They didn't realize `inline-block` does that, too, and without the `float`, `inline-block` allows `text-align` on the parent to center them. – Michael Coker Mar 21 '17 at 21:27
0

After removing the float: left you can use display: flex for <ul> or display: inline for it's children <li>s.

And you have an unwanted left padding in the <ul> that it is better to remove it to make real center.

ul { margin: 0; padding: 0; }

You can have a look at this post.

Community
  • 1
  • 1
Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23