Fluid length rounded corner buttons

We've all come accross a design that dictated that the form buttons should have rounded corners (especially in the web 2.0 era). The creation of fixed length rounded corner button is second nature to any self proclaimed web deveopler, but the creation of fluid (well at least fluid inlength if not in height) rounded corners is another thing all together, and that is what I'll be talking about in this entrie.

To create our illusion of fluid length rounded button I've prepared two images that we will be using.

The two button images created we will be using

But just to elaborate, I'm not talking about using fake buttons (nested <span> for instance), and then using Javascript to immitate button funcionality. As we all know, that does not degrade gracfully for folks using older browsers and screenreaders. I will be using semantic XHTML instead.

...
<form action="rounded.html" method="get">
<input type="submit" value="Submit me!" />
</form>
...

The easiest way of acheiving rounded corners in our example is to add a non-semantic empty div after the submit button and the using CSS to style everything.

input.button {
    border: 0;
    background: #fff url(button.gif) no-repeat;
    height: 22px;
    /* used to catch the buttonEnding */
    position: relative;
}

.buttonEnding {
    position: absolute;
    display: inline;
    width: 7px;
    height: 22px;
    background: url(end.gif) no-repeat;
}
...
<form action="rounded.html" method="get">
<input type="submit" value="Submit me!" />
<div class="buttonEnding"></div>
</form>
...

All this works as intended, but I'm a XHTML perfectionist, and as such that empty div for me is like nails on a chalkboard. If done so we would have to add that empty div after all buttons that we wanted to have rounded enges. Now imagine if you will a very long form with multiple buttons. Result, more scratching on the chalkboard. There has to be a better way to do such things.

And fortunately for us, there is. DOM. Here is what we'll do in plain english:

Sounds pretty straightforward. Here's how the finished javascript function looks.

function buttonEndings() {
	if (!document.getElementsByTagName) {
		return false
	}
	
	var buttons = getElementsByClass("button");
	/* loop through all buttons and attach a div */
	for (i=0; i < buttons.length; i++) {
		var div = document.createElement("div");
		div.className = "buttonEnding";
		insertAfter(div, buttons[i]);
	}
}

addLoadEvent(buttonEndings);

This seems to work in all browsers I could test in. If you happen to find a browser in wich this doesn't work as intended, please do give me a heads up through the contact form.

The way I'm doing this is not perfect. There is much room for improvement. For one thing this function does not degrade gracefully. When a user disables javascript they're greeted with buttons that have their tails clipped. One could write this a bit better and insert an empty div before and after our buttons, that will then hide all of our rounded parts if Javascript is disabled, but I will leave that implementation up to you. One could also make all four corners into their own separate images and attach them either using CSS pseudo classes :before and :after or again using Javascript and the DOM, but I guess that's a whole other story. All I wanted to acheive is what is in front of you right now. Fluid length buttons with rounded corners. And I don't even mind that the users with javascript disabled don't see the button ends, as it's purely presentational.

You might notice that I'm using a number of well known functions that were written by well known Javascript experts. Here is a list of authors and their respectabe functions width links to more inside info on the way those functions work.

If you want you can downlad the full example code here.

tags: , , , ,

Social bookmarking:

chiuaua, posted on 6th of August 2006 @ 18:26

gravatarAnd after all these hours of nasty job you get self-owned when Safari along with a couple of other Mac browsers eats this style for good :)

However, Safari is often left by side (also very annoying with his input boxes), so it is a cool thing to use...

I guess :)

Miha, posted on 6th of August 2006 @ 19:17

gravatarSo Safari chokes on this very basic DOM? I thought Safari is supposed to be the best browser for Macs...

chiuaua, posted on 6th of August 2006 @ 21:22

gravatarYes, SUPPOSED to be :)

Camino is the best Mac browser for me....Firefox engine, Mac style, cool one.

Señor Pantalones, posted on 17th of August 2006 @ 22:27

gravatarDon't fool yourself into thinking inserting an empty div via javascript is any more "perfect" XHTML than hardcoding it. It's still there, and it's still part of the dom. I'd say it's actually more intrusive since you're requiring javascript to influence page layout.

Sorry for being a jerk about this, but I found this page when googling for techniques to style form inputs, and i think this is unfortunately misleading the general audience. It's an interesting technique, but really a waste of time compared to just hardcoding an empty div. Thanks for sharing your thoughts though.

Miha, posted on 17th of August 2006 @ 22:38

gravatarThanks for the comment. But I have to disagree with you. Here I am using Javascript to plug the holes in CSS. This can easily be done using the CSS2 pseudo-element :after, but as you know, IE6 does not support this. So the main theme here was to circumvent IE's lack of support and make it work in as many browsers as possible.

Since writing this entrie I have found that this method is somewhat cumbersome. It easily breaks in more complex scenarios and as we speek I am searching for a better sollution to this problem.

Post your comment

Comments are clossed (more than one month has passed since this entrie was published). If you still wish to comment the entrie, please do not hesitate to contact me.


Recent entries

Star entries

Currently reading

Advanced PHP Programming

Worth a look

Flickr

Pediatricna klinika