Forms: styling text fields with CSS and HTML

Please support our Site Layout and Usability advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
roryt roryt is offline Offline Dec 31st, 2006, 10:44 am |
2
Although forms are one of the most boring elements of any website, styling them can be a dangerous business. All too often it is forgotten that the way a form looks will impact greatly upon what the user uses it for. Change the style too drastically and the user might not recognize it as a form at all. In this tutorial you will learn how to style the text field element just enough to make it look good but without ever detracting from the fact that it has to be easily recognizable as a text field from a form.

You will require the following software before you begin:
  • Graphics editor
  • Text editor
You must create your graphic first, which needs to be a shadow on a white background. Importantly, the object that the shadow is coming from must not be visible. The image should be 1*25px with a white background.

You must also create a new HTML file, within which you need a form and then a text field. Your html should look like this:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Styling a text field</title>
  6. </head>
  7.  
  8. <body>
  9. <fieldset><legend>Text Field Effects</legend>
  10.  
  11. <form action="" method="get">
  12. <input name="Search" type="text" value="Search" maxlength="200" />
  13. </form>
  14. </fieldset>
  15.  
  16. </body>
  17. </html>

You now have to add a class to this text field, which you could do using the "selector" "input" but this would style every single type of input on your page. Instead, you should put the following code inside each of the "input" tags: class="textfield_effect".

OK, you are now finished with HTML for the time being. Now you can move on to creating a CSS file. The properties you are going to be playing around with are:
  • border-width, border-color, border-style
  • width, height,
  • background-image
  • You may also choose to change the various text properties.
You will also use the pseudo classes :hover and :focus to create the main effects.

Instead of explaining each tiny bit of CSS, this tutorial will just show you it from there you should be able to understand what has been done (the CSS is commented to help you with this.)

  1. /* CSS Document */
  2.  
  3. .textfield_effect {
  4. /*we will first set the border styles.*/
  5. border-width: 1px;
  6. border-style: solid;
  7. border-color: #999999;
  8. /*we are now going to add in the shadow image that we created earlier*/
  9. background-image: url(back_field.gif);
  10. background-repeat: repeat-x;
  11. /*I am going to add some text formatting of my own*/
  12. font-family: Arial, Helvetica, sans-serif;
  13. font-size: 12px;
  14. color: #333333;
  15. width: 200px;
  16. height: 15px;
  17. }
  18.  
  19. /*we are now going to style how the textfield will look when we
  20. hover over it and when we actually have it selected*/
  21.  
  22. .textfield_effect:hover {
  23. border-color: #64acd8;
  24. border-width: 1px;
  25. }
  26.  
  27. .textfield_effect:focus {
  28. border-color: #64acd8;
  29. border-width: 1px;
  30. }
  31.  
  32. /*you may also like to add some styles for the rest of the form*/
  33.  
  34. fieldset {
  35. width: 400px;
  36. height: 200px;
  37. border-style: solid;
  38. border-width: 1px;
  39. border-color: #036399;
  40. margin-left: auto;
  41. margin-right: auto;
  42. background-color: #F5F5F5;
  43. }
  44. fieldset:hover {
  45. border-color: #0ca0ff;
  46. }
  47. legend {
  48. font-family: sans-serif;
  49. font-size: 18px;
  50. color: #097bc3;
  51. font-weight: bold;
  52. }

Next you must attach the CSS file to the HTML file by adding this code in the head of your HTML file: <link href="style.css" rel="stylesheet" type="text/css" />

Now add a title to the form element but instead of adding it on the outside of the text field you are going to put the text inside the text field. All you need to do is add this piece of code to the "input" tag: onfocus="this.value=''".

The only other thing you need to add is the submit button! A word of warning though, this is an element that should not be styled because it looks very different in different operating systems!


And that is the end of this tutorial, you should now have been able to create something that looks like this:

Click image for larger version

Name:	form-example.gif
Views:	804
Size:	3.1 KB
ID:	2908
Last edited by cscgal; May 13th, 2007 at 11:20 pm.
Quick reply to this message  
0
okparrothead okparrothead is offline Offline | Mar 6th, 2007
Hello Roryt, Forum Members,
To minimize the differences of all elements in different browsers, you can add:
* {padding: 0px; margin: 0px} to your style sheet. This eliminates the default margins and padding for everything. Elements like for H1, h2..., <li> <input>, etc. have different styles in different browsers. This starts you off with a level playing field. You'll have to go back and add some margins and padding to elements you didn't have to style before however!

Peace
Last edited by okparrothead; Mar 6th, 2007 at 2:59 pm. Reason: I can't spell
 
0
tavox tavox is offline Offline | Mar 22nd, 2007
Hello Forum Members,
Congratulations to the author(s) of this excellent tutorial, clean and simple, but i think that it would be COMPLETE if it includes all the files related -code, images, etc.- in a zip file to be able to download.

Best regards,
 
0
roryt roryt is offline Offline | Jun 5th, 2007
I wanted you to learn something from this tutorial not just download the files.. sorry
 
0
fishtattoo fishtattoo is offline Offline | Aug 5th, 2008
Originally Posted by roryt View Post
I wanted you to learn something from this tutorial not just download the files.. sorry
I also think you should have included the files, or at least a demo page. How is a new developer supposed to 'grade' their version against your version.
 
0
adeeniola adeeniola is offline Offline | Jan 23rd, 2009
Thanx for this tutorial. am a newbie to css, could u please help me on how to start using css. I would appreciate links of suggested books and tools. thanx once more.
 
0
ksanders ksanders is offline Offline | Apr 23rd, 2009
Hi,

I noticed the :focus attribute is not supported in Firefox. Anyone have a fix for this?

Thanks.
 
0
lzk82 lzk82 is offline Offline | Jul 23rd, 2009
Dear Author,
Thank you very much, it's an interesting informations for me.
Best Regards,

LZK82
 
0
almostbob almostbob is offline Offline | Jul 23rd, 2009
Originally Posted by ksanders View Post
Hi,

I noticed the :focus attribute is not supported in Firefox. Anyone have a fix for this?

Thanks.
try :active
 
 

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC