Many of the emerging techniques in CSS3 are particularly well suited to interactive Web page elements such as buttons. In this tutorial we will explore using CSS3 properties including transforms, transitions, box shadows, backgrounds and rounded corners. We will be working with an anchor element, animating the button when it is in hover state. Most modern browsers support the bulk of the properties we will be using, but users with non-supporting browsers will still be able to see and interact with the button.
This is how the button will look in its initial and hover states, with a moment during the transition captured in-between:
Create Your Page and Link
Let’s start by creating an HTML5 page as follows:
<!DOCTYPE html> <html> <head> <style type="text/css"> </style> </head> <body> </body> </html>In the body section, insert your anchor element inside a container element as follows:
<div id="container"> <a class="anim" href="index.html"> <img src="note.png" alt="link"/> <span>Music</span> </a> </div>The anchor contains an image and some text in a span. We include a class name for the anchor, which we will use to identify it and its contained elements. The container element is really just there for the purposes of demonstration.
We will be using four images for the link button, so download them now, or use your own if you prefer. This image appears within the button and will be rotating on hover:
This is the main background image for the button:
These are alpha transparent gradient images we will display on top of the main background image:
Insert General CSS Properties
Before we get to work with animated properties, let’s get the basic CSS properties for the button sorted. In the style section of your page head, add the following to style the container element:
/*basic properties*/ #container { padding:20px; background:#FFCC00; width:300px; }The color and width have been chosen to suit the images we are using, but feel free to alter them. Next style the link with basic properties:
/*link*/ a.anim { margin:auto; display: block; background:#FF0000; width: 200px; height:60px; text-align:center; text-decoration:none; color:#000000; font-weight:bold; font-family:sans-serif; font-size:large; border:2px solid #FFFFFF; }These properties are all pretty straightforward – the dimensions are again chosen to suit the background image we are working with so you will need to alter them if you are using a different image. Let’s add another couple of lines in this section before we move on. With CSS3 we can use rounded corners, so before the closing bracket for the “a.anim” section:
/*rounded corners*/ border-radius:15px; -moz-border-radius:15px;The border radius specifies the extent to which the corners should be rounded. The vendor prefix version is for older versions of Firefox. After this section, position the image within the link as follows:
/*position img*/ a.anim img { margin-top:5px; }Now set the basic styling for the text inside the link:
/*link text*/ a.anim...