-5

#nav_link {
  background: orange;
  display: flex;
  justify-content: center;
  outline: 2px solid white;
  margin-top: 30px;
  width: 12%;
  height: 15rem;
}

#nav_link li {
  display: table;
  text-align: center;
  font-size: 20px;
}
<nav>
  <div id="nav_link">
    <ul>
      <li><a href="home.html">Home</a></li>
      <li><a href="">About</a></li>
      <li><a href="">Contact Us</a></li>
      <li><a href="">Services</a></li>
      <li><a href="">Pricing</a></li>
    </ul>
  </div>
</nav>

How to anchor tag inside a li in ul tag in CSS i centered ul in div tag but how to vertically center li in ul tag.

Sfili_81
  • 2,377
  • 8
  • 27
  • 36

3 Answers3

1

The padding of ul is messing with the design Include the nav_link ul

#nav_link {
  background: orange;
  display: flex;
  align-items: center;
  justify-content: center;
  outline: 2px solid white;
  margin-top: 30px;
  width: 20%;
  height: 15rem;
}
#nav_link ul {
  padding:0;
}

#nav_link li {
  display: table;
  text-align: center;
  font-size: 20px;
}
<nav>
  <div id="nav_link">
    <ul>
      <li><a href="home.html">Home</a></li>
      <li><a href="">About</a></li>
      <li><a href="">Contact Us</a></li>
      <li><a href="">Services</a></li>
      <li><a href="">Pricing</a></li>
    </ul>
  </div>
</nav>
1

To vertically center the navbar links within the tag, you can use CSS flexbox along with the align-items: center property. Here's an updated version of your CSS code:

<!DOCTYPE html>
<html>
<head>
<style>
#nav_link {
  background: orange;
  display: flex;
  justify-content: center;
  align-items: center; /* Added property */
  outline: 2px solid white;
  margin-top: 30px;
  width: 37%;
  height: 15rem;
}

#nav_link li {
  display: table;
  text-align: center;
  font-size: 20px;
}

</style>

</head>
<body>
<nav>
  <div id="nav_link">
    <ul>
      <li><a href="home.html">Home</a></li>
      <li><a href="">About</a></li>
      <li><a href="">Contact Us</a></li>
      <li><a href="">Services</a></li>
      <li><a href="">Pricing</a></li>
    </ul>
  </div>
</nav>

</body>
</html>
betrice mpalanzi
  • 1,436
  • 12
  • 16
-1

#nav_link { background: orange; display: flex; justify-content: center; outline: 2px solid white; margin-top: 30px; width: 50%; height: 15rem; align-items: center; } #nav_link ul{ padding: 0px; text-align: center; } #nav_link li {
list-style: none; text-align: center; font-size: 20px; }
<nav>
  <div id="nav_link">
    <ul>
      <li><a href="home.html">Home</a></li>
      <li><a href="">About</a></li>
      <li><a href="">Contact Us</a></li>
      <li><a href="">Services</a></li>
      <li><a href="">Pricing</a></li>
    </ul>
  </div>
</nav>
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 08 '23 at 15:33