- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
<ul>
<li><a href="#">Messages<span>1</span></a></li>
</ul>
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;
}
当您将鼠标悬停在链接上时,下划线将应用于 <span>
即使我设置了 text-decoration: none
.有办法摆脱它吗?
最佳答案
尝试更改 display
的 <span>
至 inline-block
如下:
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 */
}
根据 CSS level 2 specification , text-decoration
不会传播到嵌套原子内联级元素的内容 - 例如内联 block 和内联表。
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.
还有规范说明(我的重点):
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.
另请注意,文本装饰会粘附在文本本身上,因此:
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.
关于html - 如何通过悬停摆脱 <a> 标签内 <span> 上的下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27137967/
我是一名优秀的程序员,十分优秀!