| | |
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
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:
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:
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:
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:
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.)
Next you must attach the CSS file to the HTML file by adding this code in the head of your HTML file:
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:
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:
form-example.gif
You will require the following software before you begin:
- Graphics editor
- Text editor
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:
html Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Styling a text field</title> </head> <body> <fieldset><legend>Text Field Effects</legend> <form action="" method="get"> <input name="Search" type="text" value="Search" maxlength="200" /> </form> </fieldset> </body> </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.
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.)
css Syntax (Toggle Plain Text)
/* CSS Document */ .textfield_effect { /*we will first set the border styles.*/ border-width: 1px; border-style: solid; border-color: #999999; /*we are now going to add in the shadow image that we created earlier*/ background-image: url(back_field.gif); background-repeat: repeat-x; /*I am going to add some text formatting of my own*/ font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #333333; width: 200px; height: 15px; } /*we are now going to style how the textfield will look when we hover over it and when we actually have it selected*/ .textfield_effect:hover { border-color: #64acd8; border-width: 1px; } .textfield_effect:focus { border-color: #64acd8; border-width: 1px; } /*you may also like to add some styles for the rest of the form*/ fieldset { width: 400px; height: 200px; border-style: solid; border-width: 1px; border-color: #036399; margin-left: auto; margin-right: auto; background-color: #F5F5F5; } fieldset:hover { border-color: #0ca0ff; } legend { font-family: sans-serif; font-size: 18px; color: #097bc3; font-weight: bold; }
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:
form-example.gif
Last edited by cscgal; May 13th, 2007 at 11:20 pm.
0
•
•
•
•
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
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
•
•
•
•
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,
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
•
•
•
•
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
•
•
•
•
Hi,
I noticed the :focus attribute is not supported in Firefox. Anyone have a fix for this?
Thanks.
I noticed the :focus attribute is not supported in Firefox. Anyone have a fix for this?
Thanks.
0
•
•
•
•
Dear Author,
LZK82
Best Regards,Thank you very much, it's an interesting informations for me.
LZK82
Similar Threads
- Extending HTML Forms (JavaScript / DHTML / AJAX)
- HTML/CSS DIV help needed (HTML and CSS)
- Featured Writer (Geeks' Lounge)
- suggestion for [code] (DaniWeb Community Feedback)
- make css apply to a chunk of html (HTML and CSS)
| Thread Tools | Search this Thread |
blogging content customer design development dreamweaver duplicate email evaluation filesharing firefox flash gilbane google html itunes javascript kazaa layout music napster peertopeer photoshop remote remoteserver satellitenavigation satnav server site tables template tips tomtom url wave web website websitedesignreview web_development web_sites





Thank you very much, it's an interesting informations for me.