Html CSS:悬停时更改背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20289481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:17:30  来源:igfitidea点击:

CSS: Change Background Color on Hover

htmlcss

提问by RogueJD

I've searched other questions, and the solutions I've found don't work.

我搜索了其他问题,但我找到的解决方案不起作用。

Here's the deets: Webpage has a .side_nav . There are 4 elements to that . Each has a small img, an h2, and a p.

这是 deets:网页有一个 .side_nav 。有 4 个要素。每个都有一个小的 img、一个 h2 和一个 p。

I'd like for the links to have a lighter color when hovered. I feel as if I'm missing something small...

我希望链接在悬停时颜色较浅。我觉得好像我错过了一些小东西......

I'm using DW:CC, and interestingly enough, it does not show the normal background in the in "Live" view. It does, however, show the :hover action in "Live" view, just not in any of the browsers I've tried.

我正在使用 DW:CC,有趣的是,它没有在“实时”视图中显示正常背景。但是,它确实在“实时”视图中显示了 :hover 操作,只是在我尝试过的任何浏览器中都没有。

HTML:

HTML:

<nav class="fluid side_nav">
    <div> <a href="#"> <img src="images/images.jpg" alt=""/>
        <h2>Estimates</h2>
        <p>Click here for an estimate</p>
        </a></div>
    <div> <a href="#"> <img src="images/images.jpg" alt=""/>
        <h2>Repairs</h2>
        <p>Click here for an estimate</p>
    </a></div>
    <div> <a href="#"> <img src="images/images.jpg" alt=""/>
        <h2>Maintenance</h2>
        <p>Click here for an estimate</p>
    </a></div>
    <div> <a href="#"> <img src="images/images.jpg" alt=""/>
        <h2>Specials</h2>
        <p>Click here for an estimate</p>
    </a></div>
</nav>

Then, the CSS:

然后,CSS:

.side_nav {
    clear: both;
    margin-top: 10px;
    margin-bottom: 10px;
}
.side_nav img {
    height: 35px;
    padding-right: 15px;
    float: left;
}
.side_nav a {
    display: block;
    border: 1px solid #151515;
    margin-bottom: 8px;
    border-radius: 8px;
    padding-top: 8px;
    padding-right: 8px;
    padding-bottom: 8px;
    padding-left: 8px;
    -webkit-box-shadow: 4px 2px 6px #8E8E8E;
    box-shadow: 4px 2px 6px #8E8E8E;
    background-image: -webkit-linear-gradient(0deg,rgba(40,121,216,1.00) 0%,rgba(25,77,138,1.00) 99.48%);
    background-image: -moz-linear-gradient(0deg,rgba(40,121,216,1.00) 0%,rgba(25,77,138,1.00) 99.48%);
    background-image: -o-linear-gradient(0deg,rgba(40,121,216,1.00) 0%,rgba(25,77,138,1.00) 99.48%);
    background-image: linear-gradient(90deg,rgba(40,121,216,1.00) 0%,rgba(25,77,138,1.00) 99.48%);
    color: #FFFFFF;
}
.side_nav a:hover {
    background-color: #3374C2;
}

Any tips?

有小费吗?

回答by SW4

You have background-imagealready set, which supercedes color. Change the your CSS to just use background:

background-image已经设置了,它取代了颜色。将您的 CSS 更改为仅使用background

.side_nav a:hover {
    background: #3374C2;
}

FIDDLE

小提琴

回答by Hiral

Write:

写:

.side_nav a:hover {
    background: #3374C2; //background instead of background-image
}

Fiddle here.

在这里摆弄。