Cross-browser inline rounded-corners input buttons and links

| 8 Comments | No TrackBacks

Why are they better than other rounded corners input buttons and links?

  • Their size change to fit the button text
  • They keep the elements' inline display so it can be used in a line of text
  • As thay are not a block elements, no float is needed so they can be centered or used inside a floated container
  • They have a pure css rollover and pushed effects (except in IE6)
  • They use only one image to improve download speed and avoid delays showing the rollover or pushed effects
  • They have simple semantic valid XHTML strict markup and valid CSS 2.1 without any hacks or tricks
  • Tested with IE5, IE5.5, IE6, IE7, IE8, FF1, FF2, FF3, Opera 9, Opera 10, Konkeror 4.2, Epiphany 2.2, Safari 3, Safari 4, Chrome 3 and Chrome 4. Available versions for Windows and Ubuntu linux.

Doctype

This example requires an xhtml-transitional doctype to work, but it can be easily adapted to xhtml strict by adding position: relative; and top: -3px; to the .button span span code to correct the vertical alignment of the text. The number of pixels in the top may vary according to the button and font sizes.

Markup

The markup of the link consists of an a tag around two span tags. The button's text is written into the inner span. Note that all tags are inline tags so the button can be used both in a line of text or in a block container. The use of the three tags is needed to support the sliding door technique and make the code work in FF2 that does not implement the inline-block display type. The a tag displays the right corner of the image and the outer span displays the left one. The elements have the same background image but they display different parts of it to create the effect of the sliding door. The background image has to be wide enough to fit the desired button's text. In this example the button can be at most 400px wide, but wider buttons can be displayed by creating a bigger image.

A button class has been applied to the a tag to give it the button look and feel. Styles for the span tags are assigned with descendant selectors.

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

The markup of the input version follows the same principles and consists of two span tags surrounding the input button.

<span class="button"><span><input type="submit" value="button text" /></span></span>

Background image

The background image has three buttons, the upper one is used to display the button in its normal state and the middle one to show the rollover effect and the bottom one is for the pushed button effect. The white space between them is necessary to avoid a bug in the vertical alignment in IE6 display unwanted parts of the image.

button

CSS

.button {
	cursor:pointer;
	text-decoration:none;
	background:url(bg-btn-corp2.gif) no-repeat right top; 
	padding-right:10px; 
	display:inline-block;
	line-height:29px;
	height:29px;
	font-size:24px;
	color:#FFFFFF;
	font-weight:bold;
}

span.button {
	vertical-align: middle;
}

.button span { 
	background:url(bg-btn-corp2.gif) no-repeat left top; 
	padding-left:10px;
	line-height:29px;
	height:29px;
	display:inline-block;
}

.button span span {
	background:transparent;
	padding:0;
	
	font-size:14px;
}

.button span input {
	cursor:pointer;
	font-family: inherit;
	font-weight:bold;
	background:transparent;
	border:0;
	padding-top:5px;
	font-size:14px;
	color:#FFFFFF;
}


.button:hover {
	background-position:right -39px;
}

.button:hover span {
	background-position:left -39px;
}

.button:active {
  background-position:right -78px;
}

.button:active span {
  background-position:left -78px;
}

Examples

In a line of text

These and links are used as inline in a line of text.


Within lists

  1. link1

In floated containers

This example shows the use of button-like links inside floated containers.
This div is left floated and contains a and a link
This div is right floated and contains and another link

Centered


No TrackBacks

TrackBack URL: http://gabriel.prat.name/cgi-bin/movabletype/mt-tb.cgi/1

8 Comments

The only thing that could be improved in this case is the use of transparent PNG as background, in order to make the button corners transparent, allowing a change of the background color. With that, would be perfect!

when I try it, the button is invisible.

I've not found the way to do it using only one image as the right image is overlapped with the left one so if the background is not opaque the image below it may become visible.

Maybe if you send me the code or an URL I may help you find the way to make it work properly.

When the field is hidden, the span should collapse to show no width as if the span is hidden as well. When there is no value, because the button is hidden, there is a default shape left over by the span and I'd like to have this collapsed. How can this be accomplished?

Your solution is the best out there that I've seen and i've looked for a good input style.

Regarding my earlier comment, a better clarification is when the input is HIDDEN it will render like this but what happens is there left and right padding shown. I'd like the padding to collapse or not appear when there's no data within the spans.

Evan, I assume you're trying to hide the input with something like a display:none style dynamically.

One simple solution to avoid the padding would be to apply the display:none to the span with the "button" class. If you have some class to apply the display:none style the code would look like this:

<span class="button hidden"><span><input type="submit" value="button text" /></span></span>

I hope I answered your question.

Gabri, Thanks for responding. I have since improved the code so when the button is hidden the entire style would disappear. On a project of mine, there could be couple of input buttons on a page and the developer will hide the buttons depending on what the user should see based on what they selected on a previous page. They would hide buttons at the server level not the client level.

So, now when (input type="submit" class="form_button" value="Submit Form") is hidden at the server level, the left over style will dissapear for all browsers. It's expected that (span class="button hidden")(span)(/span)(/span) will still remain on the page when the button is hidden, but those extra bits left over won't do anything without the (class="form_button") attached to an input button within the particular spans.

I created a demo here with my new code:
http://jsbin.com/eseki3/

Also, I included width: auto; and overflow:visible; so the extra padding left and right for input buttons within IE browser would wrap to the button label. This issue becomes apparent when a button contains a long name, but doing so also created a disappearing label issue when the page was scrolled from bottom to top for IE7. My code includes the fix for this, too. You can see a video demo of this scrolling issue here: http://screencast.com/t/MTg0NzY2Zj

Leave a comment

About this Entry

This page contains a single entry by Gabri published on October 12, 2009 7:04 PM.

Nice css/xhtml accessible tabbed navigation is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

OpenID accepted here Learn more about OpenID