CSS: Create a Beautiful Form

This tutorial will explain how to create a beautiful web form with CSS. A live example can be found here. I will take you through the steps of adding the xhtml code and how to adjust the CSS to suit your own needs. As mentioned, this form is coded in valid xhtml and CSS.

Building the Form

First you will have to build the form itself, which I have done below. I assume you're already familiar with how forms work, otherwise a simple Google search on the subject should do.

Form Code

The Styling

To style the form (which should indeed look quite boring at this state) we're needing some serious CSS work. First we're going to remove the fieldset border. Then we will position all of the labels to the left by floating (text-align works as well) the element. We will also do some minor changes to the font.

Next we will add the piece that will give the form the desired appearance. Here I set the padding to 6px to the bottom, right, and top. Since we will add icons to the form fields, the left padding is set to 30px so the text doesn't overlap them. If you wish to use the same icons (or different ones), you can find them at FamFamFam.

Time to add the icons to the form fields! All images will be placed to the left of the text, and a background color will be defined.

Time to style the text field to match the rest of the form.

We're almost done! Now the form itself is complete, but we still need a submit button so your visitors can actually use it. For this, I've decided on using an image. You can of course use a standard button if you like.

Creating an image button is quite tricky, but can of course be done. One way is to specify two different images as the background for the normal state, and the :hover state. However, since this loads two different images, an annoying flicker will be created. To avoid this, we will use one image. This technique is commonly known as CSS Sprites.
If you wish, you can use the button included in the tutorial, or you can create your own. Whatever floats your boat.

To display the normal state of the button, we will set the width and height of the first button, not the entire image. The background is set to 0px 0px at both Y and X coordinates. Next we set the overflow to hidden. This is so the text won't be visible in some browsers, such as Opera. Notice that the top padding is set to the same height as the button itself? This will cause the text to be pushed down where you can't see it.

Finally, we will add the hover state. There's 53px left of the image, starting from the end of the first, which we will push down to reveal our flashy hover effect.

And that's it! You should now have a beautiful CSS form that you can use and customize as you like.

The Complete CSS Code For Lazy People

Comments

No comments yet

Add Your Comment



Notify me about new comments
Hide my email