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