0

What exactly is needed for margin: 0 auto to work? It usually works for me but for some reason in this particular case I can't center my navigation menu. Fiddle attached below.

.item-list {
  display: flex;
  margin: 0 auto;
}

.item {
  margin-left: 10px;
  list-style: none;
}

.item a {
  color: #37302b;
  font-size: 20px;
}

header {
  position: inherit;
  height: 50px;
}
<header>
  <nav class="desktop-nav">
    <ul class="item-list">
      <li class="item active">
        <a href="http://localhost:8888/gross-daily/main.html" data-scroll>Home</a>
      </li>
      <li class="item">
        <a href="http://localhost:8888/gross-daily/main.html" data-scroll>Newest</a>
      </li>
      <li class="item">
        <a href="#projects" data-scroll>Most Popular</a>
      </li>
      <li class="item">
        <a href="#projects" data-scroll>Categories</a>
      </li>
      <li class="item">
        <a href="#blog" data-scroll>Stash</a>
      </li>
      <li class="item">
        <a href="#">Contact</a>
      </li>
      <li class="item" onclick="showSearch();">
        <a href="#">Search</a>
      </li>
    </ul>
  </nav>
</header>

https://jsfiddle.net/wwf1at2n/1/

j08691
  • 204,283
  • 31
  • 260
  • 272
Emily
  • 43
  • 5

3 Answers3

3

Since you're using flexbox, you can add justify-content: center to your .item-list rules (no margin: auto needed):

.item-list {
  display: flex;
  justify-content: center;
}

.item {
  margin-left: 10px;
  list-style: none;
}

.item a {
  color: #37302b;
  font-size: 20px;
}

header {
  position: inherit;
  height: 50px;
}
<header>
  <nav class="desktop-nav">
    <ul class="item-list">
      <li class="item active">
        <a href="http://localhost:8888/gross-daily/main.html" data-scroll>Home</a>
      </li>
      <li class="item">
        <a href="http://localhost:8888/gross-daily/main.html" data-scroll>Newest</a>
      </li>
      <li class="item">
        <a href="#projects" data-scroll>Most Popular</a>
      </li>
      <li class="item">
        <a href="#projects" data-scroll>Categories</a>
      </li>
      <li class="item">
        <a href="#blog" data-scroll>Stash</a>
      </li>
      <li class="item">
        <a href="#">Contact</a>
      </li>
      <li class="item" onclick="showSearch();">
        <a href="#">Search</a>
      </li>
    </ul>
  </nav>
</header>
j08691
  • 204,283
  • 31
  • 260
  • 272
1

add justify-content: center; because you're using display: flex

Adam
  • 1,385
  • 1
  • 10
  • 23
-1

Block level element is needed for margin: 0 auto; to work in a normal scenario.

StefanBob
  • 4,857
  • 2
  • 32
  • 38
  • a width is need for margin auto to work because the defautl width is 100% and margin:auto will have no effect – Temani Afif Apr 10 '18 at 19:55
  • You need a block level element for margin: 0 auto to take effect. It still works on width: 100% elements, there is just nothing to do. – StefanBob Apr 10 '18 at 20:04