Remove underline only from anchor element child

2020-02-05 12:40发布

When an a tag contains child elements, like an i tag, it's still applying the underline to it on hover, and I'm wondering how to remove the underline from just the i tag when someone hovers over the a tag.

The CSS I'm working with:

a{
    display:block;
    text-decoration:none;
}
a i{
  color:#888;
  margin-left:5px;
}
a:hover{
    text-decoration:underline;
}
a:hover i{
    text-decoration:none !important;
}

Here is a fiddle to explain: http://jsfiddle.net/kkz66x2q/

I simply would like the underline to be GONE only on the i element when you hover over the link.

标签: css
7条回答
兄弟一词,经得起流年.
2楼-- · 2020-02-05 13:14

I got two things to add here…

using code sometimes as a child of an (regular inline) anchor, sometimes not:

  code
    font-family: Courier New, Courier, monospace
    color: green
    background: rgba( white, .2 )
    padding: 2px 4px
    text-decoration: none

  a code
    display: inline-block
    line-height: normal
    text-decoration: underline
  1. You should add line-height: normal to kind of compensate for the inline-block, otherwise you will different paddings on linked elements than on others. (i.e. your line-height will easily start looking inconsistent)

  2. And optionally: If you were just annoyed, that the underline has a different color (i.e. the outer link color, rather than the green of code) settings text-decoration once again on that inner element helps…

enter image description here

查看更多
甜甜的少女心
3楼-- · 2020-02-05 13:20

Try following css,

a:hover i{
    display: inline-block; <-- this is the trick
    text-decoration:none !important;
}

Demo

查看更多
甜甜的少女心
4楼-- · 2020-02-05 13:21

I had the same issue and fixed this using the following css rule:

a:-webkit-any-link {text-decoration:none}

hope it helps!

查看更多
地球回转人心会变
5楼-- · 2020-02-05 13:21

Simply add display:inline-block;

FIDDLE DEMO

a:hover i {
    display:inline-block;
    text-decoration:none !important;
}
查看更多
甜甜的少女心
6楼-- · 2020-02-05 13:25

You can also write you HTML like

    <a href="#">Link</a><i>(2)</i>

and CSS like

  a{
    display:inline-block;
    text-decoration:none;
  }
查看更多
家丑人穷心不美
7楼-- · 2020-02-05 13:36

Set the display property of i to inline-block:

a i {
    ...
    display: inline-block;
}

JSFiddle

查看更多
登录 后发表回答