Thursday 29 August 2013

Building Forms - Part 3

Fieldset

Fieldsets group form controls and labels into organized sections. Much like a div or other structural element, thefieldset is a block level element that wraps related elements, however specifically within a form for better organization. Fieldsets by default also include a border outline that can be modified using CSS.
  1. <fieldset>
  2. <label for="username">Username</label>
  3. <input type="text" name="username" id="username">
  4. <label for="password">Password</label>
  5. <input type="text" name="password" id="password">
  6. </fieldset>

Fieldset Demo

   

Legend

A legend provides a caption, or heading, for a fieldset. The legend element wraps text describing the controls that fall within the fieldset. The HTML code should include the legend directly after the opening fieldset tag. Thelegend itself will appear on the page within the top left part of the fieldset border. The appearance of the legendon a page may be changed with CSS.
  1. <fieldset>
  2. <legend>Login</legend>
  3. <label for="username">Username</label>
  4. <input type="text" name="username" id="username">
  5. <label for="password">Password</label>
  6. <input type="text" name="password" id="password">
  7. </fieldset>

Legend Demo

Login   

Form & Input Attributes

To accommodate all of the different form, input, and control elements there are a number of attributes and corresponding values. These attributes and values serve a lot of different functions including the ability to disable controls, add form validation, and so forth. Below are some of the more popular and useful attributes.

Disabled

The disabled Boolean attribute turns off an element or control so that it is not available for interaction or input. Elements that are disabled will not send any values to the server for form processing.
Applying the disabled Boolean attribute to a fieldset will disable all of the controls within the fieldset. If thetype attribute has a hidden value, the disabled Boolean attribute is disregarded.
  1. <label for="username">Username</label>
  2. <input type="text" name="username" id="username" disabled>

Disabled Demo

 

Placeholder

The placeholder HTML5 attribute provides a tip within the control of an input and disappears once the control is clicked in, or gains focus. The placeholder attribute only applies to inputs with a type attribute value of textemail,searchtel, or url.
The main difference between the placeholder and value attributes is that the value text stays in place during focus unless manually deleted by a user. This is great for pre-populating data, such as personal information for a user, but not for providing suggestions. The difference between the two attributes can be seen below.
  1. <label for="username">Username placeholder</label>
  2. <input type="text" name="username" id="username" placeholder="Holder">

Placeholder Demo

   

Required

The required HTML5 attribute enforces that an element or control contain a value upon being submitted to the server. Should an element or control not have a value an error message will be displayed requesting a user complete the required field. Currently error message styles are controlled by the browser and are unable to be styled with CSS. Elements, on the other hand, can be styled using the :optional and :required CSS pseudo-classes.
Validation also occurs specific to a control’s type. For example, an input with a type attribute value of email will require that not only a value exist within the input, but that it also includes a valid email format.
  1. <label for="name">Name</label>
  2. <input type="text" name="name" id="name" required>

Required Demo

    

Additional Attributes

Other form and input attributes include, but are not limited to the following. Please feel free to research and look into these attributes as necessary.
  • accept
  • autocomplete
  • autofocus
  • formaction
  • formenctype
  • formmethod
  • formnovalidate
  • formtarget
  • max
  • maxlength
  • min
  • pattern
  • readonly
  • selectionDirection
  • step

Login Form Example

HTML
  1. <form>
  2. <label for="login_username">Username</label>
  3. <input type="text" name="login_username" id="login_username">
  4. <label for="login_password">Password</label>
  5. <input type="password" name="login_password" id="login_password">
  6. <fieldset>
  7. <input type="submit" name="login_submit" id="login_submit" value="Login">
  8. <label><input type="checkbox" name="login_remember" id="login_remember"> Stay signed in</label>
  9. </fieldset>
  10. </form>
CSS
  1. form {
  2. background: linear-gradient(top, #fff, #f8f8f8);
  3. border: 1px solid #d0d2d5;
  4. border-bottom: 1px solid #bebfc2;
  5. border-radius: 4px;
  6. margin: 0 0 20px 0;
  7. padding: 20px;
  8. width: 212px;
  9. }
  10. label {
  11. color: #404853;
  12. display: block;
  13. font-weight: bold;
  14. }
  15. input {
  16. background: #fff;
  17. border: 1px solid #c6c9cc;
  18. border-radius: 4px;
  19. box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.1), 0 1px 0 #fff;
  20. color: #555;
  21. font: 13px/20px 'Droid Sans', Arial, 'Helvetica Neue', 'Lucida Grande', sans-serif;
  22. margin: 0 0 20px 0;
  23. padding: 5px;
  24. width: 200px;
  25. }
  26. fieldset {
  27. background: linear-gradient(top, #ebeced, #dedfe0);
  28. border: none;
  29. border-top: 1px solid #d0d2d5;
  30. border-radius: 0 0 4px 4px;
  31. box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.75);
  32. margin: 5px 0 -20px -20px;
  33. padding: 18px 20px;
  34. width: 212px
  35. }
  36. fieldset input {
  37. margin: 0;
  38. width: auto;
  39. }
  40. #login_submit {
  41. background: linear-gradient(top, #687587, #404853);
  42. border: none;
  43. box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.75);
  44. color: #fff;
  45. font-weight: bold;
  46. float: left;
  47. padding: 5px 10px;
  48. text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.6);
  49. }
  50. #login_submit:hover {
  51. background: linear-gradient(top, #5a6675, #333942);
  52. }
  53. #login_submit:active {
  54. background: #333942;
  55. box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.75), 0 1px 0 rgba(255, 255, 255, 0.75);
  56. }
  57. fieldset label {
  58. color: #888;
  59. cursor: pointer;
  60. float: left;
  61. font-size: 12px;
  62. font-weight: normal;
  63. margin: 5px 0 0 20px;
  64. }
  65. fieldset label input {
  66. margin: -2px 2px 0 0;
  67. padding: 0;
  68. }

Demo