Article

Home » Client-side Coding » CSS Tutorials » Style Web Forms Using CSS

About the Author

Rachel Andrew

author_RachelA 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.

View all articles by Rachel Andrew...

Style Web Forms Using CSS

By Rachel Andrew

July 14th, 2003

Reader Rating: 8.5

Page: 1 2 3 4 Next

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.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links