html - 悬停时更改 CSS

标签 html css hover

我有以下 HTML 代码:

<div class="counters2">
    <div class="one_fourth"> Item 1 <h4> 99$ </h4> </div>
    <div class="one_fourth last"> Item 2 <h4> 139$ </h4> </div>
</div>

以及以下 CSS 代码:

.counters2 h4 {
    font-size: 18px;
    color: #999;
    font-weight: 400;
    margin: 20px 0px 0px 0px;
}
.counters2 .one_fourth {
    background-color: #f3f3f3;
    padding: 30px 0px;
    border-radius: 4px;
    font-family: 'Open Sans', sans-serif;
    font-size: 35px;
    color: #393939;
    font-weight: bold;
}

我如何在悬停期间更改 :

  • 文本颜色为白色 (#fff)
  • 背景颜色为绿色(#9eca45)

我的实际代码的问题是 <h4> 之间的价格标签更改颜色不会更改为白色,直到我将鼠标悬停在它上面。我怎样才能改变它?我尝试了什么:

.counters2 h4.active, .counters2 h4:hover {
    font-size: 18px;
    color: #fff;
    font-weight: 400;
    margin: 20px 0px 0px 0px;
}
.counters2 .one_fourth.active, .counters2 .one_fourth:hover {
    background-color: #9eca45;
    padding: 30px 0px;
    border-radius: 4px;
    font-family: 'Open Sans', sans-serif;
    font-size: 35px;
    color: #fff;
    font-weight: bold;
}

最佳答案

您需要删除 h4 中的 color,或者添加覆盖 color: #999; 的特定规则,例如:

.counters2 .one_fourth:hover h4 {
    color: #fff;
}

FIDDLE

关于html - 悬停时更改 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30949299/

相关文章:

html - 使用 float with canvas 的 CSS 布局问题

html - 关于div的简单CSS问题

javascript - 将不透明度更改切换为复选框图像

html - 将 Bootstrap li 元素置于响应页面的中心

html - a11y : Input fields, 包裹在 div 中,附加 "by id"到标签

javascript - 如何使用html代码将html页面转换为excel

javascript - 令人困惑的悬停效果

html - css 对齐 ul 以形成

jquery - 如果父元素也有鼠标悬停,如何为子元素触发鼠标悬停事件?

image - 悬停链接以更改图像的不透明度