How to Create a 3D Button Using CSS and a Gradient Image

While I was browsing through dzone today, I stumbled on this 3d button css tutorial by Dave Woods.

Using some simple css and a little color know-how, he shows you how to turn a square button into a pseudo 3-dimensional button. I’ll take it a step further and add a gradient background image to enhance the lighting effect. Like this…

Web Cash

We can build this button up in a couple of steps. First, we need to make our anchor tag (link) into a block - with some padding and a background.

Web Cash

a.button {
   background: #ab7f20;
   padding: .5em;
   color: white;
   text-decoration: none;
   font-weight: bold;

That gives us a nice, squarish look… kind of like a button. But it definitely looks flat and two dimensional. The next step we’re going to take is to add a gradient background. This will add a nice lighting effect - giving the button a bit of depth.

To do this, we need a background image. I made a gradient image in GIMP (read this tutorial to learn how to create a gradient image like this). Then we need to add the image to the css declaration by changing one line.

Web Cash

a.button {
   background: #ab7f20 url(uploads/2008/01/gradient-button.thumbnail.png)
     repeat-x top;

Notice that the color (#ab7f20) is the same as the second color in the gradient image. This gradient is long enough to color the whole area - but if our button gets too tall, we want it to blend in nicely.

The last thing we need to do is give it some borders - light on the top and dark on the bottom. This will give it the illusion of being hit by the light. It’ll also accentuate the effect of the gradient background image.

Web Cash

a.button {
   border-top: #f6daa3 3px solid;
   border-right: #724f05 3px solid;
   border-bottom: #724f05 3px solid;
   border-left: #f6daa3 2px solid; }
 
a.button:hover {
   border-top: #724f05 3px solid;
   border-right: #f6daa3 2px solid;
   border-bottom: #f6daa3 2px solid;
   border-left: #724f05 3px solid;
}

Notice that the background colors are flipped when you hover over the button. This gives the illusion that you are actually “pressing” the button - just by flipping the way the lighting hits.

You could also use the special “inset” and “outset” border types to cut down on that last css declaration. You lose a little control over the color and light placement, but it gives a pretty decent effect. Like this…

Web Cash

a.button {
   border: #ab7f20 3px outset; }
 
a.button:hover {
   border: #ab7f20 3px inset;}

And that’s all there is to it. Change the colors to your liking, add a custom gradient image, and you’ve got a nifty little 3 dimensional button.

All it takes is some css. No need to create a bunch of fancy images, and it’s easily expandable to accommodate the text inside of your button.


Bookmark and Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

Tags: , , , ,

2 Comments to “How to Create a 3D Button Using CSS and a Gradient Image”

  1. Dave Woods said this on

    Thanks for expanding upon my tutorial. It looks nice with the gradient, although I personally have a few issues with using the outset, inset border at the moment due to the way it renders in Internet Explorer (both 6 and 7). Firefox renders it perfectly, Opera isn’t bad but IE makes a pigs ear of it so I’d probably stick to applying the borders manually at the moment.

  2. Walkere said this on

    No problem. Thanks for the inspiration =)

    I agree about the inset/outset. I don’t think it comes out as good (or as consistent), but it’s a good shortcut if you’re too lazy to figure out the appropriate colors yourself.

    It’s also a good time to illustrate what it does - so a reader that doesn’t know about those border styles can test them out and see what they do.

Leave a Reply