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
  • 169,008
  • 28
  • 173
  • 236

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
  • 491
  • 6
  • 26
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,149
  • 1
  • 13
  • 19
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
  • 13,720
  • 10
  • 55
  • 101