Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > All Around Tutorials
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 06-08-2006, 10:57 AM
  #1
karinne
\m/\m/
 
Join Date: Dec 2002
Location: QC, Canada
Posts: 4,297
iTrader: (0)
karinne is on a distinguished road
[CSS] Customize your form with CSS - part 1

Forms can be boring at times with their default styling, I agree. CSS comes to the rescue once again by spicing it up a bit.

A regular form
So ... lets start with coding our form

HTML Code:
<form action="dothis.php" method="post">

  <p><label for="name">Name:</label><br />
  <input type="text" name="name" id="name" title="Type you name" /></p>

  <p><label for="website">Website:</label><br />
  <input type="text" name="website" id="website" title="Type in you website" />

  <p><label for="markup">Markup</label><br />
  <select name="markup" id="markup">
    <option value="html">HTML</option>
    <option value="xhtml">XHTML</option>
  </select></p>
	
  <p><label>CSS?</label><br />
  <input type="radio" name="css" value="yes" /> Yes<br />
  <input type="radio" name="css" value="no" /> No</p>

  <p><label for="description">Describe your site</label><br />
  <textarea name="description" id="description" cols="30" rows="10"></textarea></p>

  <input type="submit" value="submit" name="submit" />
</form>
Here's what it would look like in your browser (see attachment form-1.jpg)

CSS to the rescue!
If you've been playing around with CSS a bit you'll notice that you can do ALOT of nifty things with backgrounds and changing fonts (all the while using common fonts) and colors. Don't think that these changes stops at simple text withing div, span or p elements. Oh no my friend! You can style input elements with CSS just like anything else. So ... here we go!

input
We'll start by adding a border and a light background to the input elements in our form using CSS

CSS:
HTML Code:
input {
  border: 1px solid #000;
  background-color: #ccc;
}
Nothing is to be changed in the form at all. Since we are using the "input" selector, it will make the changes to "all" input elements on our page.

Here's what this simple change do (see attachement form-2.jpg)

You'll notice that the radio buttons didn't change at all even though they are "input" elements. Radio buttons and checkboxes are the only form elements that cannot be changed with css.

select and textarea
To get the same look with the select and textarea element, we could do this in our css

CSS:
HTML Code:
select {
  border: 1px solid #000;
  background-color: #ccc;
}
textarea {
  border: 1px solid #000;
  background-color: #ccc;
}
but why duplicate code. Let's save a few bytes and put it all in one like this

CSS:
HTML Code:
input, select, textarea {
  border: 1px solid #000;
  background-color: #ccc;
}
Again, no changes need to be made to the html. Now we're getting somewhere. See what the form looks like now (see attachement form-3.jpg)

label
Those labels are kind of boring with the regulat text. Let's bold them up and change the font.

CSS:
HTML Code:
label {
  font-family: Tahoma, Geneva, sans-serif;
  font-weight: bold;
  font-size: 14px;
}
Here's what the labels look like (see attachment form-4.jpg) Ahhh... much better.

That silly looking button
Now ... you may notice that the input button looks like those input boxes. Well... it should because it is an input element but ... it's not very distinguishable is it!? We'll fix that by changing the border, font and background color with CSS by creating a class for our button and applying it to our input.

CSS:
HTML Code:
.submit {
  border: 2px solid #333;
  background-color: #862222;
  font-family: Courier New, Courier New, Courier, monospace;
  color: #fff;
}
Add the submit class to the input element in your html
HTML:
Code:
<input type="submit" value="sumbit" name="submit" class="submit" />
And there we are (see attachement form-5.jpg)! Now that's a better looking form.

We're done

I hope this was helpfull to some. If you have any question about this tutorial feel free to PM me or reply here.

This was intended as a 2 parter so, hopefully I will have time to make part II which would talk about display, fieldsets and customizing that sumbit button with an image created in Photoshop.

Here is the complete HTML sources of the form
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-CA" lang="en-CA">

<head>

  <title>[CSS] Customize your form with CSS - part 1</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	
  <style type="text/css">
  <!--
    input, select, textarea {
      border: 1px solid #000;
      background-color: #ccc;
    }
	
    label {
      font-family: Tahoma, Geneva, sans-serif;
      font-weight: bold;
      font-size: 14px;
    }
    
    .submit {
      border: 2px solid #333;
      background-color: #862222;
      font-family: Courier New, Courier New, Courier, monospace;
      color: #fff;
    }
    -->
  </style>

</head>

<body>

<form action="dothis.php" method="post">
  <p><label for="name">Name:</label><br />
  <input type="text" name="name" id="name" title="Type you name" /></p>

  <p><label for="website">Website:</lable><br />
  <input type="text" name="website" id="website" title="Type in you website" />

  <p><label for="markup">Markup</label><br />
  <select name="markup" id="markup">
    <option value="html">HTML</option>
    <option value="xhtml">XHTML</option>
  </select></p>
	
  <p><label>CSS?</label><br />
  <input type="radio" name="css" value="yes" /> Yes<br />
  <input type="radio" name="css" value="no" /> No</p>

  <p><label for="description">Describe your site</lable><br />
  <textarea name="description" id="description" cols="30" rows="10"></textarea></p>

  <input type="submit" value="sumbit" name="submit" class="submit" />
</form>

</body>
</html>
Attached Images
File Type: jpg form-1.jpg (18.4 KB, 1329 views)
File Type: jpg form-2.jpg (15.3 KB, 864 views)
File Type: jpg form-3.jpg (15.4 KB, 753 views)
File Type: jpg form-4.jpg (15.4 KB, 784 views)
File Type: jpg form-5.jpg (17.5 KB, 1387 views)
__________________
[a web design portfolio - AVAILABLE for work | web design | Re-coding | PSD-to-HTML]
I'm also on: del.icio.us | flickr | virb | facebook | twitter

Last edited by karinne : 06-09-2006 at 11:13 AM.
karinne is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 06-08-2006, 10:32 PM
  #2
_Aerospace_Eng_
The CSS Master (somewhat)
 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a galaxy far far away...
Posts: 11,154
iTrader: (0)
_Aerospace_Eng_ has disabled reputation
Good tutorial karinne. I would like to add to this. Using karinne's code we are going to add a fieldset and a legend. Usually form elements are inside of fieldsets. To do so we add the fieldset element around karinnes code, just starting after the open form tag and closing before the closing form tag. I'll make them green and bold.
Code:
<form action="dothis.php" method="post">
  <fieldset>
  <p><label for="name">Name:</label><br />
  <input type="text" name="name" id="name" title="Type you name" /></p>

  <p><label for="website">Website:</label><br />
  <input type="text" name="website" id="website" title="Type in you website" />

  <p><label for="markup">Markup</label><br />
  <select name="markup" id="markup">
    <option value="html">HTML</option>
    <option value="xhtml">XHTML</option>
  </select></p>
	
  <p><label>CSS?</label><br />
  <input type="radio" name="css" value="yes"> Yes<br />
  <input type="radio" name="css" value="no"> No</p>

  <p><label for="description">Describe your site</label><br />
  <textarea name="description" id="description" cols="30" rows="10"></textarea></p>

  <input type="submit" value="sumbit" name="submit" class="submit" />
  </fieldset>
</form>
That will give you something like the first attachment. IE looks cooler than Firefox because it seems to give it a type of curved corners. Firefox makes it look like an inset box. We can make it look the same in IE and FF by specifying a border using CSS for the fieldset. Taking the styles we have already we will add the fieldset style.
Code:
  <style type="text/css">
  <!--
    input, select, textarea {
      border: 1px solid #000;
      background-color: #ccc;
    }
	
    label {
      font-family: Tahoma, Geneva, sans-serif;
      font-weight: bold;
      font-size: 14px;
    }
    
    .submit {
      border: 2px solid #333;
      background-color: #862222;
      font-family: Courier New, Courier New, Courier, monospace;
      color: #fff;
    }
    fieldset {
      border:1px solid #333;
    }
    -->
  </style>
We aren't going to give a background color to the fieldset. You can if you want too. Now here comes the legend tag. We will add it just after the fieldset tag.
Code:
  <fieldset><legend>This is the legend</legend>
  <p><label for="name">Name:</label><br />
  <input type="text" name="name" id="name" title="Type you name" /></p>

  <p><label for="website">Website:</label><br />
  <input type="text" name="website" id="website" title="Type in you website" />

  <p><label for="markup">Markup</label><br />
  <select name="markup" id="markup">
    <option value="html">HTML</option>
    <option value="xhtml">XHTML</option>
  </select></p>
	
  <p><label>CSS?</label><br />
  <input type="radio" name="css" value="yes"> Yes<br />
  <input type="radio" name="css" value="no"> No</p>

  <p><label for="description">Describe your site</label><br />
  <textarea name="description" id="description" cols="30" rows="10"></textarea></p>

  <input type="submit" value="submit" name="submit" class="submit" />
  </fieldset>
You will get something that looks like the second attachment in this post.
Attached Images
File Type: jpg formwithfieldset.jpg (20.4 KB, 595 views)
File Type: jpg formwithfieldsetandlegend.jpg (20.5 KB, 660 views)
__________________

76 invites left. PM me for a Gmail invite along with email addy.
Why we won't help you .::. Web Developer's Handbook .::. Why Tables for Layout is Stupid?

Last edited by _Aerospace_Eng_ : 06-09-2006 at 11:51 AM.
_Aerospace_Eng_ is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 07-07-2006, 10:19 PM
  #3
mattr12
Paladin (Level 15)
 
mattr12's Avatar
 
Join Date: Jan 2006
Posts: 461
iTrader: (0)
mattr12 is an unknown quantity at this point
Nice tutorial! I used the custom buttons on my site
mattr12 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart
 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 10:24 PM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.