Professional Documents
Culture Documents
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
LegendThemes.com Home Our Themes About Support Contact Our Blog. Themes, Design, News, WordPress.
1 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
<p><input id="acceptTerms" name="acceptTerms" type="checkbox" /> <label for="acceptTerms"> I agree to the <a href="">Terms and Conditions</a> and <a href="">Privacy Policy</a> 29 </label> 30 </p> 31 32 <p> 33 <button id="registerNew" type="submit">Register</button> 34 </p> 35 </fieldset> 36 37 </form> 38 </div> Note the input types. Instead of only using the usual name and password I also added tel and email. Most browsers dont render anything different here, but it makes a big difference for the user experience on Safari mobile (iPhone and iPad). The key difference is a rearranged keyboard to focus on the type of input. Email adds the @ sign as well as a . button. Also the auto-correct changes so it doesnt try to split domain names into multiple words.
19 20 21 22 23 24 25 26 27 28
</p> <p> <label for="password">Password</label> <input id="password" name="password" class="text" type="password" /> </p>
It looks like a minor difference but it is very frustrating to enter an email address into a text input on the iPhone. Your users will thank you for paying attention to the details. Older browsers that dont understand HTML 5 forms will just fall back to input type=text. Which is just fine for our purposes. Also each field has class of text so that I can style them with the CSS input.text selector. Even though they arent all text fields, I do want them to look the same. You could use CSS3 selectors instead, but getting that to work in IE is beyond the scope of this article.
2 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
3 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
25 #registration a { 26 color: #8c910b; 27 text-shadow: 0px -1px 0px #000; 28 } 29 30 #registration fieldset { 31 padding: 20px; 32 } Here we are using CSS3 gradients to set the background of the container. We are using three different background declarations to accommodate different browsers. First we set a background color of #2d2d2d that all browsers will understand, then we overwrite it for -webkit and -moz to use background gradients instead. Since Internet Explorer doesnt understand gradients it will ignore them and use the solid color specified first. You can use this website to generate your own CSS gradients. Then for the rounded corners we are adding -webkit-border-radius, then -moz-border-radius for the browsers that support it, then adding standard border-radius for when the official spec is adopted in the future (hopefully by IE9). For the links we are adding a default color, then more importantly adding text-shadow (not supported in IE). The syntax for the text-shadow is this: 1 text-shadow: x y blur color;
1 text-shadow: 0px -1px 0px #000; So we are using that to add a black vertical shadow that gives the text an indented look.
4 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
rgb(215,215,215) 54%, rgb(173,173,173) 100% ); background: -webkit-gradient( linear, left bottom, left top, color-stop(0, rgb(225,225,225)), color-stop(0.54, rgb(215,215,215)), color-stop(1, rgb(173,173,173)) ); color:#333; text-shadow:0px 1px 0px #FFF; -moz-box-shadow: 0px 1px 0px #777; -webkit-box-shadow: 0px 1px 0px #777; box-shadow: 0px 1px 0px #777; }
Here we are adding background gradients again, remember to specify the default for older browsers. The rounded corners also give the fields a nice pill shape. The new part is that we used a box-shadow to give it a recessed look. The syntax is the same for box-shadow as it is for text-shadow. So box-shadow: 0px 1px 0px #777; is a light colored shadow, with no blur, that is down 1px. This combined with a dark stroke gives it the look below.
Adding Icons.
Next we will add icons to each field so that it is more easily identified at a glance. With CSS3 we are able to use multiple backgrounds to have both a gradient and an image. First create a sprite image of all your icons combined into one file.
5 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
This will decrease HTTP requests, simplify your markup, and improve page load. An example of the icons I used are on the right. You will want to replace the earlier code for the input field backgrounds with this new code. 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 background: #ddd url('img/inputSprite.png') no-repeat 4px 6px; background: url('img/inputSprite.png') no-repeat 4px 6px, -moz-linear-gradient( center bottom, rgb(225,225,225) 0%, rgb(215,215,215) 54%, rgb(173,173,173) 100% ); background: url('img/inputSprite.png') no-repeat 4px 6px, -webkit-gradient( linear, left bottom, left top, color-stop(0, rgb(225,225,225)), color-stop(0.54, rgb(215,215,215)), color-stop(1, rgb(173,173,173)) );
We are able to add multiple backgrounds by separating each one with a comma. To accommodate the new icons we will also need to change the right padding to 30px. 1 padding: 7px 8px 7px 30px; Then specify the background position for each field individually so that it will display the correct icon. The exact values will depend on the icons that you use. Note that the order of the icons in the sprite does not have to match the order of the input fields. 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 input#email { background-position: background-position: } input#password { background-position: background-position: } input#name { background-position: background-position: } input#tel { background-position: background-position: } 4px 5px; 4px 5px, 0px 0px; 4px -20px; 4px -20px, 0px 0px; 4px -46px; 4px -46px, 0px 0px; 4px -76px; 4px -76px, 0px 0px;
Here the first background-position is for the browsers that dont support multiple background images, the second is for the gradient position on the browsers that do support it.
6 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
Using a bottom border and a box shadow we are able to create an indented separating line without any additional markup. For the submit button we will use an sprite that has 3 states for :link, :hover, and :active. You can then use different background positions to shift the image up for each state. This keeps your HTTP requests to a minimum and also prevents a flicker while the browser loads an image for the :hover state. 01 02 03 04 #registerNew { width: 203px; height: 40px; border: none;
7 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
05 06 07 08 09 10 11
text-indent: -9999px; background: url('img/createAccountButton.png') no-repeat; cursor: pointer; float: right; } #registerNew:hover { background-position: 0px -41px; } #registerNew:active { background-position: 0px -82px; }
8 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
"") { base.$label.hide(); base.showing = false; }; base.$field.focus(function() { base.fadeOnFocus(); }).blur(function() { base.checkForEmpty(true); }).bind('keydown.infieldlabel', function(e) { base.hideOnChange(e); }).change(function(e) { base.checkForEmpty(); }).bind('onPropertyChange', function() { base.checkForEmpty(); }); }; base.fadeOnFocus = function() { if (base.showing) { base.setOpacity(base.options.fadeOpacity); }; }; base.setOpacity = function(opacity) { base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration); base.showing = (opacity > 0.0); }; base.checkForEmpty = function(blur) { if (base.$field.val() == "") { base.prepForShow(); base.setOpacity(blur ? 1.0 : base.options.fadeOpacity); } else { base.setOpacity(0.0); }; }; base.prepForShow = function(e) { if (!base.showing) { base.$label.css({ opacity: 0.0 }).show(); base.$field.bind('keydown.infieldlabel', function(e) { base.hideOnChange(e); }); }; }; base.hideOnChange = function(e) { if ((e.keyCode == 16) || (e.keyCode == 9)) return; if (base.showing) { base.$label.hide(); base.showing = false; }; base.$field.unbind('keydown.infieldlabel'); }; base.init(); }; $.InFieldLabels.defaultOptions = { fadeOpacity: 0.5, fadeDuration: 300, labelClass: 'infield' }; $.fn.inFieldLabels = function(options) { return this.each(function() { var for_attr = $(this).attr('for'); if (!for_attr) return; var $field = $("input#" + for_attr + "[type='text']," + "input#" + for_attr + "[type='password']," + "input#" + for_attr + "[type='tel']," + "input#" + for_attr + "[type='email']," + "textarea#" + for_attr); if ($field.length == 0) return; (new $.InFieldLabels(this, $field[0], options)); }); }; })(jQuery); 16 17 $("#RegisterUserForm label").inFieldLabels(); 18 }); 19 20 </script> The line $(#RegisterUserForm label).inFieldLabels(); is what activates the script for those particular labels. Make sure to change the ID if yours is different. I modified the script to add support for the input types tel and email. If you choose to use others you will need to write those in (near the end of the script). We are also adding a class of .infield to the labels that need to be restyled. This way if JavaScript is disabled the form will degrade gracefully. Here is the necessary CSS: 1 fieldset label.infield /* .infield label added by JS */ { 2 color: #333; 3 text-shadow: 0px 1px 0px #fff; 4 position: absolute; 5 text-align: left; 6 top: 3px !important; 7 left: 35px !important; 8 line-height: 29px; 9 }
9 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
Sometimes the browser auto-complete can interfere with the inline form fields (especially on login boxes). If this is an issue for your site then you can add autocomplete=off to the input fields. Note: We could have used the HTML5 Placeholder attribute instead, but this method works better in older browsers and also I like how it looks better.
10 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
Thats all!
Please ask any questions and give feedback in the comments. View Demo Download Code
If you enjoyed this post you should read my personal blog or subscribe to the iOS Design Weekly Newsletter. -Nathan Barry
Snapshot Theme Preview A New Level of Precision
Hi, I have used the script and found that it does not work. In the action field of the form I put in mailto:myemail@address.com but nothing has happened. Any ideas? Christopher Reply
This tutorial only covers the markup, design and infield labels. If you want to actually post the form then you need server-side code, which is beyond the scope of this tutorial. You will want to look into doing that with PHP or whatever other language you are working in.
Hi Christopher, Here is a link to a contact form php file that you could use in form.
11 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
2.
Great tutorial, thanks for sharing Reply 4. colin says: April 13, 2012
Pretty form, but serves no purpose without functionality. Whilst youve said its outside the scope of this tutorial, you could have easily included a functional snippet that users could copy paste into the form to make it actually do something. Otherwise were left with a pretty CSS/HTML5 gimmick with no purpose. Reply 5. Prashant says: April 22, 2012
Great Tutorial, Loved the way u have explained new input types and how great they are in terms of accessibility. Reply
Leave a Reply
12 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
Pages Home Our Themes About Support Contact Categories Basics Blogging Design Function Interviews News Plugins Quick Tips Resources Themes Archives September 2010 August 2010 June 2010 May 2010 April 2010 March 2010 February 2010 January 2010 May 2009 April 2009 January 2009 October 2008 Meta
13 of 14
01/08/2013 1:24 PM
http://legendthemes.com/2010/04/10/designing-modern-web-forms-with...
14 of 14
01/08/2013 1:24 PM