1

I just started doing a navbar and there is a space before first item and I can't get delete it. I tried with margin 0 and padding 0 on .navbar a, didn't work

I'm sure I'm missing something :D

.navbar {
  list-style-type: none;
  background-color: black;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin: 0;
  height: 50px;
}

.navbar a {
  text-decoration: none;
  color: gray;
  padding: 17px;
  transition: background-color 0.5s linear;
}

.navbar a:hover {
  background-color: green;
  color: honeydew;
}

body {
  margin: 0;
}
<header>
  <nav>
    <ul class='navbar'>
      <li><a href='#home'>Home</a></li>
      <li><a href='#download'>Download</a></li>
      <li><a href='#register'>Register</a></li>
      <li><a href='#contact'>Contact</a></li>
      <li><a href='#FAQ'>FAQ</a></li>
    </ul>
  </nav>
</header>

mplungjan
  • 155,085
  • 27
  • 166
  • 222

3 Answers3

3

Add padding: 0; to .navbar in your style.

The space is created by the ul element, not the lielements.

.navbar {
  list-style-type: none;
  background-color: black;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin: 0;
  height: 50px;
  padding: 0; /* <--- here */
}
Tony S
  • 419
  • 3
  • 21
2

It's tag ul has padding, in your case add padding: 0; to your .navbar

.navbar {
  list-style-type: none;
  background-color: black;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin: 0;
  height: 50px;
  padding: 0;
}

.navbar a {
  text-decoration: none;
  color: gray;
  padding: 17px;
  transition: background-color 0.5s linear;
}

.navbar a:hover {
  background-color: green;
  color: honeydew;
}

body {
  margin: 0;
}
<header>
  <nav>
    <ul class='navbar'>
      <li><a href='#home'>Home</a></li>
      <li><a href='#download'>Download</a></li>
      <li><a href='#register'>Register</a></li>
      <li><a href='#contact'>Contact</a></li>
      <li><a href='#FAQ'>FAQ</a></li>
    </ul>
  </nav>
</header>
Aleksandr Belugin
  • 2,049
  • 1
  • 14
  • 18
2

ul element in html have naturally a padding-left css rule set to 40px. Since your .navbar is actually a ul element you will have to remove it:

.navbar {
    list-style-type: none;
    background-color: black;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin: 0;
    height: 50px;
    padding-left: 0;    /* <-- Reset padding-left from ul */
}

.navbar a {
    text-decoration: none;
    color: gray;
    padding: 17px;
    transition: background-color 0.5s linear;
}

.navbar a:hover {
    background-color: green;
    color: honeydew;
}

body {
    margin: 0;
}
<header>
    <nav>
        <ul class='navbar'>
            <li><a href='#home'>Home</a></li>
            <li><a href='#download'>Download</a></li>
            <li><a href='#register'>Register</a></li>
            <li><a href='#contact'>Contact</a></li>
            <li><a href='#FAQ'>FAQ</a></li>
        </ul>
    </nav>
</header>
johannchopin
  • 11,813
  • 8
  • 41
  • 91