As title, I'm adding icons using .icon-*
. When adding an icon to an hyperlink:
<a href="#" class="icon-email icon-large">Email me!</a>
The content inserted by content
property shows the underline text-decoration on hover. I'd like to disable the text-decoration
only for the content before:
[class^="icon-"]:before, [class*=" icon-"]:before { font-family: 'IcoMoon'; font-style: normal; speak: none; } .icon-mail:before { content: "\37"; } [class^="icon-large-"]:before, [class*=" icon-large"]:before { font-size: 48px; line-height: 48px; } a[class^="icon-"]:before, a[class*=" icon-"]:before { margin-right: 5px; vertical-align: middle; }
I've tried this but it's not working (decoration is still visible):
a[class^="icon-"]:hover:before, a[class*=" icon-"]:hover:before { text-decoration: none; color: white; }
To remove the underline from a hyperlink, you need to use the CSS text-decoration property. Specifically, you need to use: text-decoration: none; . This tells the browser not to decorate the text (i.e. not to display an underline).
The text-decoration-skip CSS property sets what parts of an element's content any text decoration affecting the element must skip over. It controls all text decoration lines drawn by the element and also any text decoration lines drawn by its ancestors.
The text-decoration-style property is used to set the style of the decoration line.
Insert display:inline-block; in your css. Something like the one below:
.icon-mail:before { content: "\37"; display:inline-block; text-decoration:none; }
Here is the JS FIDDLE:
http://jsfiddle.net/73p2k/18/
As the :before
pseudo-element is rendered as a descendant box (more specifically, just before the first child content box) of its generating element, it obeys the same rules its normal descendant boxes do with respect to text-decoration
:
The 'text-decoration' property on descendant elements cannot have any effect on the decoration of the ancestor.
See these answers for more details:
There isn't any good way around this... the only alternatives that come immediately to mind are:
Wrap the text in its own span
element, then apply text-decoration
to that span
, as shown by skip405. The disadvantage is, of course, extra markup.
Use an inline block background image instead of inline text in an icon font with your :before
pseudo-element (I've also corrected the inconsistencies with your class selectors):
[class^="icon-"]:before, [class*=" icon-"]:before { display: inline-block; width: 1em; height: 1em; background-size: contain; content: ""; } .icon-email:before { background-image: url(icon-mail.svg); background-repeat: no-repeat; } .icon-large:before { width: 48px; height: 48px; } a[class^="icon-"]:before, a[class*=" icon-"]:before { margin-right: 5px; vertical-align: middle; }
The advantage this has over skip405's solution is that you don't have to modify the HTML, but given that it uses SVG vector background images and background-size
, it won't work in IE8.
If you do need IE8 support, then you have to fall back to bitmap images:
.icon-email:before { background-image: url(icon-mail.png); } .icon-email.icon-large:before { background-image: url(icon-mail-large.png); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With