2

if I align my icons within my navbar with display: flex, it shows an underline below my icons. If I disable it, they disappear but my icons aren't align properly. See images below.

How can I get rid of them?

without display: flex <--> with display: flex

Here's the code:

.nav {
  width: 60px;
  height: 100vh;
  top: 100px;
  min-height: 100vh;
  background-color: #cccccc;
  position: fixed;
  z-index: 999;
  opacity: .4;
  border-right: 1px solid black;
  display: flex;
  flex-direction: column;
}

.nav a {
  width: 100%;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: none;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="nav">
  <a href=""><i class="material-icons">home</i></a>
  <a href=""><i class="material-icons">shopping_basket</i></a>
  <a href=""><i class="material-icons">business</i></a>
  <a href=""><i class="material-icons">mail</i></a>
  <a href=""><i class="material-icons">card_giftcard</i></a>
</div>
Asons
  • 84,923
  • 12
  • 110
  • 165
bene-we
  • 761
  • 11
  • 23

3 Answers3

5

<a> tags have text-decoration:underline by default, thats why the line is coming below.

Apply text-decoration: none to your anchor tags .nav a

Stack Snippet

.nav {
  width: 60px;
  height: 100vh;
  top: 100px;
  min-height: 100vh;
  background-color: #cccccc;
  position: fixed;
  z-index: 999;
  opacity: .4;
  border-right: 1px solid black;
  display: flex;
  flex-direction: column;
}

.nav a {
  width: 100%;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: none;
  text-decoration: none;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="nav">
  <a href=""><i class="material-icons">home</i></a>
  <a href=""><i class="material-icons">shopping_basket</i></a>
  <a href=""><i class="material-icons">business</i></a>
  <a href=""><i class="material-icons">mail</i></a>
  <a href=""><i class="material-icons">card_giftcard</i></a>
</div>
Bhuwan
  • 16,525
  • 5
  • 34
  • 57
5

The reason there is no underline prior to adding display: flex, is because the <i> is set to display: inline-block, for which the underline doesn't render.

When the display: flex is added, the display: inline-block on the <i> won't be applied anymore, since flex items are block like elements.

Adding text-decoration: none to a will fix this


Since the a has a fixed height, an alternative is to drop display: flex and use text-align/line-height to center the icon.

.nav {
  width: 60px;
  height: 100vh;
  top: 100px;
  min-height: 100vh;
  background-color: #cccccc;
  position: fixed;
  z-index: 999;
  opacity: .4;
  border-right: 1px solid black;
  display: flex;
  flex-direction: column;
}

.nav a {
  width: 100%;
  height: 60px;
  border: none;
  text-align: center;
  
}

.nav a i {
  line-height: 60px;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="nav">
  <a href=""><i class="material-icons">home</i></a>
  <a href=""><i class="material-icons">shopping_basket</i></a>
  <a href=""><i class="material-icons">business</i></a>
  <a href=""><i class="material-icons">mail</i></a>
  <a href=""><i class="material-icons">card_giftcard</i></a>
</div>
Asons
  • 84,923
  • 12
  • 110
  • 165
1

Try text-decoration: none; for your link