如何使图像悬停在CSS? [英] How to make image hover in css?

查看:114
本文介绍了如何使图像悬停在CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当鼠标悬停时,我想将图像从正常更改为更亮,我的代码:

I want to change the image from normal to brighter when it's on hover, My code:

    <div class="nkhome">
        <a href="Home.html"><img src="Images/btnhome.png" /></a>
    </div>
.nkhome{
    margin-left:260px;
    top:170px;
    position:absolute;
    width:59px;
    height:59px;
}
.nkhome a img:hover {
    background:url(Images/btnhomeh.png);
    position:absolute;
    top:0px;
}

为什么不工作的悬停?

Why doesn't work the hover? When my mouse is on it, it shows the first image, not the hover image.

推荐答案

您有一个 a 标签包含 img 标签。这是你的正常状态。
然后添加 background-image 作为悬停状态,它出现在您的 a

You've got an a tag containing an img tag. That's your normal state. You then add a background-image as your hover state, and it's appearing in the background of your a tag - behind the img tag.

您应该可以创建一个CSS sprite并使用背景位置,但这个标记位于 img 应该让你开始:

You should probably create a CSS sprite and use background positions, but this should get you started:

<div>
    <a href="home.html"></a>
</div>

div a {
    width:  59px;
    height: 59px;
    display: block;
    background-image: url('images/btnhome.png');
}

div a:hover {
    background-image: url('images/btnhomeh.png);
}

A List Apart Article from 2004 仍然是相关的,并且将给你一些关于sprite的背景,以及为什么使用它们而不是两个不同的图像是个好主意。这是一个比我能解释给你更好的书。

This A List Apart Article from 2004 is still relevant, and will give you some background about sprites, and why it's a good idea to use them instead of two different images. It's a lot better written than anything I could explain to you.

这篇关于如何使图像悬停在CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆