Style Web Forms Using CSS Article

By | | CSS Tutorials

5

Whether your main business is Web design or backend development, chances are you spend a fair amount of time creating forms for user input. So you already know that the default appearance of forms isn’t always appropriate for the look and feel of your site.

In this article we’ll look at how you can use CSS to create attractive and usable forms.

Styling Form Elements

It’s possible to change the default look of form elements by styling their html tags: input, select and textarea.

The input Tag

Defining rules for the input tag will change any instance of that tag in your document. For example, if I wish all elements to have a purple background, I could define the following in my style sheet.

input {
  background-color: #666699;
}

This will add a purple background color to those elements that are marked up using the input tag.

1166_image1

The select Tag

The <select> tag creates a list menu. You can create rules for select which will affect any list menus in your document.

select {
  background-color: #666699;
  color: #ffffff;
}

1166_image2

The textarea Tag

The <textarea> tag marks up multiple line text input fields. Once again, setting rules for textarea will change the look of all of these elements in your document.

textarea {
  background-color: #666699;
  color: #ffffff;
}

1166_image3

The form Tag

You can also style the form tag itself, adding borders, background colors and adjusting the margins and padding. Form is a block level element, so you can change the way it displays in much the same way that you would style a paragraph.

form {
  border: 1px solid #666699;
  padding: 5px;
}

1166_image4

Creating Classes for Form Elements

Simply defining rules for your elements has some obvious problems. You probably don’t want those funny boxes around your checkboxes and radiobuttons; you may also want to create a different appearance for text input boxes and buttons. Perhaps you’d like to have several different form styles available in your style sheet, each serving a different purpose.

To do this, you can create classes and apply them to individual form elements. For example, this class in the style sheet:

.texta {
  font-size: 10px;
  background-color: #CCCCCC;
  border: 1px solid #666666;
}

can be applied to a form element like so:

<input name="textfield" type="text" class="texta" />

Any other form elements or input tags in the document will remain unstyled, as this class is not applied to them.
1166_image5
When I begin work on a site that’s going to involve a lot of forms, one of the first things I do is create classes for my standard forms in the style sheet. It doesn’t matter if the style needs to change at a later date — that simply involves tweaking the values in the style sheet. The important thing is that classes are applied from the outset, so that any changes affect all forms on the site.

Go to page: 1 | 2 | 3 | 4

Written By:

Rachel Andrew

Rachel is the Director of edgeofmyseat.com, a Web solutions company in the UK. She has worked on a number of books as a co-author, and is a member of the Web Standards Project, serving on the Dreamweaver Task force. Rachel is the author of SitePoint's The CSS Anthology and Build Your Own Standards Compliant Website Using Dreamweaver 8.

 

{ 5 comments }

Emma Pope August 31, 2011 at 1:03 pm

i cant believe this!! me and my sister just got two i-pads for $42.77 each and a $50 amazon card for $9. the stores want to keep this a secret and they dont tell you.
go here, pluscent.com

Chris Allen Aaker July 11, 2011 at 9:28 pm

How do I get permanent text (i.e. a label with in the text field) to display on my text fields. Kind of like twitter but ones that do not disappear with the onclick

Savannah Hogan August 1, 2011 at 8:33 am

I just paid $22.87 for an iPad2-64GB and my girlfriend loves her Panasonic Lumix GF 1 Camera that we got for $38.76 there arriving tomorrow by UPS. I will never pay such expensive retail prices in stores again. Especially when I also sold a 40 inch LED TV to my boss for $675 which only cost me $62.81 to buy. Here is the website we use to get it all from, CoolCent. com

Pete September 22, 2011 at 8:54 am

Chris,

In order to achieve that desired effect you would simply add onfocus=”value=””
to the end of your input string.
eg:

Hope that helps!

Anonymous October 4, 2011 at 1:26 am

just put value inside the tag …value=”Your text”….

Comments on this entry are closed.