views:

186

answers:

2

I have two reasons I want to use csssprites for submit buttons :

  • I have dynamically created button, which in the future might be localized
  • I want just 1 HTTP request even if I have 5 buttons on the page

The problem is that I want to avoid javascript for buttons, and therefore I need to use an input field of type image. I can set the background image on this field just as i would for any csssprite.

The problem is that I found I had to set the image source to an empty pixel or else I get a broken image icon.

With that said, this is the best solution I have come up with :

<style>
    .csssprite_cat {
     background-image:url(http://www.mrfreefree.com/images/speciali/211/funny_cat_50.jpg); width: 50px;
     height: 50px;
    }
</style>

<input type=image src="http://www.grasse-ac.com/gif/no_pixel.gif" class="csssprite_cat">
<input type=image class="csssprite_cat">

(You can just load this file directly into a browser - i am borrowing images from a random site).

It works kind of OK, but I have to use our good old friend pixel.gif. Very nostalgic!

There probably isn't a better way without javascript, unless there is a way in css to hide the broken image text and icon.

+1  A: 

Don't use the image input type, you're much better off using a bog standard submit button and styling it that way.

Once you have defined your background-image, you'll need to define unique background-positions for each different button you'd like to use.

For backwards compatibility I would advise using class mixtures (since as of now IE still doesn't support CSS3 attribute selectors), like so:

<input type="submit" class="submit uniqueButtonClass" value="Submit" />

If you give the .submit class the background-image property, you can set the background-position property independently for each individual button, and avoid having to repeat yourself several times.

Example:

.submit { background-image: url(path/to/image.png); width: 50px; height: 25px; border: 0; }
/* assuming 25px is the offset you're using */
.uniqueButtonClass { background-position: 0 25px; }
Will Morgan
no problems with older browsers trying to draw an outline?
Simon_Weaver
ad i suspected this just changes the background of an existing button. it doesn't give me a clean button
Simon_Weaver
You'll need to also set border: 0; to remove the border. Thanks to whoever downvoted me though :)
Will Morgan
@will edit your css to include border:0 and i'll upvote you :-) it wont let me change my vote. urm not saying it was me that downvoted you though... oh and i also needed to put value=" " to prevent the 'Submit' text appearing on the button on top of the image. otherwise it works in the browsers i've tested it in
Simon_Weaver
Simon, it's been updated.
Will Morgan
A: 

I would use either a input type as above, or you could also:

<button><img src="http://www.mrfreefree.com/images/speciali/211/funny_cat_50.jpg" /></button>

The button tag allows you to pretty much throw anything in it, and allow for greater control on formating.

In this case you could style a span inside the button or the button it's self.

Davin
shouldnt i be cautious of this : Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the <button> and </button> tags, while other browsers will submit the content of the value attribute. Use the input element to create buttons in an HTML form.
Simon_Weaver
Yes, I only use this when I there is either 1 button or it has a unique id then do something like isset($_POST['my_button_name']) - in PHP. I have moved away from relying on the value of a button, as clients tend to change the text in the buttons from time to time, where the id is a constant.
Davin