0

I am trying to figure out how to make my submit button image change to another image when the cursor hovers over it. Here's my code so far:

<!DOCTYPE>
<html>

<body>

<form>
<font face="Arial">Please type your name below and click submit.</font>
<br />
<input type="text">
<p>
<input type="image" src="..\Pictures\submit_button_img.jpg" alt="submit" >
</p>
</form>

</body>

</html>

This code works, I just don't know how to switch the image when the cursor hovers over it. Any suggestions? I'd like it to be in CSS or JavaScript, please.

xNirdx
  • 39
  • 1
  • 1
  • 5
  • possible duplicate of [How to change an input button image using CSS?](http://stackoverflow.com/questions/195632/how-to-change-an-input-button-image-using-css) – Austin Jan 08 '14 at 19:09

2 Answers2

4

use

<input type="submit" class="somename">

and add a class name that has the image set as a background-image and then use CSS :hover to change that image. No Javascript needed

  input[type=submit]{
    width: 100px;
    height: 20px;
    background-image: url("someimage.jpg");
    background-repeat: no-repeat;
    border: none;
    text-indent: -9999px;
  }

  input[type=submit]:hover{
    background-image: url("someimage2.jpg");
 }

You can target the submit button or you can add a class name, either way. You can also use a sprite and change the background-position on hover instead of swapping image files

quick example: JSFIDDLE

jmore009
  • 12,863
  • 1
  • 19
  • 34
1

Ideally, you should use a sprite. It's a great way to do rollovers without that "flash of emptiness" before the second image loads.

First, create your sprite. It should be one image that looks like this:

+---------------+
| Normal button |
+---------------+
+---------------+
| Hover  button |
+---------------+

Note that the two "parts" should be the same size as each other, and this will be the size of your button. Save this, for example as btn_sheet.png

Now you need to prepare your input:

<input type="image" class="btnimage" />

And now for the CSS that makes it all work:

.btnimage {
    width: 100px; /* replace with actual width your button */
    height: 50px; /* replace with actual height of the button */
    background:url('/btn_sheet.png'); /* adjust path as needed */
}
.btnimage:hover {
    background-position: 0 -50px;
    /* replace the 50px with the actual height of the button */
}

And done!

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592