Pushing Your Buttons With Practical CSS3

Advertisement

CSS3 is the partially implemented sequel to the CSS2 spec we all know and love. It’s already popping up in new browsers such as Firefox 3.5, Safari 4 and Chrome. In this article, the first of the articles that explore practical (and even far-fetched) implementation of CSS3, we start by applying CSS3 to something we all have to create: buttons.

Calls to action are critical for any website, and a compelling, attention-grabbing, clickable button goes a long way toward driving that engagement. In the past, really awesome buttons needed extra markup, sliding doors or other trickery. We’ll show you here how to create nice button styles without any hacks or cheats.

Step 1: The Super-Awesome Button

Some time ago we published a post titled “Super Awesome Buttons with CSS3 and RGBa1.” Those buttons will be our first step to creating buttons that really click. We will briefly review the technique and then extend it, fixing the problems that we have encountered along the way.

Using Box-Shadow and RGBa

Our first goal when we were about to create a versatile family of buttons was to eliminate the problem that HEX-based drop-shadows have on different backgrounds. As you can see below, we can use box-shadow to create a drop-shadow, but the shadow color doesn’t work on both dark and light backgrounds. To get around that, we used a color model that is available in new browsers: RGBa.

Screenshot

RGBa works like the standard RGB model — 255, 255, 255 for white, for example — but has a fourth property that controls the alpha, or transparency, channel. In the buttons above, the gray shadow that we created for the white background is much too light against the dark background. However, using RGBa we can create a black shadow that is transparent. This allows the shadow to work against any kind of background:

Screenshot

button.awesome, .button.awesome {
	…
	-moz-box-shadow: 0 1px 3px rgba(0,0,0,0.5);
	-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.5);
	text-shadow: 0 -1px 1px rgba(0,0,0,0.25);
	…
}

That solves our box-shadow problems. All that’s left is a dash of CSS properties and a simple PNG overlay to create a finished button that can be restyled with any color, on any background, and that will work just fine.

The Amazing Border-Radius

Creating objects with rounded edges has always been a mess on the Web; after all, every object on the Web is a rectangle. CSS3 makes our lives a little easier by implementing an incredibly easy way to round off objects without sliding doors or obnoxious background elements. With these buttons, we’re using a standard background color and a PNG overlay to give them depth. We are not using a background image to round the edges.

button.awesome, .button.awesome {
	…
	-moz-border-radius: 5px;
	-webkit-border-radius: 5px;
	…
}

Currently, Webkit (Safari, Chrome) and Firefox have slightly different implementations of border-radius. Webkit will round off functional elements (like images), while Firefox will not. However, both will very cleanly round off an object and mask the background image, color or both (as with our buttons).

If you get in close, you can see that the borders behave a little strangely at the pixel level, but for the most part border-radius is a great way to bring rounded edges into your design without the mess. Here’s the complete CSS for these buttons:

button.awesome, .button.awesome {
	…
	background: #222 url(/images/alert-overlay.png) repeat-x;
	display: inline-block;
	padding: 5px 10px 6px;
	color: #fff;
	text-decoration: none;
	font-weight: bold;
	line-height: 1;
	-moz-border-radius: 5px;
	-webkit-border-radius: 5px;
	-moz-box-shadow: 0 1px 3px rgba(0,0,0,0.5);
	-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.5);
	text-shadow: 0 -1px 1px rgba(0,0,0,0.25);
	border-bottom: 1px solid rgba(0,0,0,0.25);
	position: relative;
	cursor: pointer;
	…
}

Final Touches

We’ve created the standard buttons you can see above, but another great thing about buttons that don’t use images is that we can create any color we want and with some simple class names generate an entire repertoire of buttons that we can call on any time. This is great for creating a reusable set of buttons that can be adapted to your or your clients’ style guide. Check out the color codes that we are using:

.blue.awesome {
	background-color: #2daebf;
}

.red.awesome {
	background-color: #e33100;
}

.magenta.awesome {
	background-color: #a9014b;
}

.orange.awesome {
	background-color: #ff5c00;
}

.yellow.awesome {
	background-color: #ffb515;
}

These simple classes allow us to quickly call on the button we want to get the action we need users to take. We’ve used similar methods in the buttons we’ll walk through in the next few sections.

Our last step is sizing. Without a sliding-doors situation our button size is only limited by the size of our overlay image (and in future buttons, not even by that). We’ll use a few classes (small, medium and large) to create sizes that we can call in different situations.

Screenshot2
The buttons in various sizes: an overview.

And that’s it! A scalable, easily configured set of buttons that works in all browsers (through graceful degradation) and will get you the impact you want. In the next few sections we’ll build on these buttons to show you how CSS3 can push these even further in some interesting directions.

Screenshot3

See the live demo »4

We’ve created a live demo page for these buttons; it’s in a playground, where we create small side projects and cool toys. We’ll be linking to the playground examples throughout this post and the rest of our series.

A note about compatibility: true to form, IE8 does not support border-radius. At ZURB, we’ve adopted a “graceful degradation” paradigm, so these corners will be square in IE. In our experience, the additional development cost for elements like this is not worth it, and we rely on these properties to cleanly degrade and not damage the layout or readability. These buttons are no exception: they are still as clickable in IE as anywhere else, just not quite as pretty.

Step 2: Radioactive Buttons

Now that we’ve gotten our feet wet with CSS3, let’s go in a different direction. For a recent client project, we wanted to create a stylized, attractive and eye-catching call to action for the home page. We came up with the idea of a radioactive button, a button that actually pulses on the page for more attention.

Screenshot5

Kitschy? Maybe a little. But despite its impracticality, for some websites and clients, the “Wow” factor—that feeling the user gets when they arrive at a website and say, “Oooh, ahhh!”—can’t be ignored. Where we only used to have , thanks to some tricks we’ve already covered (and one we haven’t), we can move beyond that terrifically obnoxious blink tag and explore a whole new range of opportunities to animate using pure CSS.

Animation: No JavaScript Required

The secret to the radioactive button is in a new CSS3 property called animation (or, as it currently exists, -webkit-animation and -moz-animation). Our radioactive buttons are structurally and stylistically identical to the buttons in step 1, but with one important change. In lieu of a static drop-shadow, we’ve used the box-shadow property to create a glow around the button. With “glowing shadow” added, the next step is to use CSS3 animation to dim and brighten it.

Creating the Animation

The first step in our radioactive awesomeness is to create a timed event that modifies existing CSS properties. Check out this odd-looking property:

@-webkit-keyframes greenPulse
{
	from {
		background-color: #749a02;
		-webkit-box-shadow: 0 0 9px #333;
	}
	50% {
		background-color: #91bd09;
		-webkit-box-shadow: 0 0 18px #91bd09;
	}
	to {
		background-color: #749a02;
		-webkit-box-shadow: 0 0 9px #333;
	}
}

Here’s how the code works:

  • @-webkit-keyframes tells the browser that we’re defining an animation based on keyframes, or points in the animation to where properties will change. The browser then knows to smoothly transition between those frames.
  • greenPulse is the name of the animation. We need this to attach to objects later on.
  • from { … } defines the starting point of the animation; in our case, a certain background color for the button and a box-shadow color (particularly, one that disappears into the background).
  • 50% { … } means that one change happens halfway through the animation.
  • to { … } defines the last frame of the animation. Note that animations always return to their from state—they don’t stop on the last frame. This means that a smooth animation needs to begin and end with the same properties and values.

All right, we’ve created the animation. It takes an object and over a set time changes the box-shadow color to green and then back to gray. Now, we just have to make it active by applying the animation to our buttons.

.green.button {
	…
	-webkit-animation-name: greenPulse
	-webkit-animation-duration: 2s;
	-webkit-animation-iteration-count: infinite;
	…
}

Pretty cool, isn’t it? -webkit-animation has many potential uses and, when combined with some very simple JavaScript (like onclick), you can create very rich effects and interaction without resorting to a JavaScript effects library.

Screenshot6

See the live demo »7

Check out the live demo of radioactive buttons in our playground. For maximum awesomeness, check it out in Safari 4; radioactive buttons use some Webkit-specific properties that aren’t implemented well in Chrome quite yet.

Nice, Simple, User-Friendly Buttons, Google-Style

Our last example was inspired by some recent changes made to one of the most trafficked pages on the Web: the Google search page. In addition to enlarging the search field and text size, Google also rolled out some new buttons for Webkit-based browsers (notably its own, Chrome).

These buttons incorporate everything that makes our buttons in this article great, with one cool trick: no images at all. Google has used a new property called -webkit-gradient to generate a gradient using only CSS.

Screenshot8

Using CSS Gradients

Gradients in CSS are cool because they are a CSS function, much like url(), which means that we can use gradients anywhere images go, including in the background or in borders (don’t worry, we’ll get to border-image in a later post). Here’s how gradients work:

.g-button {
	…
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(255, 255, 255)), to(rgb(98,202,227)));
	…
}

Let’s break this down:

  • linear means this is a linear gradient, as opposed to radial. This changes the color across a single axis, rather than in concentric circles.
  • 0% 0%, 0% 100% are coordinates. We’re saying that the gradient begins at 0% X, 0% Y and ends at 0% X, 100% Y: top-left corner to bottom-left corner. We could use top-left, bottom-left just as well.
  • from(rgb(255, 255, 255)) is the starting color for the gradient. If the syntax looks familiar, it should be: it’s very similar to the syntax for @-webkit-keyframes.
  • to(rgb(221, 221, 221)) is the ending color. While we have only used RGB here, you can see how gradients could get really interesting if we use RGBa and are able to control the opacity of points on the gradient.

The rest of the button is ordinary: padding, border-radius and all the fun things we’ve gone over so far. Gradients is the star here, though, and it has thousands of potential uses. Here, we’ve created (albeit in Google’s footsteps) rich, engaging buttons that are 100% CSS, no images required.

Screenshot9

See the live demo »10

You can see a live demo of these buttons in the playground, or simply visit the Google search home page in a Webkit browser.

Coming Up: Better, Stronger, Faster Design

This is the first of the articles that take advantages of the powerful new features coming in CSS 3. In the following posts, we’ll show you how CSS3 not only can make implementation faster and easier, but can help you really fly off the rails and push CSS to its breaking point while creating impressive visual effects.

References and Resources

(al)

Footnotes

  1. 1 http://www.zurb.com/article/266/super-awesome-buttons-with-css3-and-rgba
  2. 2 http://www.zurb.com/playground/super-awesome-buttons
  3. 3 http://www.zurb.com/playground/super-awesome-buttons
  4. 4 http://www.zurb.com/playground/super-awesome-buttons
  5. 5 http://www.zurb.com/playground/radioactive-buttons
  6. 6 http://www.zurb.com/playground/radioactive-buttons
  7. 7 http://www.zurb.com/playground/radioactive-buttons
  8. 8 http://www.zurb.com/playground/google-buttons
  9. 9 http://www.zurb.com/playground/google-buttons
  10. 10 http://www.zurb.com/playground/google-buttons
  11. 11 http://answers.polldaddy.com/poll/2330515/
  12. 12 http://answers.polldaddy.com
  13. 13 http://css-tricks.com/rgba-browser-support/
  14. 14 http://webkit.org/blog/138/css-animation/
  15. 15 http://www.zurb.com/article/221/css3-animation-will-rock-your-world
  16. 16 http://www.css3.info/webkit-introduce-more-new-features/
  17. 17 http://developer.apple.com/safari/library/documentation/InternetWeb/Conceptual/SafariVisualEffectsProgGuide/Introduction/Introduction.html
  18. 18 http://www.the-art-of-web.com/css/border-radius/

↑ Back to top Tweet itShare on Facebook

ZURB is a close-knit team of interaction designers and strategists that help companies design better products & services through consulting, products, education, books, training and events. Since 1998 ZURB has helped over 75+ clients including: Facebook, eBay, NYSE, Yahoo, Zazzle, Playlist, Britney Spears, among others.

Advertisement
  1. 1

    Just amazing! CSS3 gives so much freedom!

    0
  2. 102

    Thiago A. Villa Menezes

    January 25, 2010 7:11 pm

    Maaaaan! What was that?! Is it only me or someone else out there is also stunned? God, I’m acctually feeling excited right now. I haven’t seen a so useful post for… like… ages

    It is sooooo good to see how far we’ve come, how easy our lives are becoming as you designers and us developers. Old times when we had to work rounded corners with JS, sprites and all the fuz.

    Yet, there’s the stone in our shoes, Internet Explorer. But, let’s be bright!

    Sometimes, clients and users won’t even guess what’s happening. For them, the buttons are pretty and work — and that’s the point, after all, buttons that really cool, but work! If they get to their destination, it’s alright.

    That said, I always make sure people around me that just user Internet in the daily basis get to know what’s happening. Not with the technical jargon, but in simple words. And guess what’s the expression? “Oooh, so it was that hard and now just that? And it’s just a bit of code? Like there was the K7 tape, then the CD now the pen drive?” Yeah, evolution, folks.

    I’m so happy *.*

    0
  3. 203

    Thiago A. Villa Menezes

    January 25, 2010 7:23 pm

    Yeah, I know what you say. Here in Brazil, it’s not different. Believe you or not, I come accross people saying “(…) but Internet Explorer is the best, it comes with Windows, aren’t we supposed to be using it?”.

    See? I don’t know out there, but here: 1) they think they should use the software only because it’s there, ready, installed, just open it and run; 2) it’s “the best” although they haven’t even tried others and aren’t open to try; 3) “it comes with Windows”, that’s why I install Chrome or Firefox in every Windows PC that I found running Internet Explorer LOL.

    Here we are still engaging technology in people’s daily lives, people like the baker next corner and the lady who cleans the street. It’s part of our digital inclusion program. So it’s an ongoing proccess and I make my part by showing the options, opening discussons on hot topics, it’s very democratic. The most rewarding thing is when they start telling their community about what they’ve learned. Sooo rewarding!

    In my college, we got our IT staff to install Firefox on every single computer on campus. People are using it, and noticing the difference!

    0
  4. 304

    I’ve been working on an IPOD touch app and was FORCED to use the web-kit engine and found out how to do the buttons you describe here and then all of a sudden I had an orgasmotic reaction. WOW! what a great way to design a page full of buttons the EASY way. Come on IE developers, wakeup and smell the CSS3 buttons, I mean roses… It’s about time you IE developers lead or get out of the way…..

    IE sucks bigtime in this area, if you can invent AJAX (httpxmlrequest) you can implement CSS 3!

    0
  5. 405

    Nice post …. Keep the CSS3 Tutorials Coming :D

    0
  6. 506

    nice post !

    0
  7. 607

    There’s one important thing – to get the same look in Safari/FF/Chrome you have to play with :-moz-focus-inner. The guys from zurb.com are doing so in their global.css. Otherwise you can spend long time wondering why using the very same css you still do not have the same look under those browsers while they do.

    0
  8. 708

    Just amazing! CSS3 gives so much freedom! But i got a small question, CSS3 supports all browsers but what about IE browsers? that style does not support IE. Do you have any solution for that?

    0
  9. 809

    And here’s my variation of buttons: http://www.cybernoxa.pl/projects/css3/buttons/ – what important – thre’s a focus state, which zurb buttons seems to miss. Yeah, I know ;]

    0
  10. 910

    Is realy cool

    0
  11. 1011

    its very beautiful

    0
  12. 1112

    Thanks a lot!

    0
  13. 1213

    I find that you can send a link to your WordPress blog so your catoncts can see it for the first time and after that, all that person needs to do is to hit the RSS 2.0 button on the bottom of the page to allow it to hit’ their news reader such as Y-mail, Y!profiles or My Yahoo (they should be logged into the page for the RSS to find its way there) (this is what I used and it popped into my page and I accepted it for future updates. You just need to let your catoncts know where your site is at and the rest is up to them to acquire the link the way they want to.

    0
  14. 1314

    Nice Article like it………

    0
  15. 1415

    Hi all!

    Having problems implementing Radioactive Buttons with my website (alleagues. com). Any chance of more detailed instructions please? Awesome effect!

    Best regards & thanks.

    0

↑ Back to top