基于the discussion在这里我们知道可以输入 <div>
在 <a>
考虑到我们使用的是 html5,但我想知道我们如何更改此 <div>
中的样式。简而言之,我有一个这样的html代码骨架:
<a href='somelink.html'>
<div style='width:100px; height:100px'>
text text text
</div>
</a>
动机是希望达到这样的效果,当鼠标悬停在div上时(不仅仅是文本上),div
中的背景和文本颜色都一样。变化。请注意,在 css 文件中的某处,第三方模板已经定义了 a:hover
( a:focus
),我想覆盖这个特定的 div
中的悬停行为。有人可以给我提示我应该如何实现这一目标吗?
给你一个具体的例子,我正在玩 w3cschool.com ,下面提供了完整的代码。我在注释中指出了要填写的代码的位置。
查看方 block 的背景颜色div
是浅灰色的,里面的文本颜色是绿色带下划线,当鼠标悬停在文本上时,文本颜色变为红色。
任务
我希望有效区域是整个div
,即当鼠标位于div
时,可以实现以下目的:1)div
变为粉红色,2) 文本颜色变为白色,3) 悬停时不再有下划线。
代码
<!DOCTYPE html>
<html>
<head>
<style>
/* Assume following 4 rules are provided somewhere in the existing css */
a:link {
color: green;
}
a:visited {
color: green;
}
a:hover {
color: red;
}
a:active {
color: yellow;
}
/* ==== CHANGE HERE ======== */
.fill_a {
// what is the answer?
}
.fill_div {
// what is the answer?
}
/* ==== CHANGE HERE END ==== */
</style>
</head>
<body>
<p>Mouse over and click the link: <a href="http://www.w3schools.com">w3schools.com</a></p>
<a href='#' class='fill_a'>
<div style='width:100px; height:100px; background-color:#ddd' class='fill_div'>
123
</div>
</a>
</body>
</html>
感谢您的提前答复。
编辑 1:更多背景
我正在寻找一个非div的解决方案(使用 block anchor 或只是一个跨度),但我遇到的困境是我正在使用 Bootstrap ,特别是,我希望头部是整个链接,这有点动机。例如,
<div class="panel panel-default">
<div class="panel-heading">This to be an anchor</div>
<div class="panel-body">Panel Content</div>
</div>
最佳答案
基于您的 HTML
来自您的任务
CSS
.fill_a:hover{
color:white;
text-decoration:none;
}
.fill_div:hover{
background-color:pink !important;
}
你应该使用!important
,因为在你的html中你已经设置了样式。这意味着放在 html 中的样式优先。
了解有关 css 样式的更多信息 https://css-tricks.com/when-using-important-is-the-right-choice/
关于css - 我应该如何覆盖 anchor 标记中 div 的悬停样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35620657/