posted on:March 4, 2008
Creating and Styling Resizable Buttons
When structuring your markup there are many paths you can choose to reach the same goal. Most of the times it’s a matter of preferences which element you wish to apply on a specific spot. I have my own personal preferences. One of them is preferring BUTTON over INPUT type="submit" elements. Why? Well W3C says it "Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities".
It’s the rendering that I am after. You can easily apply image replacement techniques to buttons and make your form look good. Most interesting feature is that BUTTON element may have content. I will use that possibility in this tutorial.
So, the goal is to create and style button that can handle variable length so there is no need for later interventions. We’ll treat button element as a container and add
some markup.
Take a look at the demo
| Download zip file
HTML
Here it is:
<button><span><em>Button text</em></span></button>
This is a valid code, and it gives us a lot to work with.
Please note, I am using two child elements instead of only one because I couldn’t get rid of some paddings that button preserved. If anyone succeeds in styling with one child element only, please let me know 🙂
Concept
This concept is probably familiar to you from various navigation tab styling techniques. We have one long background image:
This one is 550px wide. I believe that is more than sufficient for buttons 🙂
So, we apply this image as a background image of a top element (in this case SPAN), place it to top left and add some left padding. Nested element (in this case EM) have the same background image but placed to top right and with right padding. As the content text grows so will the button.
Height of the button is fixed in my example but you can use similar technique and some more markup and place the same background image to all 4 corners.
To make sure that the text is vertically centered I use line-height property.
CSS
button{ border:none; background:none; padding:0; margin:0; width:auto; overflow:visible; text-align:center; white-space:nowrap; height:40px; line-height:38px; }
Resetting button’s default styling.
button span, button em{ display:block; height:40px; line-height:38px; margin:0; color:#954b05; }
Setting child elements. Note that value of height property is different than line-height. That’s because I have 2px shadow at the bottom. Line-height vertically centers the text within the button graphic, shadow is excluded.
button span{ padding-left:20px; background:url(bg_button.gif) no-repeat 0 0; } button em{ font-style:normal; padding-right:20px; background:url(bg_button.gif) no-repeat 100% 0; }
Setting backgrounds and paddings for both child elements.
As I mentioned earlier, it would be more elegant if I could use BUTTON and SPAN only, but I couldn’ t get rid of BUTTON paddings. Is any find a solution that uses only one child element, please let me know.
Enjoyed the article?
Then don't miss our next one! Subscribe to our RSS feed or share it with your friends.
Share on FacebookTweet thisDeliciousStumbleUpon RedditSubscribe
Comments (35 Comments)
Sorry, the comment form is closed at this time.
Jon Hartmann
March 4, 2008
cssglobe
March 4, 2008
hamlet
March 4, 2008
cssglobe
March 4, 2008
Andy
March 4, 2008
Christoph
March 4, 2008
Christoph
March 4, 2008
cssglobe
March 4, 2008
Sarah
March 4, 2008
Sasa Bogdanovic
March 5, 2008
Bramus!
March 5, 2008
cssglobe
March 5, 2008
Dr.
March 5, 2008
Belén
March 7, 2008
Jonas
March 10, 2008
Giovambattista Fazioli
March 10, 2008
Andree
March 12, 2008
pozycjonowanie
March 13, 2008
sw
March 14, 2008
Web Design Liverpool
March 17, 2008
tom
March 26, 2008
Romi Dumitrescu
April 24, 2008
agriturismo lazio
May 7, 2008
Sam
June 2, 2008
cssglobe
June 2, 2008
Sam
June 2, 2008
Sam
June 3, 2008
Aarabic
June 11, 2008
Frank Ditrich
June 14, 2008
Cutebaby
July 14, 2008
Darren Lovelock
August 4, 2008
Starry Nebula
September 26, 2008
Raj
October 31, 2008
Emil
November 3, 2008
Kane
January 20, 2009