2

I have a simple navbar. There is a header and then a container which contains all the code.

What I'm trying to do is add padding to the li item. When I do it, it just adds it to the left and right but not on top. I've been trying to fix it and I've noticed that if I put padding inside header nav instead of header nav ul li it works. Also, if I add float :left to header nav ul li.

I just want to know why do I have to add float: left to header nav ul li to make the padding work?

.container {
  width: 80%;
  margin: 0 auto;
}

#brand {
  float: left;
}

header nav {
  float: right;
}

header nav ul li {
  display: inline;
}
<header>
  <div class="container">
    <div id="brand">
      <h1>test</h1>
    </div>
    <nav>
      <ul>
        <li><a href="#">1</a></li>
        <li><a href="#">Home</a></li>
        <li><a href="#">Home</a></li>
      </ul>
    </nav>
  </div>
</header>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
ChDrY322
  • 27
  • 6
  • 1
    does display: inline-block; fix the problem? – Gert B. Jan 22 '21 at 15:59
  • It worked but i already knew how to make it work but i wanted to know why was the padding not being applied without adding inline-block. That has been answerd – ChDrY322 Jan 22 '21 at 16:10

2 Answers2

2

Try using display: inline-block

.container {
  width: 80%;
  margin: 0 auto;
  font-family: Arial;
}

#brand {
  float: left;
}

header nav {
  float: right;
}
nav a {
color: gray;
text-decoration: none;
}
header nav ul li {
  display: inline-block;
  padding: 10px;
}
<header>
  <div class="container">
    <div id="brand">
      <h1>test</h1>
    </div>
    <nav>
      <ul>
        <li><a href="#">1</a></li>
        <li><a href="#">Home</a></li>
        <li><a href="#">Home</a></li>
      </ul>
    </nav>
  </div>
</header>
Manu G
  • 158
  • 5
  • 13
1

.container {
      width: 80%;
      margin: 0 auto;
    }

    #brand {
      float: left;
    }

    header nav {
      float: right;
    }

   header nav ul li {
      display: inline-block;
      padding-top:100pt;
      background-color:red;
    }
    <header>
      <div class="container">
        <div id="brand">
          <h1>test</h1>
        </div>
        <nav>
          <ul>
            <li><a href="#">1</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
          </ul>
        </nav>
      </div>
    </header>

As you can see in the above snippet, if you use inline-block you can set vertical padding (highlighted with the use of red background-color)

Check this answer out (accepted answer) to see the difference between inline and inline-block.