html - 从 <A> 元素 anchor 文本中删除下划线

标签 html css

我有一个样式表,为所有 <A> 提供特殊样式元素:

a {
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

但是,我不想将此样式应用于 <A>用作页面 anchor 的元素(例如具有 name 属性的元素。)因此,我创建了另一个类来覆盖 <A> 的一般规则。元素:

.anchor-text {

}

.anchor-text a:hover {
    text-decoration: none;
}

然后我像这样应用它:

<a href = "">
    A Hyperlink (this should underline on hover)
</a>

<a class='anchor-text' name='foo'>
    Anchor text (this should NOT underline on hover)
</a>

但是,同时使用 Chrome 和 Firefox 时,BOTH 当我将鼠标悬停在链接文本和 anchor 文本上时,它们会显示下划线。那么,为什么是 anchor-text样式类未覆盖 <A> 的一般规则标签?

最佳答案

!important 不是问题所在。这是因为您的选择器是 .anchor-text a:hover 而不是 .anchor-text:hover

Here's a JSFiddle

CSS

a {
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

a.anchor-text {

}

a.anchor-text:hover {
    text-decoration: none;
}

HTML

<a href = "">
    A Hyperlink (this should underline on hover)
</a>

<a class="anchor-text" name="foo" href="">
    Anchor text (this should NOT underline on hover)
</a>

关于html - 从 <A> 元素 anchor 文本中删除下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21353659/

相关文章:

html - 如何使标签式导航流畅

css - Shiny 的 selectInput 标签 CSS

html - 如何停止三列液体页面中的列换行?

javascript - 如何在 Bootstrap 中检索按钮的工具提示文本?

javascript - 禁用 JS 时隐藏内容的样式

javascript - CSS/JS : split words with horizontal line in responsive design

html - 如何设置div大小的图片

javascript - 使用 slideToggle 添加/删除类

javascript - Angular Fire,嵌套 ng-repeat

html - 如何在页面调整大小时保持 div 居中?