Resizable Buttons Using CSS Sprites

Posted on Wednesday 25th May 2011 by Michael Angrave 5 Comments

In a world where we’re increasingly focusing our website design efforts towards the delights of CSS3, it’s quite easy to forget those who haven’t made the switch to modern browsers. Even though it’s beneficial to keep a finger in all of the latest pies. I still want my websites to look exactly as I designed them in all browsers. At the end of the day, I’m looking to ensure that my clients receive as much business as they can, so everyone matters.

I just wanted to go through a commonly used little technique, which allows you to create resizable buttons with rollover states using CSS sprites. This technique is found all over the web these days, and in particular where I started using this, with Magento’s very popular, Modern Theme.

View Demo

So how does it work?

The technique basically uses an anchor link with a span snugly fitting inside. The two elements both have background images, one provides the left hand site and the expanding middle, and the other has a right positioned closing image, which completes the look of the button.

It’s a very simple concept that works in-combination with the basic CSS sprite to give us an effective and cross-browser button.

Creating the background image

In order to get a better idea of how this works, you can see the single image that I will be using. The image and the Photoshop file are both contained within the download folder at the bottom of the post.

Sample Button

The HTML

<a class="my-button" href="#" title="My Button"><span>My Button</span></a>

The CSS

a.my-button { background:url(button_small.gif) no-repeat top right; display: block; float: left; height: 40px; margin-right: 20px; padding-right: 25px; text-decoration: none; font-size:1.2em; font-weight:bold; text-transform:uppercase; }

a.my-button span { background:url(button_large.gif) no-repeat; display: block; line-height: 40px; padding: 0 0 0 25px; color: #333; }

a.my-button:hover span { background-position: 0 -40px; color: #fff; }

a.my-button:hover { background-position: right -40px; }

Download the files

Please fell free to download the files that I’ve used including…

  • HTML
  • CSS
  • Graphics files, both PSD and PNG

Beware of IE6

No good blog post would be complete without the footnote warning of the potential dangers of using this technique with IE6. Although the example I’ve used, should be fine, bear in mind that the background positioning of a PNG causes issues in IE6. This could be avoided with a PNG fix.

Michael Angrave

Nothing is known about Michael Angrave at this time.
This entry was posted in HTML & CSS Tutorials and tagged , . Bookmark the permalink.

5 Responses to Resizable Buttons Using CSS Sprites

  1. Richard says:
    June 9, 2011 at 11:33 pm

    this is one of the great techniques i pick up from using Magneto, it just keeps on giving!

  2. Patrick says:
    June 16, 2011 at 2:11 pm

    Nice tutorial, thanks for sharing.

  3. mpvanitha says:
    August 16, 2011 at 6:13 am

    Very nice tutorial…

  4. viona says:
    November 23, 2011 at 6:14 am

    Good tutorial and I’ve use this method on my own web. But I’ve test ur demo on both IE6 and IE7, the hover one displays below the original one, since it cant fit for IE6/7. Any good ideas to fix it?

  5. Big D says:
    March 5, 2012 at 5:20 am

    You only need 1 image sprite.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>