tags:

views:

76

answers:

11

Here's my .html file:

<hmtl>
<head>
    <style type="text/css">
        .button{
            background: url('images/1.png');
        }

        .button:hover{
            background: url('images/2.jpg');
        }
    </style>
</head>
<body>
<img class="button" src="images/1.png">
</body>

The files are set correctly in the correct folder; but it just refuses to work. What am I doing wrong?

A: 

Probably the best way is to use css and set the background image of the element based on the hover property.

Michael
How though? I don't know how
Serg
+2  A: 
<img src="button.jpeg" onmouseover="this.src='selectedButton.jpeg'" onmouseout="this.src='button.jpeg'"/>
z33m
This doesn't work. I copied exactly as you did, renaming my pictures as I have them. No dice.
Serg
looks okay.. may be you are doing something wrong, check again.. PS. css method is more standard way to do a mouse hover effect
z33m
+2  A: 

The standard method is CSS:

input.mybutton { background-image: url('button.jpeg'); }
input.mybutton:hover { background-image: url('selectedButton.jpeg'); }

Using JavaScript, you can overwrite the .src of an image:

imageElement.src = 'selectedButton.jpeg';
greim
This isn't working. Can you please give a working example? Where do I put input.mybutton? :S
Serg
@Sergio Tapia: Take a look at the example here http://www.kyleschaeffer.com/best-practices/pure-css-image-hover/
R0MANARMY
The first code sample I posted was CSS, not JavaScript. So input.mybutton is CSS code, referring to an <input class="mybutton" ...> Take a look at R0MANARMY's link, it explains everything.
greim
A: 

If you can use jQuery in your application then it will be fairly simple.On mouseover event of the button just swap the background image.Following sample code does it for a div element :-

<div class="title"/>

 $(document).ready(function () {
            $('div.title').mouseover(function () {
                $(this).css("background-image", "url('Forest Flowers.jpg')");
            });
        });
Pawan Mishra
Why bother? This can be done with simple, very simple javascript and css
lucifer
A: 

Images don't generally have background images. Use a different element, and non-relative paths would probably be a good idea, too.

Azeem.Butt
A: 

Hi Sergio. Buttons do not have a hover state. You might want to try A (link) tags, and set the background there.

silverCORE
Not true at all.
Azeem.Butt
guess you wanted to be down-voted. Care to prove your theory that img tags have a hover css state?
silverCORE
http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes "CSS does not define which elements may be in the above states, or how the states are entered and left." Pretty much any DOM element can have a hover state.
Azeem.Butt
Guess you wanted to argue about something you have no experience with.
Azeem.Butt
fine, fine...gotta hand it to you. Though there are ways to say things man. If you were going to correct me (which I'm all for), just include a reference. thanks for the link and the info.
silverCORE
A: 

You can not change the image (value of the src attribute) of the <img> tag using hover.

Sarfraz
A: 

Use a tag to wrap the image.

<hmtl>
<head>
    <style type="text/css">
        a.button{
            background: url('images/1.png');
                            display:block;
                            width: *width*;
                            height: *height*;
        }

        a.button:hover{
            background: url('images/2.jpg');
        }
    </style>
</head>
<body>
<a class="button"></a>
</body>

Edit: I just realize that you are trying to replace the background image under your image....

one option that you have is to use javascript. but if you want to use only CSS and alse want to use this image as control, the above edited solution is good.

Mendy
This code isn't working at all. :\
Serg
A: 

Use javascript to do this. Use onmouseover and onmouseout events to handle this.

If you can use jQuery then you can use the hover event.

$("img.button").hover(function(){
    $(this).attr("src","path of image on mouse over");
},
function(){
    $(this).attr("src","path of image on mouse out");
});
rahul
A: 

Pure css solution:

<html>
<head>
    <style type="text/css">
        .button{
            background: url('images/1.png');
            width: 100px; /* example size. other options: width:100% etc... */
            height: 100px;
        }

        .button:hover{
            background: url('images/2.jpg');
            width: 100px; /* if size of 1.png, 2.jpg same then useless */
            height: 100px; /* if size of 1.png, 2.jpg same then useless */
        }
    </style>
</head>
<body>
<span class="button" src="images/blank.gif"></span>
</body>
</html>

Note: blank.gif - is 1px transparent gif image. (spacer.gif)

gmunkhbaatarmn
A: 

I used an h1 tag. I have tested the following code in IE8,Firefox,GoogleChrome and it is working, we just need to add the doctype correctly:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<style type="text/css">
.button {
    background: url('images/1.jpg');
    height: 400px;
    width: 400px;
}

.button:hover {
    background: url('images/2.jpg');
}
</style>
</head>
<body>
<h1 class="button"></h1>
</body>
</html>
Osama