Tutorials

Latest Word » Tutorials » Liquid & Color Adjustable CSS Buttons
Liquid & Color Adjustable CSS Buttons

Liquid & Color Adjustable CSS Buttons

Tags:

When working on a large site with multiple buttons, it can be quite tedious to make all the buttons in Photoshop. Making future adjustments on the verbiage and colors can be also be time consuming.

By having dynamic buttons, this scenario is much easier to handle, and by having liquid and color adjustable buttons with CSS, we are able to change the verbiage and colors in a flash.

Step 1.
Create a Transparent Button

We will first start off from the button made in my previous tutorial. If you already know how to create buttons in Photoshop, you can just skim through this step.
(If you haven’t checked out my Glossy and Beveled Buttons Tutorial yet, now is a good time. Or if you’re lazy or in a rush, you can download the PSD file here.)

  1. We will first open up the file and ditch unnecessary layers like the reflection, shadow, and text. Next, select the “Button Base” layer and let’s turn down the Fill to 0% (Top right corner on the layer palette.) Also in your ‘effects’ get rid of the red gradient Overlay.
    Clean up Buttons
  2. Now we need to select the shape of our “Button Base” layer by clicking on the “Vector Mask Thumbnail” (the thumbnail on the right of the chain icon) while holding the ‘Ctrl’ Key on your keyboard. With the selection highlighted, lets create a new layer (‘Ctrl‘ + ‘Shift’ + ‘N’) and name it “Gradient Fade”. Fill it with a ‘Transparent to Black’ gradient. Tone down the gradient by making the Opacity to 50%. (Now you should have a light grey/gradient button).
  3. Select the shape of our “Button Base” layer again, and from here we need to invert the selection (‘Ctrl’ + ‘Shift’ + ‘I’). If you did this correct you should see a dotted line around the button and also around the canvas. With the inverse selection still selected, let’s now create a new layer named “Mask” and fill it with a white background.
    Mask the button
  4. Hide and turn off the visibility on the “Background” layer so now should have a transparent background. Now crop the button to 160px by 40px.
    Hide background and crop

Step 2.
Slice Buttons

At this point, we now have to slice our button into 3 pieces.
Slice A – Should be the left side of the button, save this image as btn_left.png
Slice B – Should be the center piece that stretches, save this image as btn_stretch.png
Slice C – Should be the right side of the button, save this image as btn_right.png

Slice button in 3 pieces

Step 3.
HTML / CSS

The HTML

<div class="btn"><a href="#">Add to Cart</a><span></span></div>

The CSS

body {
	margin: 0;
	padding: 10px;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
.btn {
	float: left;
	clear: both;
	background: url(images/btn_left.png) no-repeat;
	padding: 0 0 0 10px;
	margin: 5px 0;
}
.btn a{
	float: left;
	height: 40px;
	background: url(images/btn_stretch.png) repeat-x left top;
	line-height: 40px;
	padding: 0 10px;
	color: #fff;
	font-size: 1em;
	text-decoration: none;
}
.btn span {
	background: url(images/btn_right.png) no-repeat;
	float: left;
	width: 10px;
	height: 40px;
}

The beauty of this technique comes into play after all of this is done. Now that the button is liquid and transparent, to adjust colors of our buttons, now all we have to do is add a colored background to them.

.btn_addtocart { background-color: green; }
.btn_checkout { background-color: red; }
.btn_learnmore { background-color: orange; }

This is how you would implement these styles in our html.

<div class="btn btn_addtocart"><a href="#">Add to Cart</a><span></span></div>
<div class="btn btn_checkout"><a href="#">Check Out</a><span></span></div>
<div class="btn btn_learnmore"><a href="#">Learn More</a><span></span></div>

Step 4.
Cross Browser Testing

Now we all know IE is a pain. Refer to my previous PNG Transparency Fix in IE6 tutorial, and use the SuperSlight Javascript technique for this example. Here’s the direct link to www.24ways.org’s IE6 Fix tutorial

Conclusion

This idea dawned on me after I was continuously creating multiple image buttons on one of my client’s sites. As you can see, with a quick switch in the style sheet, I am able to adjust all buttons across the site.

I feel this technique still has some refining to do and has room for improvement, so I’m looking forward to anyone that has a better way or can add on to this to make this a better strategy.

Related Posts

Author Bio

My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!

You can learn more about me or Twitter  Follow me on twitter for more updates and resources!

Did You Enjoy This Post?

subscribe  Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.

+ Add Comment50 Peeps Have Spoken Their Minds...

  1. Manu

    Neat.
    I thought of this but ‘cos of IE never ventured.

    Thanks for the post

  2. Fritz

    Funny way of making buttons. You should make it with a hover effect too!

  3. Martin Ivanov

    The technique you have described is rather useful. However, it has a couple of issues that need to be fixed for the sake of semantics and to achieve a “real” button behavior:

    1. You cannot use a “div” element as a wrapper, because this makes the code non-semantic. The button is a clickable element, so you need an “a” tag with two nested elements, rather than a wrapping “div”. You may use display: inline-block for the or play with the line-height in order to make things look well and according to your design;

    2. The “button” and “input” are inline elements. Thus, your buttons should behave the same way, i.e. one should not be forced to set float: left in order to align the buttons in a single line.

    3. The presence of empty elements, as it is with your “span” is not acceptable, so I can suggest that you rewrite your code with “a” and two nested “span” elements.

    Good luck!

  4. Soh

    Thank you Martin, that was the kind of schooling I was looking for~ Ill give it a shot!

  5. Rahul

    Just superb. thanks for coding this all and putting it online for download. you rock mate.

  6. Mike Rundle

    When seeing the title of this article I initially thought your button example would be fully liquid, scaling in both the horizontal and vertical direction upon font-size increase. Any thoughts on making an updated tutorial with fully liquid buttons?

    (Hint: check how Google Analytics does it.)

  7. Soh

    Just looked at the Google Analytics button, I’m going to be dissecting their technique and try to apply it to this one. Thanks Mike, great idea!

  8. Webstandard-Team

    But if you scale the font-size, the button don’t look pretty nice anymore.

  9. Martin Ivanov

    Hello again. As a quick follow-up to my yesterday’s comments, I tried to reproduce your excellent idea of buttons with a slightly more semantic markup and “real” button behavior. Here is the download link with my view of the button:

    http://acidmartin.wemakesites.net/DownloadAdmin/click.php?id=98

    I have used an “a” tag with three nested “span” elements.

  10. wayne

    The Google Analytics button is done in for very smart layers of elements with transparent backgrounds. It’s kinda complicated just for one little button.

    I’m using the PS section of your article to make a reusable button. Thanks for the tutorial.

  11. Jack

    The technique is nice but I wouldn’t use it. It’s easy enough to use layer styles in photoshop if you need a button of a diffrent color. You don’t need to change button colors or design elements often in real life. And even if for some reason you want to be able to have dynamic generated buttons you would be better with using PHP + GD to generate them (an then cache them of course so you don redo them over and over).

    The main reason why I don’t like your technique is that it adds extra markup. I am an adept of minimalistic markup. Makes code easy to maintain, lowers bandwidth consumption, etc. One other reason I don’t like the technique is that IE6 and older doesn’t support it without using a PNG transparency hack.

  12. Brian

    I have been converting my png’s to 8 bit transparent using Fireworks. It looks pretty close and it degrades nicely in IE6. I have been using them for adding nice gradients to my backgrounds. No IE6 specific css or javascript needed. They just disappear. I tried it out with you images and IE6 just renders rounded solid color buttons.

  13. Soh

    Thank you all for the comments and great ideas.

    Jack and Martin had some very good points about semantics and avoiding extra markup.
    I have combined the 2 requests and came up with this example.

    The only downside of this is that, the button is only liquid to your background button size. In my example, its 600px. What do you guys think? Passable?

    My 2nd example is the one Mike had brought up, which was being able to vertically scale as well as horizontal. Now, my example is not usable and its clunky. But if you would like to see it, here’s the link.

    Again, this one is not usable…. I’m wondering if someone can take a stab at this?

  14. YoYurec

    hey guys! styling links not so difficult. let’s try to make real buttons with button tag.

    /me go away to test some ideas…

    btw, good idea and great tutorial!

  15. Travis

    Another great tutorial, and a really good result in the end. I’ll have to consider using this next time a client wants different colored buttons.

    One thing, I think the buttons look ever so slightly nicer if you exclude the border outside the bevel from the gloss gradient.

    This can be easily done by selecting the gloss layer, then selecting the shape of “Button Base” as you described, contracting the selection by 1 pixel, inverting that selection, and then deleting that selection.

    That really makes the bevel “pop”, and looks a bit crisper.

    But anyway, thanks again for a great write-up!

  16. Ant

    It’s good,
    I’ve modified the code and image – using image sprite (so u only have 1 image instead of 3)..so it will reduce the HTTP request and make your site run a bit faster – here’s the demo http://antzcreations.com/liquid-button/

    The only thing i notice the background position on ie6 doesn’t work.
    I’ve been trying to use other png fix as well http://gedankenkonstrukt.de/
    but no luck. The script doesn’t support “right” background position for PNG.

    Anyone?

  17. Ant

    I’ve put an active effect as well…so it looks like a button – same behaviour when you click a button.

  18. manimala

    I want a code for change the color of button in javascript..with example? If anyboby know that..Pls send it me…

  19. Jason

    This tutorial is great, however, I can only get the buttons to show up in a column, not horizontally as my website design requires. The buttons are supposed to show up like:

    home about me contact us products why is this?

    instead of

    home
    about me
    contact us
    products
    why is this?

  20. Sean Robertson

    Jason, if you float the buttons left as in Ant’s example (but without the extraneous text and clear divs) it should work just fine. I’ve been using a similar technique to this for a few years now, as can be seen on these sites:

    http://lefty.ngphost.com/ (two years old now)
    http://jackforcongress.com/
    http://stonewalldemocrats.org/

    I used to have a lot more with that technique online, but most have since been taken down (saddest thing about doing political campaign sites is they disappear after the election).

  21. david.milewski

    I’m beginner in CSS, nice example.

  22. Daniele Pignedoli

    Wow, thanks for this article!
    Its really usefull and well-written, i think i’ll use this way to make the button on my application, keeping the loading time of the page quick (only 3/1 images to load, instead of 3 images for every button)

    Thanks!

  23. Jamie

    The corners are not transparent, so, if you have the image on anything other than a white background you see white corners!

    Can anyone fix the png images so the rounded eges are transparent

  24. Daniele Pignedoli

    @Jamie: do you mean the psd example file?
    I suppose that file is just for example, you can build your own with photoshop (if you have it) or GIMP (that is free)
    I removed the corners border in the png image (and use the css relative properties, dont care if IE dont support them, the buttons look nice even withour rounded corners) in order to use just 1 div and 1 png image for my buttons

  25. Buddhist Prayer Beads

    It does work great. Thanks.

  26. Peter K.

    Great post. I wish I had the time to keep working on photoshop so my skills would not be so rusty. Seeing this tutorial should motivate me now.

  27. Scot

    Yes! Thank you Soh. I searched ‘css gradient buttons’ in google and your site was the 4th hit! I’m like ‘Hey I know that guy’ LoL, awesome :-). I’m going to give this a try, I’ll let you know how it works out. I’m foreseeing positioning issues for relative positioning, but I don’t see it being a huge issue. Thanks again!

  28. Typo3-Freelancer

    Thank you … this tutorial has me very helped.

  29. takafumi

    So COOL !!!!

  30. Faisal Khan

    Hi,

    This doesn’t work in IE6. Any workaround for that?

    I did try the technique showed in 24ways but it still didn’t work. It actually doesnt show the color.

    Many Thanks.

  31. Dan

    I want to put these in a column and have them all the same width. Is there an easy way to do this without just adding a bunch of nbsp’s to either side of the text?

  32. Chris

    To: [Mike Rundle] [wayne] [Soh]
    Re: Google Analytics button, top right corner graphic is missing?

    - yep, it’s complicated. However, using Code Snippets, this should be very useful. Especially for round the clock html+css+graphic designers :)
    - Someone mentioned “hover effects”, though it would come in handy, google doesn’t do it, so why should we, haha xD. Me thinks, when the mouse cursor changes to a pointing hand, that would be enough!

    Thanks for this article :)

  33. Bockerl

    Wow … that are very nice buttons.

  34. Vadim T

    Fantastic writeup! I have used this for some buttons but i also want to use it to to format text (bring more umph to titles). I find that if i remove the element is breaks the whole thing. Any suggestions? I am experimenting with it here http://motionunlimited.ca/faq

  35. Vadim T

    alrite nevermind i fixed it! thanks for the awesome tutorial!

  36. Nick

    http://chikuyonok.ru/2009/03/photoshop-for-webdevs-2/ — see code. Very interesting solution.

  37. Ferdy

    I absolutely love the styling of these buttons and how it looks nice also in IE. However, using this in an application does bring a few problems:

    - Since this is not a real button, you have to use javascript to submit a form. What if the user has javascript disabled?

    - Since this is not a real button, hitting [ENTER] in a field will not submit a form.

    All in all it does bring a few accessibility issues. That’s unfortunate because I love the look. I wish I could get this kind of style on real button elements, but I know from experience that IE spoils it.

    Any thoughts from you or the other commenters on how to get this style, yet still get proper form behavior without resorting to hacks?

  38. Jeremy

    Nice buttons, thanks for the tutorial!

  39. Taitems

    I can see two issues immediately:

    - You cannot click the button edges

    - You use 3 separate images when a single sprite would have worked just as well

    What I have learnt from spriting my images is that often, three 5kb images will often combine into a single 5kb image. This results in a third of a the file size and also saves two requests.

  40. KKStudios

    Nice button script.

    I edited it in a cool way that you “possibly” could add to this tut’,
    it is a hover method. It isn’t hard to add, but not many people understand
    how to do it.

    Between the “style” tags, you can add:

    .btn:hover{
    -moz-opacity:.50;
    filter:alpha(opacity=50);
    opacity:.50;
    }

    So when someone hovers the button, the button will change the transparency (in other names alpha & opacity) to whatever they want to.

    B.T.W.
    Hope this was a usefull method for adding hover WITHOUT making more images for the script.

    Note!
    All the “content (text)” in the button will also become “lighter”.

    _____________________________________________________

    Greetings KK = D (Please reply to this By my e-mail)

  41. KKStudios

    ADD TO THE LAST POST:

    You can replace/add :active , :visited :link or :hover (used) for other details to the button.

    _____________________________________________________
    Greetings KK = D (Please reply to this By my e-mail)

  42. KKStudios

    <h2>@ Taitems</h2>
    <br />
    <h4>If it weren't added more then 3 sprites, it could have not been "stretched" (there are script methods that actually can do this), so the easiest way to make a stretched button, we need to add more sprites then a base sprite.
    Or else large button text's wouldn't fit into the button without coming outside.

    _____________________________________________________
    Greetings KK = D (Please reply to this By my e-mail)

  43. random

    Just use CSS3, why care about users with old browsers.

    These buttons fallback nicely.

  44. Michael

    I found a good explanation and downloadable example of how to make a liquid anchor button at http://www.combsconsulting.com/liquid-anchor-button/index.html.

  45. Deafboy91

    Hi, how to set buttons on center align?
    Thank

  46. Scorp

    perfect tutorial and a perfect match to my requirement :)
    Thanks for the neat explanation
    ——————-
    Scorp

  47. Abhijit V. Chaore

    This technique will also make “Buttons” lighter and fast loading. Thanks for sharing.

  48. GaB

    Hi,

    Great button and great tutorial, thanks!
    Is there a way to get rid of the dirty line that appears between the left and the right part in IE7-8?

    This drive me crazy.

    Thanks!

  49. srinivas

    Thanks

  50. jhice

    Usefull ! Great, thank you.

Speak Your Mind...

Post HTMLNeed to Post HTML Code? Use Postable to post code friendly html. *Wrap all html code in <pre></pre> tags.
Post HTMLNeed to Keep Updated with Comments? Subscribe to comments now.

CSS Gallery