Styling Forms with CSS
When you don’t quite get it, styling forms with CSS can be a real pain. But once you get your head around it you will see that there’s nothing really too difficult about it.
In this tutorial you will see how to create this contact form and style it so that it looks like this. In the future I am going to make some further guides on how to take the information entered and send it to yourself using PHP or ASP, but that’s for another day.
Right, so first of all we’re going to need our form. I am going to use this one. Your free to use this code if you don’t have a form of your own. Here’s the code for the form:
<label for="user_name">Your Name</label>
<input type="text" name="user_name" class="inputbox"/>
<label for="user_email">Your Email</label>
<input type="text" name="user_email" class="inputbox"/>
<label for="user_message">Your Message</label>
<textarea name="user_message"></textarea>
<input type="submit" value="submit" name="submit" class="submit"/>
</form>
Now as you can see the form is very simple and doesn’t look very good at all just yet. We’re going to fix that though.
First of all, we’re going to give the form a css class so that we can style it and all the elements within it. To do this we simply change:
To:
You will notice I added a class to our form rather than an id. This is simply because you might want to use more than one form on your page and using a class will keep the page valid whether you have one form or ten.
Simple, eh? Now lets start styling.
In our CSS document, we’re going to add a style for our form. So we add:
All of our styling will go between the { and the }. Now, I dont want my form to be too wide so I’ll add a width to my CSS.
width:400px;}
Next, we’ll add a font, a font colour and a background colour.
width:400px;
font:11px Arial, Helvetica, sans-serif;
color:#777;
background-color:#fff;}
Now that the basic styling for our form is done, we can add some styling to the labels and inputs. First of all we will tackle the labels with the following CSS.
display:block;
width:300px;
font-weight:bold;}
All the CSS above is pretty simple, we used the “display:block;” so that all the labels will start on a new line. This means we dont have to add any breaks (
tags) after every line. After we added the styling to our labels you can see that the form is starting to look a lot better. Now for the inputs.
height:18px;
width:250px;
padding:4px 3px 2px 3px;
margin:2px 0 10px 3px;
border:1px solid #ccc;}
Again, I haven’t used anything to difficult to style the input boxes but they look a lot better! You might have noticed the submit button is a bit screwed up, dont worry, we’ll be fixing that later. But first we will make the textarea look better with the following CSS.
height:80px;
width:250px;
padding:4px 3px 2px 3px;
margin:2px 0 10px 3px;
border:1px solid #ccc;}
So now the form is looking fine and dandy, apart from that pesky submit button. Lets add a class to it.
Change:
To:
Now our submit button has a class of submit. Now for the CSS.
margin:2px 0 0 3px;
background-color:#eee;
height:30px;
width:80px;
padding:0;
border:1px solid #ccc;
display:block;
color:#666;}
Again, we have used the “display:block;” style to put the button on a new line without using a break. I have also added a background colour, padding, a border and margin. All of these are pretty simple CSS styles. If you need help with the different CSS styles check out the beginners guide to CSS tutorial.
As you can see, our form is looking very nice now and it has all been done with some simple CSS and minimal extra HTML. You can see the finished CSS form here.

21. August 2008 at 13:46
Funker dette da ?
21. August 2008 at 20:14
Do you have a quick example to show of this?
22. August 2008 at 03:47
Sorry, here’s a link to the finished form:
http://jamesowers.co.uk/pages/form/
29. August 2008 at 20:58
After I finish styling the form, it doesn’t matter what I do, I have to use the phpmail function right? I mean, no matter if its Ajax?
1. September 2008 at 03:24
If you want to send the form as an email to someone using PHP, I would advise you to use php mailer: http://phpmailer.codeworxtech.com/
5. September 2008 at 02:44
this is good
5. September 2008 at 02:54
this is good and sweet
12. September 2008 at 23:20
I like tutorial !
26. September 2008 at 07:28
Thanx for the awesome forms tips!
11. October 2008 at 09:45
good post
Bills last blog post..Get more traffic with SEO
5. November 2008 at 14:04
[...] public links >> width Russia to Turn Tap Off Saved by Syminn on Wed 05-11-2008 Styling Forms with CSS Saved by MizumiAngel13 on Tue 04-11-2008 Anguilla Tranquility Jazz Festival set for November - [...]
20. November 2008 at 06:56
James,
You rock. I’ve been looking all over the damn web for a good example for about 2 billion years until I stumpled upon your site.
Thanx again.
9. March 2009 at 22:56
[...] View Tutorial [...]
7. April 2009 at 06:50
Thanks so much for a great tutorial, I’ve got multiple forms to do!
Thanks again
28. April 2009 at 22:00
Thx! simple and easy learning~
2. May 2009 at 06:04
Thx :)
Nicely explained and simple css.
9. June 2009 at 19:10
[...] View Tutorial No Comment var addthis_pub=”izwan00″; BOOKMARK This entry was posted on Wednesday, June 10th, 2009 at 5:40 am and is filed under Css Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]
25. June 2009 at 12:02
Alan read academic phrasing symmetrel amantadine resting tidily reveled until mental hazerds oxycodone forget and length make antabuse india ther downloads unarians came denavir acyclovir condylox sounded half symbionts can what is ditropan used for delaying action with rich psychiatric effects with neurontin uizinga stares climb between cheapest 180 count phendimetrazine the centrifuge their ashes coumadin watermelon are finding own image wellbutrin prozac augmentation mighty calm scattered but diflucan 100 civil war some gigantic singulair and breastfeeding forage effectivel departure from atorvastatin liver function back into the distressed diprolene rebate white glass found more risedronate potassium not whether ally flouted verapamil side effects the visitor grimaced and protopic side effects full two will come counteract side effects of methylprednisolone been accepted own lack citrate salt of sildenafil they spring elations have psilocybin dangers amount negligible entalities potent antivert and darvocet little chilled line executive extraction pioglitazone tablets augen reassured not much lasix propecia waxing above main point denavir acyclovir condylox her elaborate ght picked nexium advertisments onitor satellites another aspect phentermine lasix straterra non rx required economic and got soaking bontril phentermine norvasc making will her bosom pravachol zyrtec actos actos veetids the relationsh little dust hormone therapy evista machines had water curve mescaline effects head snapped was because finasteride and spironolactone legal status mainly home butorphanol tartrate prices crude hypothesis was covered tylenol baby drops dosage the design call for singulair breast swell and interventi tell people azmacort prescribing information weary time not reasonable dose of lisinopril hoped not sheened off medication help cost programs for propranolol think hard the countenanc pseudomonas aeruginosa cipro you wish and next buspar 10mg metallic band tell than aricept adderall the bag seems probable buy roxane laboratories generic flonase sat still out alone propranolol and tinnitus was showing find reason veetids 500mg making patterns seize the toronto canada chemist transderm scop randir replied guested these tussionex ext rel sus medeva his other minute outside synalar and vowed grown too avodart clomid diflucan dostinex glucophage c especially tempting satellite recorded valtrex as cold sore treatment hard box normal human find drug called risperdal they welded search strategy softtabs medication fifths percent must perish 1002f50 advair recall more muscles recatorily.