Create mouse hover effect for button using CSS sprite


You must have noticed buttons in various websites that its appearance on mouse hover. Typical example is the download buttons in Mozilla website’s download link. Below is a tutorial to one such button, on mouse hover the button changes its color from blue to red.

 

We need few things in hand: couple of buttons (one for before hover and one after hover). You can download such a set of buttons in PSD (Photoshop file) format from our download section for free. If you do not know how to use (or do not have it); you can follow this tutorial on how to create basic 3D shapes in Windows 7 MS Paint.

 

So, after this step, you will get two set of buttons, exactly the same in size :

 

mouse hover effect for button

 

Now, we need to create CSS sprite of these images. CSS sprite is nothing but combining two images into one single image; CSS will direct which part of the image will be used for which action.

Creating CSS sprite is easy. Just go to some free online service that provides such work to do. We have used CSS Sprite Generator website to illustrate the method. You have to two images: first the normal and then the hover one; then click the Generate button. After few seconds you will get link to download this combined image (that is the sprite) and our required CSS code. Rename the sprite as “Download-button” for this tutorial. After practice, you can create other buttons of your own. For the above two images, we got this as a sprite:

 

mouse hover effect for button using CSS sprite

 

Basically this CSS code will only say you specific position of the buttons; it is not the full CSS code we need. Here is the full CSS code (for the button we have used):

 


/***** Download-button ********************/
a.rollover {
display: block;
width: 203px;
height: 70px;
text-decoration: none;
background: url("images/Download-button.png");
background-position: 0px 0px;
}

a.rollover:hover {
background-position: 0px -80px;
}

.displace {
position: absolute;
left: -5000px;

 

 

 


We have marked the codes with different colors which you need to change.

The red colored digits: It is simply the dimension of one individual button. To know this dimension; simply right click on any of your button images (not the sprite!) in Windows 7 and click Properties. In the Properties dialog box, go to tab and you will get the dimension. Replace with our digits with your digits. Do everything in Notepad, not in Wordpad or Word. For Mac, just clicking and selecting it will show the dimension on right hand panel.

 

The blue colored url: It is simply the url of your image sprite. Upload to whereever you want and put the url there.  For self hosted WordPress blogs / sites; you can use url in the form we have used. Others can put the full url (with http) there.

 

The pink colored digits: You will get that value from CSS sprite generator website. Just replace it. Do not copy-paste the whole CSS from there.

 

Put the completed CSS code to your CSS file (better to put at the end, if you mess something, you can find it out easily!) ; for Bloggers, go to HTML editor, put this code where all CSS codes ends and HTML codes begins (generally in the middle part). Though there is no hard and fast rule, it is better to keep the things tidy.

 

Save the CSS file (or HTML editor in case of Blogger).

Now, go to write a new post (use HTML mode). This is the required xHTML code:

 

<a class="rollover" title="Download-button" href="your target url here"><span class="displace">Download-button</span></a>

 

Replace the red text “your target url here” with the link url you want and green text “Download-button”  with the text you want to appear in case the image load is off in user’s browser (or can not load due to low bandwidth, mobile devices etc.). Save it. Only the text will appear when you switch from HTML to Visual mode (in WordPress). Click “” and you will get your custom button with hover effect.

Here is the result:

 

Download-button

 

If you click the button, it will lead to this website’s header.

 

For advanced users: You can set the position of the button from the last parameter of the CSS code.

Note for Windows users: When copy-pasting the the xHTML code, after pasting sometimes class=”rollover” and span class=”displace” disappears; to avoid this, copy the code then paste to Notepad, again copy from Notepad and paste to your website’s writing panel in HTML mode. Check if it disappears or not! If still disappears, manually type the codes. Otherwise, obviously it will not work.

 

Signature


If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave Lame Behind - Get Engaged

Here’s what we’ve got for you:

Contact Us Today for Smarter Marketing of Knowledge

See each of the premium resources below:

For Publishers and Content Owners

Do you want to optimize your available content?

Do you want to increase the visibility of your content?

Do you want to increase your reliability as an Author?

Do you want your next or existing project to be technically planned?

If your answer to any of these is YES contact us with your approximate budget.

Contact Us Today

For the Media Companies and Advertisers

You must be seeking a publisher website like ours with real content to give an exposure to your client's business?

We provide Industry's Standard opportunities for advertisement.

Do you want some promotion that comes with proper invoicing, SLA on per annum basis?

If your answer is YES contact us today for a quote.

Contact Us Today for Quote

Comments

  1. Albert says

    I notice that in IE the rollover image shifts 1 pixel. It looks fine in FF, Safari and Chrome. Is there a fix?
    Thanks.

    • says

      IE 6 like few problems are back in new form IE 8 (I HOPE they will be in IE 9, I have not tested) causing headache of the webmasters and developers.
      I have to write a few things before starting the IE fix sad songs.
      There was a fix for IE 6 (which MS claimed as the best browser ago) but that does not work for IE 7 or IE 8. People “discovered” another fix for IE 7, that does not work in IE 8. Now IE 9 (Beauty of the ? ) : I must admit, I found no fix anywhere. Now the sad song starts:

      1. IE 6: add

      a:hover {zoom:1;}

      2. IE 7:

      They says in MSDN blogs hover things will only work on xHTML STRICT declaration in the header as W3C says STRICT is better than TRANSITIONAL.

      3. IE 8 (may or may not work for IE 7):

      Add this before background url starts defining hover:

      filter: none !important;

      So the thing for hover becomes:

      .yournicebutton:hover {
      filter: none !important;
      background: url(images/yournicebutton.png); background-position: 0px -80px;

      }

      4. Other things:

      You can use the same background color of the button in addition to hide the bald area. For unknown reasons (to me) IE loves gif; no problem occurs with gif. These are exclusive with transparent png.

      As your website is about business it is a serious affair. Try to use CSS solid color or gradient (we will write soon) to create button effects.

  2. lizardwijanarko says

    Here only 2 button, hover and normal button. If we want to add one more button eg clicked button (normal, hover and when we press the button) how to do that? I want to use it on my blogger web, thanks

    • says

      You want to add three states : Normal, Hover and Active. That is the same like above. Just you need to define the CSS property of hover state.

      1.Create one image sprite with three button images instead of two like above.

      2. add one additional :

      a.rollover:active {
      background-position: 0px -xypx;
      }

      You can add another : a.visited. For example, once clicked it become dimmed.

Mentions

  1. [...] can read CSS tricks on how to create hover buttons, how to use text shadows to get pressed text effect, how to create custom blockquotes , how to [...]

  2. [...] you will need to add extra CSS code to that we mentioned above. Nothing much difficult. We already wrote a separate article on how to create such mouse hover action button [...]

  3. [...] Create Mouse Hover Effect for Buttons Using CSS Sprites [...]

  4. Most Hot Latest CSS And CSS3 Buttons, 110 Best Of The Best Sets ! says:

    [...] Create Mouse Hover Effect for Buttons Using CSS Sprites [...]

  5. CSS And CSS3 Buttons, 110 Best Sets ! says:

    [...] Create Mouse Hover Effect for Buttons Using CSS Sprites [...]

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>