Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep underline on anchor text but not on font awesome icon in anchor

I have the following markup:

<a href="#" title="Feedback" id="feedbacktogglenav">
  Feedback
  <i class="icon-comment"></i>
</a>

On hover, I would like the text to be underlined but not the font-awesome icon. I know you can do "text-decoration:none" on the icon (which works) but when you hover on the text part it still adds the underline to the icon. Any ideas?

http://jsfiddle.net/ZZEQd/

like image 757
Anthony Avatar asked Oct 11 '13 20:10

Anthony


People also ask

How do I hide the underline in an anchor tag?

By setting the text-decoration to none to remove the underline from anchor tag. Syntax: text-decoration: none; Example 1: This example sets the text-decoration property to none.

How do you keep underlined in CSS?

There are two primary ways to underline text, the U tag and the text-decoration CSS property. The U element can apply semantic meaning to the underlined content the CSS technique may not convey to automated tools. The CSS text-decoration approach is a little more flexible and is the approach I recommend.

What text-decoration value will remove the underline from a link?

To remove underline from a link in HTML, use the CSS property text-decoration. Use it with the style attribute. The style attribute specifies an inline style for an element. Use the style attribute with the CSS property text-decoration to remove underline from a link in HTML.

How do I change the position of an underline in HTML?

In the horizontal text we use text-underline-position: under; to put the underline below all the descenders. In the text with a vertical writing-mode set, we can then use values of left or right to make the underline appear on the left or right of the text as required.


5 Answers

I've discovered a way of doing this without needing an extra span tag, it works in every browser I've tried it in (FF/Chrome/Safari/Opera)... except IE8 (I haven't tested in IE 9 or 10 either).

Just declare the icon as display:inline-block, no more underline on hover.

Example: http://jsfiddle.net/J432G/

HTML:

<a href="#" title="Feedback" id="feedbacktogglenav">
  Feedback<i class="icon-comment"></i>
</a>

CSS:

a{
  text-decoration: none;
}
a:hover{
  text-decoration: underline;
}
i:after{
  content: '{icon}';
  display: inline-block;
  padding-left: 1em;
}
like image 197
Brian Avatar answered Oct 03 '22 07:10

Brian


...but when you hover on the text part it still adds the underline to the icon. Any ideas?

To get this to work you'd need to contain the link text within a separate element (a span, ideally):

<a href="#">
    <span>Feedback</span>
    <i class="icon-comment"></i>
</a>

Now you can remove the text-decoration from the link completely and assign it only to the span (on hover):

a {
    text-decoration:none;
}

a:hover span {
    text-decoration:underline;
}

JSFiddle demo.

like image 30
James Donnelly Avatar answered Oct 03 '22 05:10

James Donnelly


The only way to do this reliably is to assign text-decoration:none to the first parent element i.e the initial a tag.

like image 36
Luke Avatar answered Oct 03 '22 05:10

Luke


If you cannot add a span element (let's assume you just have acccess to CSS), it should work withfloat:left or right on your icon element.

See: CSS linked images are being underlined ("a" display is set to block)

like image 25
Nicolas Hefti Avatar answered Oct 03 '22 06:10

Nicolas Hefti


Someone just responded but deleted their response. The solution was:

#utilitynav a i {text-decoration:none;}

Thank you mystery person!

like image 36
Anthony Avatar answered Oct 03 '22 07:10

Anthony