CSSKarma

Form Design with Sliding Labels

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

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

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

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: , , ,

Both comments and pings are currently closed.

| Newer Comments »

Comments (111)

  1. Mike Allen says:

    Wow, this is really great! I love how it just works, thanks!

  2. Jason says:

    I just created a form not 30 minutes ago without any styling. And I received @chriscoyier ‘s tweet about this.

    Perfect timing. :D

  3. This is absolutely awesome! Great for saving space, great for intuitive and sleek design, and really awesome for UX and readability – both before AND after the form is filled in.
    Just one more step towards pefecting design! Thanks a lot!

  4. bernardo cruz says:

    realy nice, but dont work on chrome!

  5. really nice great idea to create forms

  6. Wow….Cooooooooooooooool….

  7. Andy Redman says:

    @benardo works fine in chrome for me. Maybe you need to upgrade?

  8. Interesting idea.

    Unfortunately not very helpful for UX. Arguments below in order of importance.

    1/ the sliding catches eye focus – instead of focusing on clicked input element i feel forced to watch the sliding effect (seem like its made just for somebody having fun – from user perspective such form would be rejected by majority for playing tricks on user) – try to change sliding to simple immediate change of position – I bet on will feel much better. But I bet on it will be still unpleasantly catching the eye.

    2/ It’s confusing that on different parts of same input filed you get different behaviour – on part with label text inside you get cursor hand on other part edit cursor. I know its standard label behavior, but users have some experiences with text inside serving as hint that can be edited. This is simply confusing.

    3/ When clicking on label element on its outside position text becomes partialy selected, when it slides back to input field.

    Most important argument for rejecting it (in its current state) is

    0/ its novelty – something nonstandard. People are not used to it. It will slow down the processing considerably.

    But still interesting mockup. So thanks for showing this to us.

  9. Daniele Simonin says:

    Good idea :)
    i think that the code can be optimized, but the idea is awesome.

  10. gpilotino says:

    the effect is nice and I’ve never seen it before but how is this supposed to enhance usability and save space ?

  11. Sam says:

    Looks awesome. Interestingly though, you don’t seem to be using it for your comment form ;)

  12. It looks nice, but I’m afraid I’m missing the point. If you have the space next to your input fields, why wouldn’t you simly place your labels there? Putting your labels inside your input fields is most often used when you have no space beside your fields…

  13. Andrew says:

    @jaron @coda did you guys even RTFA? It’s purely a creative design solution.

  14. Dudley says:

    What about when you have dropdown list?

  15. coda says:

    @Andrew: more like a fully-functional proof of concept. What are you saying?

  16. Ted Goas says:

    This is the best form design demo I’ve seen in a while, though I see it might be limited to text inputs and textareas.

    @Coda is probably right (this effect is unnecessary and the form would achieve the same effect with the labels off to the left), it is a neat trick that doesn’t get in the way.

  17. Lonka says:

    It bugs out in Safari when autofill is activated in the browser…

  18. Tim says:

    @dudley That’s kinda funny, but I guess you probably wouldn’t use this on a dropdown form

    Actually, @Andrew has it right. It’s just a neat solution I was playing with in order to achieve the inline label look while still keeping the inputs usable when they’re filled with content. Not meant for space-saving at all, esp. with all the padding you need on the left side.

    You could easily solve that problem by fading in a background to create a tab-look when the label slides out (for example), if you’re looking to use this to save space.
    Just thought I’d share something neat I came up with.

    @Lonka You probably want to clear out the input in that case.

    Thanks for all the comments.

  19. Michael Risser says:

    Great article, and an awesome technique.

    Just because there is space available, it doesn’t necessarily mean that you should fill it with something. From a design stand-point I totally get it. The interactivity alone is worth it, fun and engaging for the user without being obtrusive.

  20. Kien says:

    This is awesome! Will be using this form for sure! jQ is so powerful and I’ve seen a lot of cool and creative stuff taking advantage of it over the past few months. I really want to learn more about it. Very cool post and thx!

  21. Great post! This will help with streamlining my contact tables even more! thanks for this…

  22. Erik Ford says:

    Now this has to be my favorite find of the day. Very simple, but incredibly cool. Thanks a lot for sharing this.

  23. What a great idea … Thanks for sharing … stumbled !

  24. Jay says:

    Interesting …

    I can use this

    Thx 4 sharing :)

  25. Laurent says:

    Nice and simple idea. Thanks for sharing ;)

  26. Jacob Yap says:

    Nice one, thanks for sharing.

  27. codemenkey says:

    the best way to solve a problem is to not create it. just saying. :) neat plugin.

  28. ASAD says:

    now i am working on this nice tips, thanks

  29. Hank_Scorpio says:

    It’s a bit OTT in my opinion. Why not just put the label in the field, and then have the same label above it? It’s not that hard and saves on all that code.

    There’s probably better uses for this.

  30. Richard H says:

    This i a really cool solution! I’ll probably never used any of this, but I like it.

| Newer Comments »

Trackbacks & Pingbacks (33)