htmlcssfont-awesometext-decorations

Font Awesome icons have text-decoration issues inside links


I have a "ugly" issue with font-awesome, when I place a icon-link (icon inside a link) in front of a text. By hovering the icon, the icon itself will not get underlined but somehow the space between the text and the icon. Somehow the text-decoration css rule from the link (underline while hover) collides with the one coming from the icon in this strangely appearing space.

How can I get rid of this underline in the space and have no decoration at all in the end? (when possible without adding a class to the link element nor using JS)

Here is a code snippet that may help you.

a {
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>
<h1>
  <a href="#">
    <i class="fa fa-wrench"></i>
  </a>
  Text of Title
</h1>

Fiddle: https://jsfiddle.net/kg6zdxu5/


Solution

  • Apparently your <a> tag and your <i> tag will not render a space if you write them in a single line. Avoiding line break between these two elements fixes your issue.


    Code Snippet:

    a {
      text-decoration: none;
    }
    a:hover {
      text-decoration: underline;
    }
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
    <h1>
      <a href="#"><i class="fa fa-wrench"></i></a>
      Text of Title
    </h1>

    EDIT:

    Usually it is better if you do not change the default display value of an element, but here you can use display: inline-block; in your <a> tag to remove that space.

    a {
      text-decoration: none;
    }
    a:hover {
      text-decoration: underline;
    }
    h1 > a {
      display: inline-block;
    }
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
    <h1>
      <a href="#">
        <i class="fa fa-wrench"></i>
      </a>
      Text of Title
    </h1>


    Not necessarily question-related but I stopped using icon fonts a while back and adopted SVG icons, which, in my opinion, are way better.

    Here's a good article on making the switch, and here's another on how to use them.

    DEMO:

    a {
      text-decoration: none;
    }
    a:hover {
      text-decoration: underline;
    }
    h1 > a {
      display: inline-block;
      color: purple;
    }
    .icon {
      display: inline-block;
      width: 1em;
      height: 1em;
      stroke-width: 0;
      stroke: currentColor;
      fill: currentColor;
    }
    .icon-wrench {
      width: 0.939453125em;
    }
    <h1>
      <a href="#">
       <svg class="icon icon-wrench">
       <use xlink:href="#icon-wrench"></use>
       </svg>
      </a>
      Text of Title
    </h1>
    <svg style="display:none;">
      <symbol id="icon-wrench" viewBox="0 0 30 32">
        <title>wrench</title>
        <path class="path1" d="M6.857 26.286q0-0.464-0.339-0.804t-0.804-0.339-0.804 0.339-0.339 0.804 0.339 0.804 0.804 0.339 0.804-0.339 0.339-0.804zM18.357 18.786l-12.179 12.179q-0.661 0.661-1.607 0.661-0.929 0-1.625-0.661l-1.893-1.929q-0.679-0.643-0.679-1.607 0-0.946 0.679-1.625l12.161-12.161q0.696 1.75 2.045 3.098t3.098 2.045zM29.679 11.018q0 0.696-0.411 1.893-0.839 2.393-2.938 3.884t-4.616 1.491q-3.304 0-5.652-2.348t-2.348-5.652 2.348-5.652 5.652-2.348q1.036 0 2.17 0.295t1.92 0.83q0.286 0.196 0.286 0.5t-0.286 0.5l-5.232 3.018v4l3.446 1.911q0.089-0.054 1.411-0.866t2.42-1.446 1.259-0.634q0.268 0 0.42 0.179t0.152 0.446z"></path>
      </symbol>
    </svg>