In Part 1 of this tutorial, you designed a button sprite that will be coded with HTML, CSS, and JQuery in this part of the tutorial.
If you do not want to complete part one of this tutorial, you can download the source files created in that lesson here.
Step 1 – HTML
Different people will require a button for different purposes. The remainder of this tutorial will explain a simple scenario where the button functions as a simple download link. Create a link to an imaginary (or real) file for download:
<a href="path/to/download.zip" class="button"></a>
Step 2 – CSS
Add the following CSS to your HTML document:
.button {
width:570px;
height:64px; /* Notice that the height is not the height of the whole sprite, but the height of one single button */
display:block;
background-image:url(images/downloadbutton.png); /*path to the sprite*/
background-position: top; /* the background position (in combination with the height!) makes it possible that only the top of the whole sprite will be visible */
}
When you apply the CSS code above, you will only see the grey button, because it’s positioned on top and the height is 64px
Link hover button
.button:hover{
width:570px;
background-position: bottom;
height:64px;
background-image:url(images/downloadbutton.png) no repeat;
}
When you apply the CSS code above, you will only see the green button when you hover the download button, because it’s positioned at the bottom and the height is 64px
Demo: View Live Demo
Step 3 – Fading hover effect
This step is not necessary, but it’s an optional step. The transition will be smoothed with JavaScript. We’re going to use the popular jQuery library.
The original tutorial comes from this website. I’ll do my best to explain.
Step 4 – Add code between the head tags
Download jQuery. First we need to refer in the head to the .js file that we’ve just downloaded.
<script type="text/javascript" src="path/to/jquery-1.3.2.min.js"></script>
After that we can add the following code between the head tags.
<script type="text/javascript">
$(document).ready(function() {
// Add the class 'button' just like in CSS with a dot in front of it
$('.button').append('<span class="hover"></span>').each(function () {
var $span = $('> span.hover', this).css('opacity', 0);
$(this).hover(function () {
$span.stop().fadeTo(500, 1); //Change the number 500 to change the speed of the Fade In
}, function () {
$span.stop().fadeTo(500, 0); //Change the number 500 to change the speed of the Fade Out
});
});
});
</script>
How to deal with several buttons on one page
If you have several buttons on one page and you’d like to add the fading hover effect, you can give it a different class in the HTML and add this in the JavaScript above after the word .button and you need also to separate the words with a comma. (Example: ‘.button,.buttonTwo’)
Step 5 – Edit the CSS
.button {
position:relative;
display:block;
height: 64px;
width: 570px;
background:url(images/downloadbutton.png) no-repeat;
background-position: top;
}
.button span.hover { /*notice the different class: span.hover*/
position: absolute;
display: block;
height: 64px;
width: 570px;
background: url(images/downloadbutton.png) no-repeat;
background-position: bottom;
}
View Live Demo to check the result.
Download the source files (PSD included)
Related Topics: image sprites, jquery tutorials, web development tutorials, web interface design
Free Goodies. Delivered to You.
Subscribe to Tutorial9, and we'll deliver you the newest freebies and tutorials for free.
Subscribe By Email Subscribe By RSSWrite for Tutorial9
- Do you want to get paid $150 for writing at Tutorial9?
- Are you a talented Photoshopper, Blogger, or Photographer?
- Want to help thousands of others by sharing your knowledge?
If so, we're interested in you, and we'll pay you. Find out how to write for Tutorial9.




123 Comments Leave a Comment
Hi, Nice tutorial.
But I was wondering if we could use the effect on a input type sumbit =D?
I tried a lil but it never worked =S
Vincent-
It might be better to run a script that replaces input type=submit with a link, and an onclick triggering the form.
The reason is because IE doesn’t handle the sprite replacement technique on input type=button or type=submit properly (the value=”text” cannot be text-indented unlike in other browsers).
khalid, good tutorial, thanks
One could have several buttons by doing this:
- Remove the background property from the .button
- Create different rules for each button (will only contain the background property)
- Set the span:hover background to “inherit”
This way the jQuery code doesnt have to be modified
Thanks for the tutorial. It’s helped me with understanding jquery a little bit more. slow and steady!
So Cool …. Thank For This
Thanks for this resource, great tutorials for a newbie.
Thanks! It’s fully tutorial, I like this!!
Hi, nice tutorial but i have got a problem, with the validation thing….
document type does not allow element “span” here
…button,.donatebutton’).append(”).each(function () {
how can i fix this problem ? On my template i’m not using any span tag how can i fix ?
Put the javascript in a new file and call it script.js (or another name, but keep the extension .js). Now refer to this file between the head tags:
<script type=”text/javascript” src=”path/to/script.js”></script>
Now you can delete the javascript on your html page. This part can be deleted:
<script type=”text/javascript”>
javascript stuff
</script>
very good thank.
I think that you can make the same effect of button only with CSS which offer linear gradient too and you don’t need to use an image for it. But for more effects you can use an image, it is better.
You can do a lot with CSS3, even the fading effect can be created with CSS3. But recreating the whole buttons, especially the green one, without images is not (yet?) possible.
The download link for the source files does not work. Can someone provide them to me?
Thanks
Good work! add to bookmark! Thanks
very cool
Thankks
Thank you for the clear instructions, good
I’m sorry, but when I did everything like it was written in article
my page could not load. Why it can be so?)
Maybe it’s the javascript between the head tags. The code in the tutorial has some faults: you’re seeing > instead of >
Copy the code from the demo.
great instructions thanks for sharing with us
it’s bad very bad tutorial not professional use of jquery!! why changing the image so long ??? for me this tutorial is stuped …
Hi mr47,
I do not understand you. ‘why changing the image so long???’ < If this is your argument I don't understand you, because I haven't changed an image …
Good work! Thank you for the clear instructions
No. i about the time. it’s very long ….
I don’t think it’s too long, but that is my personal opinion. However, you can change the speed of the transition, as I stated in the tut.
Awesome tutorial.. Thanks for the share…
Good wook, I have tried to add to the button “position:absolute”, at this moment the effect has stopped working. Why??
Something is not ok with the code for me. I add the hover script and DW says:
</span>').each invalid markup, not correct html format?
The hell is that?
Copy the text/code from the demo. Something went wrong with the code in the tutorial.
Hey great post, I’ve been looking for a tutorial for a while that explains how to do this jquery hover fading technique. This has really inspired me to use it in a couple of my websites as its such a nice transition, I can see it working great with images with a hover state that changes it from a grayscale to colour image.
Thanks!
Awesome tutorial.
I have applied this to a span, we’ll call it image. This is in larger span called article.
How can i make the hover effect happen in the image span when i my mouse is over the article span?
Thanks
EXCELLENT!
Its great to see both PS and implementation …….. Thanks dude
Very Nice Tutorial!!!
I’m sorry for my english but i’m french! ^^
I have a little problem with the javascript effect:
-when I hover over the button with my mouse, image is erased instantanously and next, the “hover image” appear… I want to have a true transition like in the Demo, but it doesn’t work :/
I advise you to copy the javascript code from the demo. Something went wrong with the code in the tutorial.