htmlcssfont-awesomefont-awesome-4

Matching FontAwesome background in icon bar


I'm trying to replicate this W3 icon bar tutorial for my website, and it uses FontAwesome icons.

HTML:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- menu -->
<div class="icon-bar">
  <a href="#" class="active"><i class="fa fa-home"></i><br>Início</a>
  <a href="#"><i class="fa fa-user"></i><br>Sobre</a>
  <a href="#"><i class="fa fa-list"></i><br>Serviços</a>
  <a href="#"><i class="fa fa-money"></i><br>Preços</a>
</div>

CSS:

* {
    color: white;
    text-align: center;
    font-family: helvetica;
    background-color: rgb(50, 50, 50);
}

/* menu */
.icon-bar {
  width: 100%; /* Full-width */
  background-color: #555; /* Dark-grey background */
  overflow: auto; /* Overflow due to float */
}

.icon-bar a {
  float: left; /* Float links side by side */
  width: 25%; /* Equal width (4 icons with 25% width each = 100%) */
  padding: 12px 0; /* Some top and bottom padding */
  transition: all 0.3s ease; /* Add transition for hover effects */
  color: white; /* White text color */
  font-size: 20px; /* Increased font size */
  text-decoration: none;
}

.icon-bar a:hover {
  background-color: #000; /* Add a hover color */
}

.active {
  background-color: #04AA6D; /* Add an active/current color */
}
/* end menu */

But I'm struggling with dark backgrounds in each icon, as per this fiddle. It seems the icon goes through the icon bar and matches the body background.

Is there a way to match the green background for the icon itself?

I noticed that it displays nicely when i remove the body background-color in CSS but I wanted to keep it.


Solution

  • You have two options:

    set the background of the icon i to transparent

    .icon-bar i {
        background: transparent;
    }
    

    Snippet Below

    * {
      color: white;
      text-align: center;
      font-family: helvetica;
      background-color: rgb(50, 50, 50);
    }
    
    /* menu */
    .icon-bar {
      width: 100%; /* Full-width */
      background-color: #555; /* Dark-grey background */
      overflow: auto; /* Overflow due to float */
    }
    
    .icon-bar a {
      float: left; /* Float links side by side */
      width: 25%; /* Equal width (5 icons with 20% width each = 100%) */
      padding: 12px 0; /* Some top and bottom padding */
      transition: all 0.3s ease; /* Add transition for hover effects */
      color: white; /* White text color */
      font-size: 20px; /* Increased font size */
      text-decoration: none;
    }
    
    .icon-bar a:hover  {
      background-color: #000; /* Add a hover color */
    }
    
    .active {
      background-color: #04AA6D; /* Add an active/current color */
    }
    
    .icon-bar i {
     background: transparent
    }
    /* fim menu */
    <!-- Add icon library -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    
    <!-- menu -->
    <div class="icon-bar">
      <a href="#" class="active"><i class="fa fa-home"></i><br>Início</a>
      <a href="#"><i class="fa fa-user"></i><br>Sobre</a>
      <a href="#"><i class="fa fa-list"></i><br>Serviços</a>
      <a href="#"><i class="fa fa-money"></i><br>Preços</a>
    </div>

    set the properties to i

    here

    .active, .active i {
      background-color: #04AA6D; /* Add an active/current color */
    }
    

    and here

    .icon-bar a:hover, .icon-bar a:hover i  {
      background-color: #000; /* Add a hover color */
    }
    

    Snippet below

    * {
      color: white;
      text-align: center;
      font-family: helvetica;
      background-color: rgb(50, 50, 50);
    }
    
    /* menu */
    .icon-bar {
      width: 100%; /* Full-width */
      background-color: #555; /* Dark-grey background */
      overflow: auto; /* Overflow due to float */
    }
    
    .icon-bar a {
      float: left; /* Float links side by side */
      width: 25%; /* Equal width (5 icons with 20% width each = 100%) */
      padding: 12px 0; /* Some top and bottom padding */
      transition: all 0.3s ease; /* Add transition for hover effects */
      color: white; /* White text color */
      font-size: 20px; /* Increased font size */
      text-decoration: none;
    }
    
    .icon-bar a:hover, .icon-bar a:hover i  {
      background-color: #000; /* Add a hover color */
    }
    
    .active, .active i {
      background-color: #04AA6D; /* Add an active/current color */
    }
    /* fim menu */
    <!-- Add icon library -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    
    <!-- menu -->
    <div class="icon-bar">
      <a href="#" class="active"><i class="fa fa-home"></i><br>Início</a>
      <a href="#"><i class="fa fa-user"></i><br>Sobre</a>
      <a href="#"><i class="fa fa-list"></i><br>Serviços</a>
      <a href="#"><i class="fa fa-money"></i><br>Preços</a>
    </div>