Sliding Door Buttons

March 8th, 2009

There are a few different ways to create styled buttons for a website. One is to create an image for the button, but the problem is you will need to create new images for each button on the site. Another option is to create a background image and overlay the button text over the image. This is a better option, but what if the Button text is too long?

The best option to handle button styles on a website is to use the sliding doors technique. Create two images that will adjust accordingly to the size of the button. This means that the user only needs to load two images and they’ll be able to view every button on your website, regardless of the size of the text.

Now, lets go over how to create these buttons using CSS. First, create the images, one for the right side and the other for the left.

Right Button

Left Button

As you can see, one is excessively long so that it’s able to stretch depending on the amount of text in the button. Now the html:

<button class="wbButton">
     <span>Search</span>
</button>

Finally, the CSS:

.wbButton, .wbButton:hover {
     border:0;
     cursor:pointer;
     background:url(/demos/button_right.png) no-repeat right;
     padding: 0 25px 0 0;
     text-align: center;
     width: auto;
     overflow: visible;
     margin: 0 0 0 10px;
}

.wbButton span {
     background:url(/demos/button_left.png) no-repeat left;
     position: relative;
     display: block;
     white-space: nowrap;
     padding: 0 0 0 25px;
     height: 25px;
     line-height: 25px;
     font-size: 13px;
}

The background image sets and positions the image, while the padding will adjust the positioning of the two images. When you create your images, you’ll probably need to adjust the line-height, height & padding. This should work in all modern browsers.

8 Responses to “Sliding Door Buttons”

  1. Marc Marc says:

    nice but i like that this guy’s design supports both normal and pressed state http://www.oscaralexander.com/tutorials/how-to-make-sexy-buttons-with-css.html

    just food for thoughts.

  2. admin rmlumley says:

    That’s a good point. This is missing a graphic change on a pressed state.

    Thanks for the feedback.

  3. Marc Marc says:

    another guy added the hover state:
    http://members.chello.nl/o.karadeniz/sndbx/even_more_sexy_button/index.html

    it seems a little too much for me now.

  4. 15个滑动门效果CSS导航菜单实例教程 | TechTrack- 科技追踪 15个滑动门效果CSS导航菜单实例教程 | TechTrack- 科技追踪 says:

    [...] Sliding Door Buttons [...]

Leave a Reply