Using an Image as a Submit Button with CSS
Continuing on from yesterday's tutorial, Error Output With Scriptaculous, in which we got started with a nice little login form showing off simple validation and error output with Scriptaculous, I decided today we would add onto that form a little bit. One neat thing that you can accomplish in creating your forms in HTML is the use of a custom "submit button".
By defining your submit button as an image, you can use any image file to replace the standard grey HTML button you see sprinkled all over the web. In order to do this, you simply change the HTML code you use in your form.
Previously we were using the code:
<input type="submit" value="Login" />
Now obviously, this got the job done but we would like for our site visitors to have a nice themed experience when logging in, so let's style it up! To do so, simply change the "type" attribute and define a "src" attribute on your input element, leaving your code looking like
<input type="image" src="./images/submit_button.jpg" />
The "src" is the path to the image you want to use. I decided I wanted to spruce things up a little more and give the submit button some flare, adding a rollover state to it. In order to do so, I first prepped my image file in Photoshop, leaving me with a two state image:

As you can see, both the on and off states are visible in this image. Why wouldn't I separate them out into individual image files? Well the answer is, there is no need to with the use of CSS (and afterall, I'd rather only have to worry about loading one image on the client side than two).
How exactly is this accomplished? Well for starters, we need to simplify the markup for our submit button. Start by changing the attributes of the "input" element, defining "type","class", and a blank "value":
input type="submit" class="submit" value="" />
Before the CSS is added, we are left with a small non-user friendly button. By defining the "submit" class that we just attached to our "input" element, we can all at once implement our image as the button.
.submit
{
background: url(./images/submit_button.jpg) no-repeat;
height: 42px;
width: 130px;
border: none;
}
In this CSS class we are setting the background image, height and width, and stripping the element of its default browser-styled HTML border. The button is perfectly functional! However, we still want to add the hover state. In order to do so, we simply need to define a hover state for the "submit" class.
.submit:hover
{
background: url(./images/submit_button.jpg) 0 -42px no-repeat;
}
In this code, we are leaving all the attributes the same except for the background property, which we are adjusting the background position with a height of -42px.
View Image as a Submit Button with Hover Example
Voila! There you have some nice little CSS submit button magic. Note, the hover state will not work in Internet Explorer 6, which does not recognize :hover classes in CSS. There is an easy way to work around this, which I will uncover in the not so distant future.


January 11th, 2008 at 4:52 pm
Hi - stumbled on your post looking for a similar workaround. Just FYI, although the method you've described above seems initially to work well enough in FF and Opera (and the hover can even be made to work in IE6 and IE7 if you apply a "hover" behavior file), there are two flaws to be taken into consideration:
1. Form controls, including buttons, cannot be styled in Safari - so that browser will just show the unstyled button
2. Leaving the value attribute blank raises a pair of potential accessibility issues when images and/or CSS are turned of in the browser. No images, the button is completely gone. No CSS (but images on), the default browser button shows but there is no text.
Still looking for a workaround for those two points.
January 15th, 2008 at 11:30 am
Use text-indent: -9999px; to hide the value of value="" so that you can put accessible text there.
March 22nd, 2008 at 2:17 pm
text-indent won't work in Opera for some reason
May 15th, 2008 at 9:06 am
Hi, thanks for this tutorial but how do you make it work for IE 6 and 7.
thanks
May 26th, 2008 at 4:07 pm
Nothing conventional, all functional. It's cool and simple:
padding-top with the size of the image or button.
.submit
{
background: url(./images/submit_button.jpg) no-repeat;
height: 42px;
width: 130px;
border: none;
padding-top:42px;
}
Tested in IE 6 and 7, Firefox 1.5 and Opera 9.
October 24th, 2008 at 1:22 pm
Thanks Muca, the padding-top did the trick! Stupid IE, hopefully we won't have to use this hack in the future…
January 30th, 2009 at 11:34 pm
I worked on a solution to this problem for days and day. Can't thank you enough for the simplicity of this answer.
April 22nd, 2009 at 10:24 pm
[...] Go To Article on Sean Sullivans development blog. [...]
May 22nd, 2009 at 10:38 pm
Add another line to get the right UI feedback:
cursor: pointer;