Make fancy buttons using CSS sliding doors technique

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.

105 Responses

  1. ASP.NET 10. May 2008 at 13:37

    Cool post.
    Very useful.
    Thanks.

  2. Jens Wedin 12. May 2008 at 20:08

    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

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

    Keep it up!

  3. cease 12. May 2008 at 20:24

    Can you please post a link with an example using this technique.

  4. Chandoo 13. May 2008 at 02:35

    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…

  5. Munna 13. May 2008 at 05:17

    Cool Buddy

  6. The Bloggers Times 13. May 2008 at 06:35

    Thank you. very useful.

  7. Janko 13. May 2008 at 09:26

    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/

  8. Janko 13. May 2008 at 10:03

    @Cease, you can find the example here: http://www.jankoatwarpspeed.com/examples/CSS-Sliding-doors/

    @Jens, very good article, thanks!

  9. dmitry39 13. May 2008 at 10:34

    thx, good solution

    Regards from Russia

  10. Manu Temmerman-Uyttenbroeck 15. May 2008 at 07:16

    Hi Janko,

    The example you made is not working for me in Firefox and opera. Just wanted to inform you about this…
    http://www.jankoatwarpspeed.com/examples/CSS-Sliding-doors/

  11. Janko 15. May 2008 at 10:01

    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!

  12. Manu Temmerman-Uyttenbroeck 15. May 2008 at 10:16

    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.

  13. Janko 15. May 2008 at 10:31

    Manu, you are right, I missed that. IE really ignores this error. I fixed the error on the example page.

    Thank you very much! :)

  14. önder 17. May 2008 at 17:31

    good job. its useful. thanks.

  15. zakir 18. May 2008 at 01:31

    very nice buttons, thanks

  16. Martin 18. May 2008 at 09:36

    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.

  17. Yannis 18. May 2008 at 13:16

    Great sample and very useful!

  18. Rainmaker 18. May 2008 at 16:24

    Great tutorial. Thanks so much.

  19. rob lynch 18. May 2008 at 17:38

    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.

  20. Janko 18. May 2008 at 21:53

    - 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 :)

    - 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 [b]w3schools[/b] can help: http://www.w3schools.com/browsers/browsers_stats.asp

    And I do agree: .NET ROCKS!

  21. Jenny 19. May 2008 at 05:34

    Oh wow. This is pretty nifty.

  22. Jon Cage 20. May 2008 at 04:32

    @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.

  23. Janko 20. May 2008 at 09:39

    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.

  24. Andre 20. May 2008 at 11:12

    Very nice article, thanks.

  25. Reynder Bruyns 20. May 2008 at 12:13

    Nice. It is always good to put these sort of technics under the attention of your audience.

  26. sotec 20. May 2008 at 13:26

    very nice. i will try it

  27. Jon Cage 20. May 2008 at 19:56

    @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!

  28. Jon Cage 20. May 2008 at 20:12

    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!

  29. Janko 20. May 2008 at 21:24

    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 :)

  30. Jon Cage 20. May 2008 at 22:46

    No problem :)

  31. Jad 22. May 2008 at 05:35

    Thanks :)

  32. nulll 23. May 2008 at 11:32

    Unofurtunatly tihs solution doesn’t work with input tags…
    :(

  33. Janko 23. May 2008 at 11:40

    @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.

  34. Jay 23. May 2008 at 16:04

    Hi,

    nice tutorial, but if you put a lot of chars in it. Than the image breaks.
    (FF on linux)

  35. Janko 23. May 2008 at 16:13

    @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 :)

  36. Jay 23. May 2008 at 18:04

    @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 ?

  37. Janko 23. May 2008 at 18:18

    @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.

  38. Jeremiah 24. May 2008 at 03:11

    Very useful bit of Information, bookmarked!

  39. niteworks 24. May 2008 at 20:25

    This is a beautiful exercise in css button design. Thanks for the tip.

  40. guest 24. May 2008 at 20:29

    so lame. why use 2 images instead of 1?

  41. DailyCoding 3. June 2008 at 08:47

    Trackback from DailyCoding.com
    Links – Jun 02, 2008

  42. Babak 9. June 2008 at 20:51

    Your Header Is Cool !

  43. Website Design 10. June 2008 at 08:30

    Always works well for me when I try it!. Great tutorial; thank you.

  44. Gryet 10. June 2008 at 13:53

    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. :)

  45. web design 18. June 2008 at 07:08

    i tried the tutorial and manage to get it to work! thank you.

  46. brian 26. June 2008 at 04:16

    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?

  47. Janko 27. June 2008 at 10:41

    @Brian: I haven’t tried this but it should be easy: <a class="thickbox button"… Drop me a line if you have any problems

  48. Brian 27. June 2008 at 15:20

    @Janko: That worked just like you said. I didn’t know you could stack classes like that. Thanks again for the post!

  49. Darius 1. July 2008 at 13:35

    How can I use this techinque withou float, because I can not center button in table then?

  50. Egor 14. July 2008 at 10:33

    Nice work.

    In <em>some</em> cases, it could be simplified further through the use of just one long image, and <code>background-position</code>.

  51. Thomas Herold 20. July 2008 at 20:18

    Great technique….thank you for working this out. I used your code and thought that there is no reason to use 2 pictures. You can use just one long picture and it works well.

    Cheers

    Thomas Herold
    CEO Dream Manifesto
    http://www.dreammanifesto.com

  52. Janko 22. July 2008 at 20:13

    Thomas, I’m glad you found it useful. And, yeah, doing it with only one image actually works great!

  53. Cécile 3. August 2008 at 11:46

    Great and very clear explanation!
    Thank you for taking the time to write this small tutorial.
    Now I still need to find out how to make images shift so that I can use different colors in the buttons…
    But this already was a GREAT help!

    Cheers.
    Cécile

  54. Eazzy | Web Design 8. August 2008 at 17:11

    thanks, nice buttons, very useful.

  55. Elaine Mingus 13. August 2008 at 22:06

    Do you have a rollover effect that works with this button?

    Thanks

    Elaine Mingus

  56. Janko 19. August 2008 at 12:55

    Elaine, I didn’t have a time to extend it with a rollover effect :$, but simple :hover might help

  57. Virtual 26. August 2008 at 15:26

    Thanks for share us the great buttons!

  58. Olivier 27. August 2008 at 21:15

    Merci beaucoup !

  59. ASP Net Web Development 29. August 2008 at 11:18

    Cool post…. nice explanation thanks…

  60. busby seo challenge 30. August 2008 at 01:14

    nice one post..thanks…great tips
    :D

  61. kabonfootprint 8. September 2008 at 05:10

    Cool post…. nice explanation thanks…great one… :D

  62. laptop cases 21. September 2008 at 14:06

    nice explanation thanks…great one..8o|

  63. voyance 24. September 2008 at 07:31

    Very nice article, thanks.

  64. internet fax 3. October 2008 at 13:52

    hot hot hot. great idea, i think ill have to use it.

  65. benz 6. October 2008 at 06:13

    I tend to do this with an external DIV with an inner A, works the same way – I like both these approaches because you don’t need much HTML markup for it to work.

    THE PROBLEM with both these approaches though is that the outer element is floated to prevent it from stretching across the width of its container, as a result you can’t just turn any old link into a button as you will break the link out of the document flow and find it floating over to the left somewhere.

    Has anyone found a neat way to resolve this without adding extra markup (as you can just wrap it all in another div etc and control that)?

  66. tefly 10. October 2008 at 19:33

    wow great article. very useful. thx (K)

  67. Bob S. 29. October 2008 at 16:57

    i tried and it work well, thanx very good tip :) and now i can do buttons more better.

  68. Msn Nickleri 30. October 2008 at 21:05

    VeRry cooll. thankss man. ;-)

  69. Caroline 4. November 2008 at 16:06

    Thanks very much – I have been looking around for a simple tutorial like this to follow, and the code included makes it perfect :D

  70. VJPQ Directory 12. December 2008 at 12:54

    I just love the tutorial, very simple indeed.
    Thanks a lot man!, there’s a ton of great things here.

  71. Cameron Mauch 19. December 2008 at 22:29

    How do you get this method to work with IE6 and the PNG transparency bug fix using the filter:progid:DXImageTransform.Microsoft.AlphaImageLoader CSS trick? The right image placement gets broken. Thanks!

  72. Kazi 22. December 2008 at 14:52

    the codes works fine as long as the text is within the limit. And also you could complete the article with full menu implementation but the basic things works fine.

  73. Cameron Mauch 23. December 2008 at 20:29

    For the IE6 png fix, I tried using DD_belatedPNG, and it works with this method. The only issue is the images are loaded every time, never cached. Side effect of how the script works internally.

    Is there a way to get opacity to work with this method on IE6? I tried using the "filter: alpha(opacity=XX)" method, but it doesn’t work. Any ideas?

  74. Janko 31. December 2008 at 17:01

    Cameron Mauch: I really can’t think of anything at the moment. That’s exactly why I dropped the support for IE6 :)

  75. cuocthiseo 6. January 2009 at 09:55

    Great tips, it make my site more attractive,
    Thanks a lots:)

  76. Oyunlar 9. January 2009 at 20:03

    Thanks for tutorial..

  77. marco 29. January 2009 at 16:28

    great and useful post, simple and really straightforward. since as the sayin’ goes "sharing is caring", let me say THANKS for caring :-)

    ciao!

  78. cherooo 31. January 2009 at 01:57

    Thank you for your post its very nice

  79. rumax 6. February 2009 at 09:26

    Very usefull for me, Thanks

  80. cod4 hacks 9. February 2009 at 08:59

    Thanks for the tutorial, the button looks great.

  81. gagner de l'argent 13. February 2009 at 01:42

    very nice Tutorial thank you very match

  82. Lida Dai Dai Hua Jiao Nang Seo Yarışması 15. February 2009 at 15:07

    thank you

  83. k07ka 21. February 2009 at 19:49

    Hello!

    I’d really like your work and want to use one of your buttons in a simple script. But have some problems (probably because I’m a noob :}), my submit buttons must do some onClick stuff… onClick="if(password1.value!=password2.value) { alert(\’Password missmatch!\’); return false; } else create.submit();"

    Hmmm, can you help me? Probably I should do some JS function… :S

    Thanks in advance and again good work! ;)

  84. Simulateur de pret 26. February 2009 at 00:53

    This is a very nice tutorial. Thanks a lot for the good work.

    Cheers

  85. zenaida 6. March 2009 at 10:34

    amazing! it is very informative and useful. it will be a great help to everybody. keep up the good work

  86. jose 17. March 2009 at 07:38

    How do you align the button? I would like to center the button. Thanks again for providing this css code.

  87. Kia 25. March 2009 at 16:47

    Nice guide. I’m gonna add these buttons to my clients webpage!

  88. Robert Lee 10. April 2009 at 20:09

    when i place your a href button between the p tag, the closing p tag doesn’t break to the next line.

  89. clifford 11. April 2009 at 10:28

    very nice tip. I’ll use it. thanks.
    P.C

  90. Mike 29. April 2009 at 17:03

    how can i center the button? when i try, it stretches the width of the screen?

  91. Baden 1. May 2009 at 19:05

    Hi, Thanks for this post.
    I would like to adapt it to my needs.
    In the Html I don’t want to create a new buton
    <a class="button" href="#"><span>Submit</span></a>
    But I would like to use the css for an existing button:
    <input type="submit" value="<?php echo $VM_LANG->_(‘BUTTON_LOGIN’) ?>" class="button" name="Login" / >

    Is it possible to do that? Can you write me the code to insert? I spent 1 hour on it without any success…

    Thanks

    Baden from France

  92. Tiffany 2. May 2009 at 09:22

    Thanks so much for this article…it’s saving my life. I find it easier to understand then the A List Apart article since I’m totally wet behind the ears. I’m now following you on twitter. Keep up the great work!

  93. Suzanne 23. May 2009 at 00:35

    Perfect! Just what I needed – nice and simple too for a newbie like me :D

  94. matthew 4. June 2009 at 02:35

    Great article, thanks for the quick fix. Will definately be doing this. How about mouse over with css sprites?

  95. Shay Howe 22. June 2009 at 23:11

    This a great tutorial, and the sample buttons you have provided are great as well.

    One thing I might recommend if you have time is to add a sample, with CSS, on how to do the sliding doors vertically. It’s really the same basic principles however I haven’t seen any tutorials for it outline. Just a thought..

    Thanks!

  96. Parmesh 26. June 2009 at 14:36

    This is a very useful tutorial. But when we work with Asp.net, how we can implement this for server buttons?
    Can you help me ?

  97. Nikita Sumeiko 8. July 2009 at 18:43

    Well done, Janko!
    Your method is very nice and working for me.
    But usually we use input tag to submit some data in the form element, therefore I have posted a way, which is based on my own experience, on how to code cross-browser sliding door input button.
    Please, have a look:
    http://www.manakor.org/xhtml/cross-browser-sliding-door-input-button/

    When I started to compare your method and mine, I see that they are not the same. And have you tested your button in all the major browsers?
    I’ve got a lot of css code, which displays the button in all the browsers in the same way. [b]And I thinks we shouldn’t handle it, because it gives us a great result![/b]

  98. Sergio Zambrano 22. July 2009 at 06:51

    My button, let’s call it the "sergio zambrano" submit button because I haven’t come up with a better name yet, hehe, it’s the same than this one, but with no extra markup, with a single image, and working for a SUBMIT button. Same transparent, same elastic, less code.
    You can see it in the comment forms of my website at http://seosumo.com

  99. Clare 10. August 2009 at 21:51

    Brilliant!
    Elegant!
    Thank you!

  100. Madhu 20. August 2009 at 15:44

    It was very useful to me.
    Thank you for the post

  101. STK 23. August 2009 at 18:38

    nice example !!

    thank

  102. Rock 28. August 2009 at 18:12

    You had mentioned in the comments that this could be done with only one image.
    How would you do it?

  103. Craig 5. October 2009 at 17:05

    @Rock

    Using 1 image would be easier in my opinion. I use this technique all the time for buttons, and I always use 1 image.

    Instead of chopping the right hand side of the image off, leave it on and simply add "background-position:right;" to the "span" selector.

    So, it would look this this;

    [quote]
    a.button span { background: transparent url(‘button.png’) no-repeat; }
    a.button span { background: transparent url(‘button.png’) no-repeat RIGHT; }
    [/quote]

  104. tracy 26. October 2009 at 05:56

    nice example !!

    thank

  105. Silviya 15. November 2009 at 08:53

    Very useful article! Thanks!