How to get rid of underline on <span> inside <a> tag with hover?

mpen

fiddle

HTML

<ul>
    <li><a href="#">Messages<span>1</span></a></li>
</ul>

CSS

a {
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

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

span {
    background-color: red;
    border-radius: 999px;
    color: white;
    margin-left: 2px;
    position: relative;
    top: -.5em;
    font-size: .75em;
    font-weight: bold;
    padding: 0 .3em;
}

When you mouse-over the link the underline is applied to the <span> even though I've set text-decoration: none. Is there a way to get rid of it?

Hashem Qolami

Try changing the display of <span> to inline-block as follows:

Example Here

span {
    background-color: red;
    border-radius: 999px;
    color: white;
    margin-left: 2px;
    position: relative;
    top: -.5em;
    font-size: .75em;
    font-weight: bold;
    padding: 0 .3em;
    display: inline-block; /* <-- Added declaration */
}

Explanation

According to CSS level 2 specification, text-decoration is not propagated to the contents of nested atomic inline-level elements - such as inline-blocks and inline-tables.

16.3.1 Underlining, overlining, striking, and blinking: the 'text-decoration' property

[...] Note that text decorations are not propagated to floating and absolutely positioned descendants, nor to the contents of atomic inline-level descendants such as inline blocks and inline tables.

Also the spec states (my emphasis):

Underlines, overlines, and line-throughs are applied only to text (including white space, letter spacing, and word spacing): margins, borders, and padding are skipped. User agents must not render these text decorations on content that is not text. For example, images and inline blocks must not be underlined.

Also note that text decorations would stick with the text itself, therefore:

Relatively positioning a descendant moves all text decorations affecting it along with the descendant's text; it does not affect calculation of the decoration's initial position on that line.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CSS/HTML: a tag with a span inside, span margin creating white space on hover underline

From Dev

span tag inside li with link is moving on hover

From Dev

Scrapy SgmlLinkExtractor how go get number inside span tag

From Dev

How to get rid of @SuppressWarnings tag?

From Dev

How to disable underline for <i> tag inside <u> tag?

From Dev

How to get rid of annoying hover info in folder?

From Dev

How to append a span tag inside another span tag in jquery?

From Dev

How to append a span tag inside another span tag in jquery?

From Dev

How to make a span tag wider with an alphabet inside?

From Dev

How to fit in an image inside span tag?

From Dev

How to make a span tag wider with an alphabet inside?

From Java

How to get rid of "would clobber existing tag"

From Dev

li tag when hover won't underline

From Dev

Get rid of the underline in Vim's fold name

From Dev

Get rid of the underline in Vim's fold name

From Dev

How to get rid of space between lines of multiline span?

From Dev

How to get rid of the default blue color on hover over a button?

From Dev

How to get text from span tag in BeautifulSoup

From Dev

How to get the text with in span tag using jquery?

From Dev

Get value inside span tag in second td element in row

From Dev

get all the text inside HTML span tag and clustering it using javascript

From Dev

Get all span by id inside section tag using jquery

From Dev

Get rid of double hover in plotly

From Dev

how to get the span created dynamically inside an iframe?

From Dev

how to get the span created dynamically inside an iframe?

From Dev

Disable underline of inside element of underlined anchor tag

From Dev

Disable underline of inside element of underlined anchor tag

From Dev

How to hide everything inside a p tag except span jQuery?

From Dev

How to get rid of Checkstyle error "unused @throws tag.."

Related Related

  1. 1

    CSS/HTML: a tag with a span inside, span margin creating white space on hover underline

  2. 2

    span tag inside li with link is moving on hover

  3. 3

    Scrapy SgmlLinkExtractor how go get number inside span tag

  4. 4

    How to get rid of @SuppressWarnings tag?

  5. 5

    How to disable underline for <i> tag inside <u> tag?

  6. 6

    How to get rid of annoying hover info in folder?

  7. 7

    How to append a span tag inside another span tag in jquery?

  8. 8

    How to append a span tag inside another span tag in jquery?

  9. 9

    How to make a span tag wider with an alphabet inside?

  10. 10

    How to fit in an image inside span tag?

  11. 11

    How to make a span tag wider with an alphabet inside?

  12. 12

    How to get rid of "would clobber existing tag"

  13. 13

    li tag when hover won't underline

  14. 14

    Get rid of the underline in Vim's fold name

  15. 15

    Get rid of the underline in Vim's fold name

  16. 16

    How to get rid of space between lines of multiline span?

  17. 17

    How to get rid of the default blue color on hover over a button?

  18. 18

    How to get text from span tag in BeautifulSoup

  19. 19

    How to get the text with in span tag using jquery?

  20. 20

    Get value inside span tag in second td element in row

  21. 21

    get all the text inside HTML span tag and clustering it using javascript

  22. 22

    Get all span by id inside section tag using jquery

  23. 23

    Get rid of double hover in plotly

  24. 24

    how to get the span created dynamically inside an iframe?

  25. 25

    how to get the span created dynamically inside an iframe?

  26. 26

    Disable underline of inside element of underlined anchor tag

  27. 27

    Disable underline of inside element of underlined anchor tag

  28. 28

    How to hide everything inside a p tag except span jQuery?

  29. 29

    How to get rid of Checkstyle error "unused @throws tag.."

HotTag

Archive