CSSKarma

display your <style>

designing the web since 2002

Form Design with Sliding Labels

Sliding Label banner

Please use version 1.1 of sliding labels with updated options and bug fixes at: http://www.csskarma.com/blog/sliding-labels-v2/

A few weeks ago I was reading an article on form UI by Luke Wroblewski of Yahoo!. For those who aren’t familiar with Luke, he (quite literally) wrote the book on good form design.

In the article, one certain section about placing labels inside of form fields stood out to me:

Because labels within fields need to go away when people are entering their answer into an input field, the context for the answer is gone. So if you suddenly forget what question you’re answering, tough luck—the label is nowhere to be found. As such, labels within inputs aren’t a good solution for long or even medium-length forms. When you’re done filling in the form, all the labels are gone! That makes it a bit hard to go back and check your answers. Luke Wroblewski

He brings up a good point. Generally speaking, you can look at a field that says “Tim Wright” and know that it was a field for your (my) name. But for long forms, yea, I do agree that you can forget some of the questions you answered.

For best practice, Luke talks about leaving your labels outside the form field so it’s always available to the user. I do think it’s a pretty good solution, but I think we can get a little more creative than that.

Enter: Sliding Labels

After reading that article it occurred to me that there’s no reason we can’t have the best of both worlds. I like how inline labels look, and I see Luke’s point about them disappearing as you fill out the form. But, we have jQuery, we know about progressive enhancement, and we have creative minds, so let’s build something that allows us to keep the label inline, but slide it off to the left (or up, whichever you prefer) rather than going away on click.

View demo (version 1.1)

The HTML

<form action="" method="post" id="info">
   <h2>Contact Information</h2>
   <div id="name-wrap" class="slider">
      <label for="name">Name</label>
      <input type="text" id="name" name="name">
   </div><!--/#name-wrap-->

   <div id="email-wrap"  class="slider">
      <label for="email">E&ndash;mail</label>
      <input type="text" id="email" name="email">
   </div><!--/#email-wrap-->

   <div id="street-wrap"  class="slider">
      <label for="st">Street</label>
      <input type="text" id="st" name="st">
   </div><!--/#street-wrap-->

   <div id="city-wrap"  class="slider">
      <label for="city">City &amp; State</label>
      <input type="text" id="city" name="city">
   </div><!--/#city-wrap-->

   <div id="zip-wrap"  class="slider">
      <label for="zip">Zip code</label>
      <input type="text" id="zip" name="zip">
   </div><!--/#zip-wrap-->

   <input type="submit" id="btn" name="btn" value="submit">
</form>

The only necessary items to make sliding labels (as I built it) work are the wrapper element (DIV in my case) and applying a class of “slider” to it (you can change this easily in the JavaScript).

At this point we have a pretty basic, and ugly form

slidinglabels no css

The CSS

body                       { font:12px/1.3 Arial, Sans-serif; }
form                       { width:380px;padding:0 90px 20px;margin:auto;background:#f7f7f7;border:1px solid #ddd; }
div                        { clear:both;position:relative;margin:0 0 10px; }
label                      { cursor:pointer;display:block; }
input[type="text"]         { width:300px;border:1px solid #999;padding:5px;-moz-border-radius:4px; }
input[type="text"]:focus   { border-color:#777; }
input[name="zip"]          { width:150px; }

/* submit button */
input[type="submit"]       { cursor:pointer;border:1px solid #999;padding:5px;-moz-border-radius:4px;background:#eee; }
input[type="submit"]:hover,
input[type="submit"]:focus { border-color:#333;background:#ddd; }
input[type="submit"]:active{ margin-top:1px; }

The only 100% necessary CSS in there is the position:relative on the wrapper element (DIV). The rest is basically cosmetic and you can mess with it as you see fit.

At this point you should have a pretty normal looking form with labels on top of their respective inputs.

labelslider no js

The jQuery

Now for the section everyone skipped to, provided you didn’t go straight to the demo (which I usually do).

$(function(){
$('form#info .slider label').each(function(){
	var labelColor = '#999';
	var restingPosition = '5px';

	// style the label with JS for progressive enhancement
	$(this).css({
		'color' : labelColor,
		 	'position' : 'absolute',
	 		'top' : '6px',
			'left' : restingPosition,
			'display' : 'inline',
    	               'z-index' : '99'
	});

	// grab the input value
	var inputval = $(this).next('input').val();

	// grab the label width, then add 5 pixels to it
	var labelwidth = $(this).width();
	var labelmove = labelwidth + 5;

	//onload, check if a field is filled out, if so, move the label out of the way
	if(inputval !== ''){
		$(this).stop().animate({ 'left':'-'+labelmove }, 1);
	}    	

	// if the input is empty on focus move the label to the left
	// if it's empty on blur, move it back
	$('input').focus(function(){
		var label = $(this).prev('label');
		var width = $(label).width();
		var adjust = width + 5;
		var value = $(this).val();

		if(value == ''){
			label.stop().animate({ 'left':'-'+adjust }, 'fast');
		} else {
			label.css({ 'left':'-'+adjust });
		}
	}).blur(function(){
		var label = $(this).prev('label');
		var value = $(this).val();
		if(value == ''){
			label.stop().animate({ 'left':restingPosition }, 'fast');
		}
	});
})
});

At this point, you should have a fully working sliding label form!

labelslider final

View demo (version 1.1)

Making sure everything is still usable without JavaScript is important (no matter what people say), it’s just a basic principle of progressive enhancement. Believe it or not, there are still people browsing without JavaScript (blackberry users – turned off by default). So creating the interaction in layers, as we did, will help it be past-proof.

Try it out and let me know what you think. So far I’ve used it on a password change form, so definitely let me know if you find a place to use it!

Tags: , , ,

You can leave a response, or trackback from your own site.

130 Responses to “Form Design with Sliding Labels”

« Older Comments |

  1. [...] week back I saw a great effect created by CSSKarma: input labels being animated horizontally. The idea is everything positive: elegant, practical, unobtrusive, and requires very little jQuery [...]

  2. Jani Forsey says:

    Nice article, lots of useful information. Thanks

  3. [...] CSSKarma – This is a handy blog that goes over various tutorials, including jQuery. A tutorial to note would be the Form Design with Sliding Labels. [...]

  4. Fabian says:

    Very creative! I like it.

  5. [...] is the link for the tutorial on how to create the form http://www.csskarma.com/blog/sliding-labels/.   Leave a [...]

  6. [...] Form Design with Sliding Labels By Tim Wright, January 28th, 2010 Site: CSSKarma [...]

  7. Tachyon says:

    What about a textarea?

  8. [...] Sliding Labels Sieht super aus. Überlege mir, so etwas auch in mein Theme einzubauen. [...]

  9. 3kkl says:

    oh, this is a neat idea! although it’s not useful for making forms take less space in contrast to the original idea of inline labels, since you still have to preserve the extra space for the sliding label. you can also use the title attribute or any other “onhover” method to display labels. very cool effect anyways!

  10. cvul.com says:

    Form Design with Sliding Labels…

    A few weeks ago I was reading an article on form UI by Luke Wroblewski of Yahoo!. For those who aren’t familiar with Luke, he (quite literally) wrote the book on good form design. In the article, one certain section about placing labels inside of form …

  11. [...] Kaynak var addthis_pub = ''; var addthis_language = 'en';var addthis_options = 'email, favorites, digg, delicious, myspace, google, facebook, reddit, live, more'; Etiketler: iletişim formları, Javascript, jquery, jquery effect, jquery form, jquery kayan yazı, muhteşem form örnekleri, slide Bu yazıyı paylaşın! [...]

  12. Kai Nehm says:

    creative approach, but you lost on Safari/Mac with auto-fill (which is enabled by default)

    The first label slides, and the rest gets filled in a very ugly manner and editing doesn’t make it better :(

  13. [...] regardant mes tweet, un lien SmashingMagazine faisant référence à cet article a attiré mon attention. Le principe est de déplacer les label des zone de saisie des formulaire [...]

  14. [...] Form Design with Sliding Labels By Tim Wright, January 28th, 2010 Site: CSSKarma [...]

  15. [...] is a strange post: Form Design with Sliding Labels AKPC_IDS += [...]

  16. Steve Holden says:

    I hope this doesn’t seem too negative, but since there’s room outside the fields for the labels, why not just put them there in the first place?

  17. This is really helpful information, thanks.

  18. [...] potential solution to the downsides of labels within inputs is using the sliding labels technique—proposed by CSSKarma—where the labels move to the side and remains visible when the user [...]

  19. [...] potential solution to the downsides of labels within inputs is using the sliding labels technique—proposed by CSSKarma—where the labels move to the side and remains visible when the user [...]

  20. [...] potential solution to the downsides of labels within inputs is using the sliding labels technique—proposed by CSSKarma—where the labels move to the side and remains visible when the user [...]

  21. Max says:

    very usefull.. love it

  22. [...] potential solution to the downsides of labels within inputs is using the sliding labels technique—proposed by CSSKarma—where the labels move to the side and remains visible when the user [...]

  23. [...] Form Design with Sliding Labels « CSSKarma Sliding labels adhv jQuery. Updated script te vinden op http://www.csskarma.com/blog/sliding-labels-v2/ (tags: code cool css design webdevelopment webdesign javascript jquery label) [...]

  24. [...] than the last id did.  Here the link to my version sliding label. The link to the tutorial is here. This entry was posted on Friday, March 5th, 2010 at 10:04 pm. You can follow any responses to [...]

« Older Comments |

Leave a Reply

New from the blog

Are My Sites Up? authenticjobs.com