1

I want to build a navigation in the header containing three items where the first two ones are aligned left and the third one is aligned right. Tried it by use of flexbox but there is an arror: The ul is exceeding the width of it's parent container.

header {
  width: 100%;
  background: #417690;
  margin-left: 10px;
  margin-right: 10px;
  font-size: 20px;
  height: 70px;
}

header ul {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  /* justify-content: center; */
}

header ul li {
  display: inline-block;
  padding-right: 15px;
  margin-left: 10px;
}

.filler {
  flex-grow: 1;
}
<header>
  <ul>
    <li><a href="#">Logo</a></li>
    <li><a href="#">New article</a></li>
    <li class="filler"></li>
    <li><a href="#">username</a></li>
  </ul>
</header>

How can I fix this? Tested in FF and Opera.

kukkuz
  • 41,512
  • 6
  • 59
  • 95
Sempervivum
  • 928
  • 1
  • 9
  • 21

2 Answers2

2

Reset margin to zero and add box-sizing: border-box to all elements to include the padding in the size calculations. You may also reset the padding for the ul element - see demo below:

* { /* ADDED */
  box-sizing: border-box;
}

header {
  width: 100%;
  background: #417690;
  /*margin-left: 10px;
  margin-right: 10px;*/
  font-size: 20px;
  height: 70px;
}

header ul {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  /* justify-content: center; */
  padding: 0; /* ADDED */
}

header ul li {
  display: inline-block;
  padding-right: 15px;
  margin-left: 10px;
}

.filler {
  flex-grow: 1;
}
<header>
  <ul>
    <li><a href="#">Logo</a></li>
    <li><a href="#">New article</a></li>
    <li class="filler"></li>
    <li><a href="#">username</a></li>
  </ul>
</header>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
1

Try this:

header ul {
    box-sizing: border-box;
    padding: 0;
}
vortin
  • 135
  • 6