Advertisement
Guest User

Untitled

a guest
May 6th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. A big part of our work as website designers is the ability to make things look good and function well. We spend hours taking the time to make every aspect of our site visually compelling, intuitive, user friendly, accessible and overall beautiful. Our forms are no exception! Our forms should be beautiful, easy to use, and should look consistent with the rest of our website. We can do this easily with CSS.
  2.  
  3. The process isn’t difficult, you just can to know what each tag does, and how to style it. The first thing we need to do is bring in our HTML. Below is the HTML found in our sample form.
  4.  
  5. <form>
  6. <div>
  7. <h1>Contact Form :</h1>
  8. <label>
  9. <span>Your name</span><input id="name" type="text" name="name" />
  10. </label>
  11.  
  12. <label>
  13. <span>Email Address</span><input id="email" type="text" name="email" />
  14. </label>
  15.  
  16. <label>
  17. <span>Subject</span><input id="subject" type="text" name="subject" />
  18. </label>
  19.  
  20. <label>
  21. <span>Message</span><textarea id="feedback" name="feedback"></textarea>
  22. <input type="button" value="Submit Form" />
  23. </label>
  24.  
  25. </div>
  26. </form>
  27. You will notice that in the HTML, I used words, names, and ids that make sense. They are consistent with what you’d expect each field to be called. Each Field is wrapped in a label tag to make things easy for us to style. Our form looks pretty plain without any styling, as you can see from the sample below:
  28.  
  29.  
  30.  
  31. Our form has no structure, no color, and no personality. We can change that with a little bit of code. First, we are going to style the form tag itself.
  32.  
  33. form {
  34. background: -webkit-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
  35. background: -moz-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
  36. background: linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
  37. margin: auto;
  38. position: relative;
  39. width: 550px;
  40. height: 450px;
  41. font-family: Tahoma, Geneva, sans-serif;
  42. font-size: 14px;
  43. font-style: italic;
  44. line-height: 24px;
  45. font-weight: bold;
  46. color: #09C;
  47. text-decoration: none;
  48. border-radius: 10px;
  49. padding: 10px;
  50. border: 1px solid #999;
  51. border: inset 1px solid #333;
  52. -webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
  53. -moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
  54. box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
  55. }
  56. The code above can look like a mouthful, but it is fairly simple when broken down. Flat colors can be really boring, so adding a slight gradient can break up the monotony and give your design some dimension. That is done with the background style. When using this property and gradients, you have to include the specific prefixes for certain browsers such as Firefox, or they won’t show up. Both are saying the same thing. Create a linear gradient, start from the bottom, and use a medium gray and a light gray and blend it over 175px.
  57.  
  58. Since this is where you entire form is going to be contained, I decided to center the form in the browser by setting margin to auto. Setting the Position to Relative is intended for aligning an element later, so that explanation is to come. I specified the width and the height of the form, the fonts used, and styled it to be bold, italic, 14px in size and a line height (spacing between each line of text) of 24px.
  59.  
  60. Border radius gives us rounded corners for our boxes. Increase the number for more rounded corners. Padding gives some space between the text and the edge of the form, so that your text doesn’t run outside the bounds of your form and its rounded corners.
  61.  
  62. You can create subtle borders for contrast and dimension. I also added box shadows to the overall form, so if this becomes a popup form, it will add dimension and make the form look like it is floating over the rest of the site. This is a popular technique right now. This is yet another style that needs you to specify the proper prefix in order to get it to show up. You form should look something like this:
  63.  
  64.  
  65.  
  66. Next, we should style the input area. That is where the text is actually entered into each field.
  67.  
  68. input {
  69. width: 375px;
  70. display: block;
  71. border: 1px solid #999;
  72. height: 25px;
  73. -webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
  74. -moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
  75. box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
  76. }
  77. The code shown above selects all of the text input areas, and styles them to be 375px wide, and setting the display to block stacks them vertically. Adding a 1px border helps to emphasize each input area, and setting the height to 25px gives the user plenty of room visually to enter their text.
  78.  
  79. I added a box shadow for dimension, but remember to include the prefix for each browser. The first 2 digits control the offset for the shadow. Positive numbers push the shadow to the right and up, and negative numbers push it to the left and down. The 3rd number determines how much the shadow is blurred. The higher the number, the larger the blur. Inside of the parenthesis, the 1st three numbers determine the red, green, and blue values of the shadow, and the decimal number determines the opacity of the shadow itself. 1 is 100% opacity and 0.1 is 10% opacity. With these style added, your form should begin to take shape, and look like the image below:
  80.  
  81.  
  82.  
  83. Everything is aligned, but notice that the submit button has been affected b the width styling. We will fix this later. The message area doesn’t look right, but we can fix this easily.
  84.  
  85. textarea#feedback {
  86. width: 375px;
  87. height: 150px;
  88. }
  89. You can specify the width and the height directly, but this still doesn’t make the textarea fall in line with the other fields.
  90.  
  91.  
  92.  
  93. We have to set the display property to block manually, so that it performs the same way as the input areas.
  94.  
  95. textarea#feedback {
  96. width: 375px;
  97. height: 150px;
  98. display: block;
  99. }
  100.  
  101.  
  102. Now that everything is aligned properly, we can get down to fixing the submit button. The CSS that we need to fix this is fairly simple:
  103.  
  104. button {
  105. width: 100px;
  106. position: absolute;
  107. right: 20px;
  108. bottom: 20px;
  109. background: #09C;
  110. color: #fff;
  111. font-family: Tahoma, Geneva, sans-serif;
  112. height: 30px;
  113. border-radius: 15px;
  114. border: 1p solid #999;
  115. }
  116.  
  117. input.button:hover {
  118. background: #fff;
  119. color: #09C;
  120. }
  121. We select the button named submit and define its width to 100px and set its position to absolute. As we mentioned earlier, we had styled the form to have a relative position. The way this works is that when you set something to have a absolute position, it looks for the last element that has its position set to relative. If that element is nested inside of the element with a position of relative, its absolute position is relative to that element. In other words, the submit button will be positioned somewhere inside of the bounds of the form container. I defined that it will be 20px from the right and from the bottom with those respective styles.
  122.  
  123. I set the background to blue and the text to white. I gave it a definite height of 30px and rounded corners. I also have it a 1px gray border. This is the normal state for your submit button.
  124.  
  125. You will notice that I defined a hover state for the submit button. The styles defined here override the original styling once the user hovers over the button. I changed the background to white and the text to blue, giving the user a highly contrasting effect when they mouse over the button.
  126.  
  127. Here is the normal state:
  128.  
  129.  
  130.  
  131. Here is the hover state:
  132.  
  133.  
  134.  
  135. Our form’s structure is done. You could stop here and you would have a great form, all styled with CSS. However, you could take it one step further, by adding a little user friendly styling to the text input areas, so that the user can tell where they are typing. You can do this with a small amount of CSS:
  136.  
  137. textarea:focus, input:focus {
  138. border: 1px solid #09C;
  139. }
  140. What this does is it tells the browser that if a person has a text input or text area selected, that it needs to add a 1px blue border around the active input area, so the user knows where they are visually in the form. This is just a little extra something that is much appreciated by many users.
  141.  
  142.  
  143.  
  144. Conclusion
  145. You can do some wonderful things with CSS, including creating beautiful web forms for your users. With just a little CSS and some imagination, you can make something as boring as filling out a form, much more enjoyable for anyone who is visiting your site.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement