This was one of those things I’d wanted to figure out, but I hadn’t sat down to finally come up with a workable solution. When I did, it seemed so simple, but I hadn’t read about it anywhere else so I thought I’d put it out there for other people to use.
The basic idea is it’s nice to have images for things like submit buttons, but it’s equally annoying to generate as many of these as you have text to put over them. Wouldn’t it be nice to just use the same image but vary the text which is displayed? Of course. And clearly you can do something by putting a background-image over any div and having JavaScript trigger the form submit, but that is a little ugly in my opinion. You always would have to remember to include the form submit code in your page, instead of simply using the div as you would a standard input type=”image”.
Well it turns out you can have both. All that you really need to do, is put the image you want to use as a submit button into the background of a div, as you might before. And put the text you want over the image as the content of that div. The next part is the trick, and it’s a simple one: using absolute positioning, put a input type=”image” right over the top of that div. But, just use a clear gif as the image. Viola! Now you can see and read the text and the image behind it, but when you click on it, you’re really just clicking on the submit button. Just make sure the container is position:relative so that it works in Internet Explorer as well as FireFox. Also note we’re sizing the input at 100%, so that all we really need to size is the image part, making that class reusable for different sizes of submit image.
About the only downside I can see to his technique is that you can’t easily copy the text on the submit button, since you’ll be selecting the clear gif sitting on top of it. But, that really doesn’t bother me much. And you would not be able to copy text that sat inside of an image in any event.
Markup is like this:
<div class="image-submit-container image-submit-big">
Continue
<input type="image" name="image-button" value="Continue" src="spacer.gif"/>
</div>
And styles are like this (flavor to taste):
.image-submit-container {
color:#FFFFFF;
font-weight:bold;
position:relative;
}
.image-submit-container input {
width:100%;
height:100%;
display:inline;
float:left;
position:absolute;
top:0px;
left:0px;
}
.image-submit-big {
background: transparent url(btn_blank_big.gif) no-repeat 0 0;
height:36px;
width:190px;
padding-top:6px;
}