Make fancy buttons using CSS sliding doors technique

30. April 2008 14:42 by Janko | Comments (61)

This article will show you how to create fancy buttons using CSS sliding doors technique. It is much better to use this technique than to use image buttons because you can apply the style to any link and at the same time you don't have to create an image for each button. I posted this one on Morning Break weblog but due to its popularity I decided to publish it here.

What is sliding doors technique?

The technique is very simple. If we want to have a dynamic-width button, we have to find a way to stretch it horizontally. We will accomplish this if we create two background images that will define the button: one for the left side and one for the right side - like in the example below.

  LEFT                                                                                                           RIGHT

  

Smaller, right image will slide on the top of the larger, left image (that why it is called sliding doors). The more right image slides to the left, the narrower button will be and vice versa. The image below shows the technique.

Styling the button

First, let's take a look at the HTML elements that will simulate button. We have <span> element within the <a> element. Span element contains left, wider image and text. The width of the text will determine the size of the button.

<a class="button" href="#"><span>Submit</span></a> 


Now let's take a look at the CSS code. We have a .button class that will be applied to <a> element and .button span class that will be applied to <span> element within <a> element. We also have .button:hover span that will change the font style within <span> element.  And that's all. Simple, eh? The comments in the code below describes each element.

a.button {
    /* Sliding right image */
background: transparent url('button_right.png') no-repeat scroll top right; display: block; float: left; height: 32px; /* CHANGE THIS VALUE ACCORDING TO IMAGE HEIGHT */ margin-right: 6px; padding-right: 20px; /* CHENGE THIS VALUE ACCORDING TO RIGHT IMAGE WIDTH */ /* FONT PROPERTIES */ text-decoration: none; color: #000000; font-family: Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; } a.button span {
/* Background left image */ background: transparent url('button_left.png') no-repeat;
display: block; line-height: 22px; /* CHANGE THIS VALUE ACCORDING TO BUTTONG HEIGHT */ padding: 7px 0 5px 18px; } a.button:hover span{ text-decoration:underline; }


The result will look like the examples below.

UPDATE 14.05.2008: You can see it "in action" on example page.

I've made a few more designs that you can use with the CSS code above. Each design consist of two images, like in our example. Enjoy!

  1.  
  2.   button_right_02
  3.  
  4.  
  5.  
  6.  

kick it on DotNetKicks.com

Currently rated 4.0 by 28 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: | Categories: Design
E-mail | Permalink

Comments

DotNetKicks.com on May 10. 2008 13:02 wrote:
Trackback from DotNetKicks.com

CSS sliding doors technique
ASP.NET on May 10. 2008 13:37 wrote:
Cool post.
Very useful.
Thanks.
Jens Wedin on May 12. 2008 20:08 wrote:
Nice example. I wrote a similar article as I wanted the button the enlarge when the font size is enlarged. You may check it out at

jedisthlm.com/2008/03/27/flexible-css-buttons/

Keep it up!
cease on May 12. 2008 20:24 wrote:
Can you please post a link with an example using this technique.
Chandoo on May 13. 2008 02:35 wrote:
Nice tip there, I was wondering what if the button height is dynamic? Often browser image scaling is erratic and ugly, do you know any solution to smoothly design buttons where the height could be dynamic instead of fixed in CSS like yours...
Munna on May 13. 2008 05:17 wrote:
Cool Buddy
austinwilliams.org on May 13. 2008 05:48 wrote:
Pingback from austinwilliams.org

AustinWilliams.org » links for 2008-05-13
The Bloggers Times on May 13. 2008 06:35 wrote:
Thank you. very useful.
james.a.arconati.net on May 13. 2008 08:32 wrote:
Pingback from james.a.arconati.net

links for 2008-05-13 at James A. Arconati
Janko on May 13. 2008 09:26 wrote:
Chandoo, yes, there is a way to make a button completely scalable by using "rounded corners" technique. Here are some links that might be useful:

http://www.redmelon.net/tstme/4corners/
http://tutorials.alsacreations.com/cadre/
http://www.sperling.com/examples/box/
Janko on May 13. 2008 10:03 wrote:
@Cease, you can find the example here: www.jankoatwarpspeed.com/.../

@Jens, very good article, thanks!
dmitry39 on May 13. 2008 10:34 wrote:
thx, good solution

Regards from Russia
programatik.wordpress.com on May 13. 2008 15:59 wrote:
Pingback from programatik.wordpress.com

Dica de CSS: Botão deslizante « namespace Programatik
jeremiahtolbert.com on May 13. 2008 18:44 wrote:
Pingback from jeremiahtolbert.com

links for 2008-05-13 | JeremiahTolbert.com
velcro-city.co.uk on May 14. 2008 05:32 wrote:
Pingback from velcro-city.co.uk

Links for 14-05-2008 | Velcro City Tourist Board
straighten.jp on May 14. 2008 22:35 wrote:
Pingback from straighten.jp

links for 2008-05-14 | straighten
Manu Temmerman-Uyttenbroeck on May 15. 2008 07:16 wrote:
Hi Janko,

The example you made is not working for me in Firefox and opera. Just wanted to inform you about this...
www.jankoatwarpspeed.com/.../
Janko on May 15. 2008 10:01 wrote:
Manu,

You are right about this. I didn't try it in other browsers than IE. I'll see what I can do.

Thank you for this information!
Manu Temmerman-Uyttenbroeck on May 15. 2008 10:16 wrote:
It's because IE seems to ignore a syntax error you have in your css.
You're closing the 'body' css twice with a '}'. Removing one of them makes it fine in FF and probably in all browsers.
Janko on May 15. 2008 10:31 wrote:
Manu, you are right, I missed that. IE really ignores this error. I fixed the error on the example page.

Thank you very much! Smile
abhijeetmaharana.com on May 16. 2008 20:22 wrote:
Pingback from abhijeetmaharana.com

Gwt-Ext and Number Madness | Abhijeet Maharana
önder on May 17. 2008 17:31 wrote:
good job. its useful. thanks.
zakir on May 18. 2008 01:31 wrote:
very nice buttons, thanks
Martin on May 18. 2008 09:36 wrote:
Nice article and very nice buttons. But try to test your CSS in "all" browsers like Firefox, Opera, Safari etc. In these day's a lot of people left IE and are using other browsers instead.
Keep the good work.
Yannis on May 18. 2008 13:16 wrote:
Great sample and very useful!
Rainmaker on May 18. 2008 16:24 wrote:
Great tutorial. Thanks so much.
rob lynch on May 18. 2008 17:38 wrote:
Re Lots of people leaving IE>

Have you checked your webservers stats?

I all of the sites we host, ie is over 90% of the market. (and it isn't shrinking)
still alot of ie 6 (over half of ie - go figure) but firefox is a solid single digit.
Admitidly we look after sites that cater to the business to business crowd and not the artsy/linix sect but firefox seems to have less penetration than it did a year ago.
Opra less than 1% Netscape >.1%

It doesn't stop me from trying to look after all the browsers, but the $$ investment to make a site all browser compatible isn't always there. - and yes - I test in all browsers - on all os's - interesting how going to mac or linix will effect firefox.. It would be nice if firefox has the same features (bugs) in all platforms.

Rob
PS .- I loved the article. Also the blogengine blog looks great! We used to use wordpress, but blogengine seems better and easier to customize! DOTNET Rocks.
Janko on May 18. 2008 21:53 wrote:
- Martin, you are right. I do test in all browsers, but I had a small syntax error on the example page. However, the code in the example works in IE, Firefox and Safari. I didn't test it in Opera Smile

- Rob, when I make a summary of all of my sites I host, IE (both 6&7) is in a small advantage. But, it depends on content. Maybe the stats from w3schools can help: www.w3schools.com/browsers/browsers_stats.asp

And I do agree: .NET ROCKS!
Jenny on May 19. 2008 05:34 wrote:
Oh wow. This is pretty nifty.
Jon Cage on May 20. 2008 04:32 wrote:
@Janko: You realise this was posted on A list apart about 5 years ago?

http://www.alistapart.com/articles/slidingdoors/

@rob lynch: Where do you get your figures from? I agree with Janko on this one; Depending on the site I think things sway a long way from IE these days. Just try googling for browser statistics and you'll see what I mean.
Janko on May 20. 2008 09:39 wrote:
Jon, I realize it has been posted not only on A List Apart, but on many other blogs as well. But I don't see why this should stop me to write about this? You can find at least ten articles on any subject you choose. I don't see whay this should be a problem?

Sliding doors technique is not a copyright of A List Apart. The code in this article is somethnig that I use for a long time in almost every project.

I hope you realize that you can't be unique on web, but you can do things in your own way. I just visited the article on A List Apart - although technique is similar, buttons in this article are different from tabs they desribed.
Andre on May 20. 2008 11:12 wrote:
Very nice article, thanks.
Reynder Bruyns on May 20. 2008 12:13 wrote:
Nice. It is always good to put these sort of technics under the attention of your audience.
sotec on May 20. 2008 13:26 wrote:
very nice. i will try it
Jon Cage on May 20. 2008 19:56 wrote:
@Janko: Sorry, that wasn't supposed to be a dig; I just wanted to point out that there is something very similar over there. Bringing up old articles (and expanding on them) on sites that have new readership is definitely a _good_ thing! Personally I would stick some links to any other similar techniques though on the ground that more information is a good thing. Also, it'll quite probably improve your search engine rankings if you provide links to similar articles ;)

You can be unique, but the chance to so is certainly very rare!
Jon Cage on May 20. 2008 20:12 wrote:
Weird... my post disappeared! This was what I was trying to say:

@Janko: Sorry, that wasn't supposed to be a dig; I just wanted to point out that there is something very similar over there. Bringing up old articles (and expanding on them) on sites that have new readership is definitely a _good_ thing! Personally I would stick some links to any other similar techniques though on the ground that more information is a good thing. Also, it'll quite probably improve your search engine rankings if you provide links to similar articles ;)

You can be unique, but the chance to so is certainly very rare!
Janko on May 20. 2008 21:24 wrote:
Jon, I know that wasn't supposed to be a dig. ;) I am big "fan" of linking out, I should have provide some other links here as well.

P.S. Comment moderation is turned on, so that's why you didn't see your comment after refresh. I had to turn it on bacause of spam. I try to approve all comments as soon as it is possible. Sorry for that Smile
pligg.com on May 20. 2008 21:40 wrote:
Trackback from pligg.com

Fancy CSS Buttons Tutorial
Jon Cage on May 20. 2008 22:46 wrote:
No problem Smile
Jad on May 22. 2008 05:35 wrote:
Thanks Smile
nulll on May 23. 2008 11:32 wrote:
Unofurtunatly tihs solution doesn't work with input tags...
Frown
Janko on May 23. 2008 11:40 wrote:
@nulll: That is correct. Inputs are just not the kind of elements you can style this way. You can do it only with anchor html element or (in case of asp.net server controls) LinkButton and HyperLink.
Jay on May 23. 2008 16:04 wrote:
Hi,

nice tutorial, but if you put a lot of chars in it. Than the image breaks.
(FF on linux)
Janko on May 23. 2008 16:13 wrote:
@Jay: You are right. I think the width of a larger image is around 500px. If size of the text exceedes this size, button will break. If you need realy large buttons, you'll have to make this image larger Smile
Jay on May 23. 2008 18:04 wrote:
@janko : I'm not much of an CSS expert, but if you make a left peace (rounded corners) and the middle is just a little peace of the mid of the image and let it repeat itself, you can make very long images with text on, or am I wrong here ?
Janko on May 23. 2008 18:18 wrote:
@Jay: You can create such buttons im many ways. If you mean that left and right side should be fixed, and middle image repeated to streched content - answer is yes, this could be done this way. You can also use "rounded corners" technique which is a little bit different.

Sliding doors function in this way and the goal is to have a minimum of html elements. This way you just have anchor and span in it. Any other way will need more html elements.
Jeremiah on May 24. 2008 03:11 wrote:
Very useful bit of Information, bookmarked!
niteworks on May 24. 2008 20:25 wrote:
This is a beautiful exercise in css button design. Thanks for the tip.
guest on May 24. 2008 20:29 wrote:
so lame. why use 2 images instead of 1?
epdsn.com on May 24. 2008 23:13 wrote:
Pingback from epdsn.com

epdsn’s blog post » Blog Archive » Make fancy buttons using CSS sliding doors technique
mitkinsprojects.com on May 25. 2008 10:27 wrote:
Pingback from mitkinsprojects.com

Mitkins Projects Blog :: Great blog post explaining how to make CSS sliding doors : Mitkins Projects!, The Art of Web Design
DailyCoding on June 3. 2008 08:47 wrote:
Trackback from DailyCoding.com
Links - Jun 02, 2008
Babak on June 9. 2008 20:51 wrote:
Your Header Is Cool !
Website Design on June 10. 2008 08:30 wrote:
Always works well for me when I try it!. Great tutorial; thank you.
Gryet on June 10. 2008 13:53 wrote:
Nice tutorial - just what i needed ;)

PS:
guest on May 24. 2008 20:29 >
"so lame. why use 2 images instead of 1?"

One day you will learn. Smile
jay-han.com on June 17. 2008 17:11 wrote:
Pingback from jay-han.com

Jayhan Loves Design & Japan » Button design PSD resource download
web design on June 18. 2008 07:08 wrote:
i tried the tutorial and manage to get it to work! thank you.
brian on June 26. 2008 04:16 wrote:
Great and simple to understand example, thanks!

I have a few links that I'm already using a class for the jquery plugin "thickbox" <a class="thickbox"...

how can I use this sliding door technique and still use the thickbox modal?
Janko on June 27. 2008 10:41 wrote:
@Brian: I haven't tried this but it should be easy: <a class="thickbox button"... Drop me a line if you have any problems
Brian on June 27. 2008 15:20 wrote:
@Janko: That worked just like you said. I didn't know you could stack classes like that. Thanks again for the post!
Darius on July 1. 2008 13:35 wrote:
How can I use this techinque withou float, because I can not center button in table then?

Add comment


 

  Country flag

biuquote