Creating Slick Buttons With CSS3 Multiple Backgrounds

February 22nd, 2010

In this tutorial I’m going to show you how to make a slick button using CSS3 multiple backgrounds. The background property works the same as CSS2, with the exception of allowing you to add additional background images. If you’d like to code along, Download the source files.

Browser Support

Currently, multiple backgrounds are only supported by Safari and Chrome.

The Button Images

We’re going to need to create 3 images for our button. A left cap, a right cap, and a middle which we will repeat. The repeat will allow us to stretch the width of the button to fit our button text. Here’s what the images look like.

Left

Middle

Right

Here’s What the CSS Looks Like

The below styles are the bare minimum you require to get this working. An important thing to note is the order of the images in the background property. The order should be left, right, then middle. The first declared image is the top, the last is the bottom.

.button {
 background: url(left.jpg) top left no-repeat,
 url(right.jpg) top right no-repeat,
 url(middle.jpg) top center repeat-x;
 float: left;
 height: 40px;
}

Here’s What the HTML Looks Like

The HTML for this button is actually quite easy to code. Simply create a new <div> using the .button class. Enter your button text after the <div> tag, then close it.

<div class="button">Hello this is my button</div>

Here’s What it Looks Like

This is what the button should look like with only the essential styles defined.

Adding a Few More Styles

Let’s add a few more styles to clean up the button formating.

.button {
 background: url(left.jpg) top left no-repeat,
 url(right.jpg) top right no-repeat,
 url(middle.jpg) top center repeat-x;
 float: left;
 height: 40px;
 line-height: 40px; /* centers vertically */
 padding-left: 10px;
 padding-right: 10px;
}

.button a {
 color: #fff;
 text-decoration: none;
}

.button a:hover {
 color: #ccc;
 text-decoration: none;
}

Now let’s add the <a href> tag to our button text

<div class="button"><a href="http://yourwebsite.com">Hello this is my button</a></div>

Here’s What it Looks Like

Now that we’ve fixed the button formatting, by adding a few more styles, this is what it should look like in your browser.

LIve Demo

That’s it, that’s all. View the live demo or download the tutorial source files.

Print This Post Print This Post

Theme Forest

Share this post

  • Twitter
  • Facebook
  • Delicious

4 Comments on “Creating Slick Buttons With CSS3 Multiple Backgrounds”

nik

buttons look great. I will try themm

February 26, 2010 » 11:49 am

admin

thanks, if you have any questions just post them here

February 26, 2010 » 6:40 pm

Aaron Valentine

I certainly don’t want to come across as “that guy” (you know… Mr. super critical because he only dreams he could be as good as the people he criticizes!) but I thought you might want to know… in the example code you have above, as well as the in iPhone app I got off the App store this morning… there is oversight/typo.

In the two places where you give the .css code examples of the button composed of multilple images…

“background: url(left.jpg) top left no-repeat,
url(left.jpg) top left no-repeat,
url(middle.jpg) top center repeat-x;”

You referenced “(left.jpg)” and “top left” on the following line when I’m sure you intended for it to be… “url(right.jpg)” and “top right.”

For some reason I will put a million typos in my code, and miss every single one of them. But I can troubleshoot my buddy’s code in a second….Weird.

Good work tho, I’m really diggin the app as well as the website. Keep up the good work.

Thanks,
Aaron Valentine

May 19, 2010 » 11:52 am

admin

Good eyes! You are totally right, I missed that, thanks for pointing it out

May 19, 2010 » 1:32 pm