Navbar Icons are separating from hyperlink a tag

Viewed 41

I am trying to create a navbar with a tags and icons, but there is for some reason a separation between them where the a tag background is not directly behind the icon. Here is an image of what I mean: https://gyazo.com/986c59e17f031ce0b94547af6bceebac Here is my current html and css for the navbar:

.nav a {
  float: right;
  display: block;
  margin-right: 10px;
  background-color: black;
  text-align: center;
  line-height: 50px;
  text-decoration: none;
  padding: 3px;
  height: 10px;
  width: 10px;
}

.nav a:hover:not(.active) {
  background-color: grey;
  border-radius: 20px;
}

.nav a:not(.active) {
  color: white;
  border-radius: 20px;
}

.active {
  background-color: yellow;
  border-radius: 20px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="nav">
  <a href="#"><i class="fa fa-fw fa-align-justify"></i></a>
  <a href="#"><i class="fa fa-fw fa-wifi"></i></a>
  <a href="#"><i class="fa fa-fw fa-user"></i></a>
  <a class="active" href="#"><i class="fa fa-fw fa-gear"></i></a>
</div>

How can I fix this to make the a tag and icon align?

3 Answers

Remove height and width properties of the a tag and set the padding on the icons

.nav a {
  float: right;
  display: block;
  margin-right: 10px;
  background-color: black;
  text-align: center;
  /* line-height: 50px; */
  text-decoration: none;
  /*
  padding: 3px;
  height: 10px;
  width: 10px;
  */
}

i {
  padding: 3px;
}

.nav a:hover:not(.active) {
  background-color: grey;
  border-radius: 20px;
}

.nav a:not(.active) {
  color: white;
  border-radius: 20px;
}

.active {
  background-color: yellow;
  border-radius: 20px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="nav">
  <a href="#"><i class="fa fa-fw fa-align-justify"></i></a>
  <a href="#"><i class="fa fa-fw fa-wifi"></i></a>
  <a href="#"><i class="fa fa-fw fa-user"></i></a>
  <a class="active" href="#"><i class="fa fa-fw fa-gear"></i></a>
</div>

You can easily achieve this layout using flexbox properties just like that:

.nav a {
  float: right;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-right: 10px;
  background-color: black;
  text-decoration: none;
  padding: 3px;
  height: 15px;
  width: 15px;
}

.nav a:hover:not(.active) {
  background-color: grey;
  border-radius: 20px;
}

.nav a:not(.active) {
  color: white;
  border-radius: 20px;
}

.active {
  background-color: yellow;
  border-radius: 20px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="nav">
  <a href="#"><i class="fa fa-fw fa-align-justify"></i></a>
  <a href="#"><i class="fa fa-fw fa-wifi"></i></a>
  <a href="#"><i class="fa fa-fw fa-user"></i></a>
  <a class="active" href="#"><i class="fa fa-fw fa-gear"></i></a>
</div>

It has the advantage of not distorting the circles.

You have to remove the line-height and size on your link <a>, like this :

.nav a {
  float: right;
  display: block;
  margin-right: 10px;
  background-color: black;
  text-align: center;
  text-decoration: none;
  padding: 3px;
}

.nav a:hover:not(.active) {
  background-color: grey;
  border-radius: 20px;
}

.nav a:not(.active) {
  color: white;
  border-radius: 20px;
}

.active {
  background-color: yellow;
  border-radius: 20px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="nav">
  <a href="#"><i class="fa fa-fw fa-align-justify"></i></a>
  <a href="#"><i class="fa fa-fw fa-wifi"></i></a>
  <a href="#"><i class="fa fa-fw fa-user"></i></a>
  <a class="active" href="#"><i class="fa fa-fw fa-gear"></i></a>
</div>

Related