Design a Prettier Web Form with CSS 3

Design a Prettier Web Form with CSS 3

This entry is part 7 of 16 in the CSS3 Mastery Session
« PreviousNext »

Thanks to advanced CSS properties, such as gradients and shadows, it’s now quite easy to turn a dull web form into something beautiful – with minimal effort. I’ll show you how in today’s tutorial!


Prefer a Video Tutorial? Join Plus!

If you’re more of a visual learner, you can watch a video version of this article instead. Simply help give back to Nettuts+ by signing up for a Plus membership, which grants you access to the Plus content for ALL of our sites – all for $19 per month.

Already a Member?

Watch the video version of this tutorial.

Our Final Product

Subtle background gradients give depth to the fields while shadows lift them from the page. Even more impressive is that this is done without any images at all.

By following this tutorial you will not only end up with a lightweight and beautiful form, you’ll also learn and understand new CSS3 techniques, such as box-shadow, gradients, opaque colors, and rounded corners.

CSS3?

CSS3 is the next generation of CSS that is currently under development, but that doesn’t stop browsers from already implementing many of the prominent features.

Opera has greater levels of support for CSS3 (except background gradients) in their next version (10.50 Beta).

Internet Explorer has also stated that they will have improved CSS3 support with version 9; however, only time will tell how true this is.

The things you can do with CSS3 (shadows, gradients, round corners, animations, etc) all serve a purpose of creating beautiful effects without having to integrate images or scripts, resulting in quicker loading times.

Step 1: The HTML

Before we begin styling we need something to style, so here is the form.

<form class="form">

	<p class="name">
		<input type="text" name="name" id="name" />
		<label for="name">Name</label>
	</p>

	<p class="email">
		<input type="text" name="email" id="email" />
		<label for="email">E-mail</label>
	</p>

	<p class="web">
		<input type="text" name="web" id="web" />
		<label for="web">Website</label>
	</p>

	<p class="text">
		<textarea name="text"></textarea>
	</p>

	<p class="submit">
		<input type="submit" value="Send" />
	</p>

</form>

Each field is inside a paragraph with its own class, and the three first fields have a label explaining their use.

How does it look without any styling?



Functional, but dull. Let’s start pimping out this form.

Step 2: Basic Styling

Before we dive into the CSS3 techniques we need to create a basic layout for browsers that don’t yet support CSS3.

input, textarea { 
	padding: 9px;
	border: solid 1px #E5E5E5;
	outline: 0;
	font: normal 13px/100% Verdana, Tahoma, sans-serif;
	width: 200px;
	background: #FFFFFF;
	}

textarea { 
	width: 400px;
	max-width: 400px;
	height: 150px;
	line-height: 150%;
	}

input:hover, textarea:hover,
input:focus, textarea:focus { 
	border-color: #C9C9C9; 
	}

.form label { 
	margin-left: 10px; 
	color: #999999; 
	}

.submit input {
	width: auto;
	padding: 9px 15px;
	background: #617798;
	border: 0;
	font-size: 14px;
	color: #FFFFFF;
	}

How does our effort look so far?

Not too bad. Now, let’s begin our enhancements with the more advanced CSS3.

Step 3: Box-shadow

Box-shadow does exactly what it sounds like: creates a shadow around a box.

The syntax for box-shadow is fairly simple:

box-shadow: <color> <horizontal offset> <vertical offset> <blur>;

Horizontal offset is the placement of the shadow from left to right. If you set it to “2px” the shadow will be 2 pixels to the right. Vertical offset is the same but up/down.

Blur is simply the amount of blur the shadow will have, where 0 is minimum.

This is how our box-shadow will look like:

input, textarea {
	box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;
	-moz-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;
	-webkit-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;
	}

Here we have three lines that look similar.

  • box-shadow is pure CSS3 and so far only used in Opera.
  • -webkit-box-shadow is for browsers using the Webkit engine, like Chrome and Safari.
  • -moz-box-shadow is for browsers using Mozilla’s Gecko engine, like Firefox, Camino, Flock, and SeaMonkey.

Until CSS3 becomes the standard, you have to use all three methods. Internet Explorer has their own weird way of doing things, and although it’s capable of making a shadow it will not look the way we want it. 3

You might notice that there was no normal RGB color used, this is because we’re using two CSS3 techniques on the same line: box-shadow and rgba.

RGBA (Red Green Blue Alpha) is, simply put, color with opacity.

The syntax for rgba is this:

rgba(<red>,<green>,<blue>,<opacity>);

It’s perfectly fine to use a light grey for the shadow’s color, but if you are using any other background than white it will look strange. An opaque black on the other hand will work well no matter what background.

So our box-shadow is black with 10% (0.1) opacity, no horizontal and vertical offset, and with a blur of 8 pixels. It will look like this:

The keyword here is subtlety. If we apply too much shadow, it will look ugly; if we apply too little, it won’t have an effect. Basically, we don’t want anyone to notice the shadow, but still have it lift the fields from the page.

Step 4: Background Gradient

While the box-shadow syntax is easy to grasp, gradients are trickier. With CSS3 gradients, you can create some amazing shapes — from dart boards to rainbows — so as you can imagine it has a more complex syntax. Thankfully, we don’t need to code a rainbow today; we just need a straight linear gradient.

Syntax for Webkit:

-webkit-gradient( linear, <start>, <end>, from(<color>), to(<color>) )

Syntax for Gecko:

-moz-linear-gradient(<start> <angle>, <color>, <color>)

As you can see, the methods are quite different, so this will require some explaining.

Webkit gradients require a start point (X and Y), an end point (X and Y), a from-color, and a to-color. The angle is determined by where start and end are, and the gradient will be colored with the “from(color)” fading to “to(color)”.

Gecko gradients, on the other hand, require only a start point (Y), and at least two colors. If you want a gradient going from top to bottom (90deg) you don’t need to assign an angle.

So to get a simple linear gradient from top to bottom – black to white – we would do like this:

background: -webkit-gradient(linear, left top, left bottom, from(#000000), to(#FFFFFF));
background: -moz-linear-gradient(top, #000000, #FFFFFF);

And it would appear like this:

(I will continue to use the black color for demonstration; at the end, I’ll switch to the real color we will be using for the form.)

Now that we have the basics out of the way, we can start making the form look how we want. The first thing we want to do is limit the height of the gradient so that it looks the same for both input fields and textarea; otherwise the gradient would fill the entire height, like this:

This is how we limit the background gradient to 25px in Webkit and Firefox:

input, textarea { 
	background: -webkit-gradient(linear, left top, left 25, from(#000000), to(#FFFFFF));
	background: -moz-linear-gradient(top, #000000, #FFFFFF 25px);
	}

For Webkit, instead of setting the end point to “left bottom,” we set it to “left 25″, indicating it will end 25 pixels from the top.

For Gecko, we do the same thing by simply adding a “25px” value to the end color.

And the result is:

The second thing we want to do is create a thin white line at the top of the gradient, to give the subtle visual impression that the field is raised. How important can a single pixel be? Take a look at this article: Adding Depth with Pixel Perfect Line Work.

To create this, we’ll need three points in the gradient. In the previous example, our gradient had two points: top and bottom (black→white). Here, we’ll add an additional point in between them (white→black→white).

To illustrate:

How do we do this?

input, textarea {
	background: -webkit-gradient(linear, left top, left 25, from(#FFFFFF), color-stop(4%, #000000), to(#FFFFFF));
	background: -moz-linear-gradient(top, #FFFFFF, #000000 1px, #FFFFFF 25px);
	}

In Webkit we use the color-stop function, but unfortunately it doesn’t support values in pixels, only percentage. But thanks to paying attention to math in school we figure that 4% of 25px is 1px.

For Gecko, we simply add a third color between the first two and give it a “1px” value, indicating that it should end 1 pixel from the top.

The thin white line:

Now, let’s change the black color (#000000) to a more fitting light grey (#EEEEEE):

Just some small detail work remains.

First, we’ll create a darker shadow for the fields when the user hovers or selects it:

input:hover, textarea:hover,
input:focus, textarea:focus { 
	-webkit-box-shadow: rgba(0, 0, 0, 0.15) 0px 0px 8px;
	}

It’s just an increase from 10% to 15%, but what we are after is, once again, subtlety.

The last thing we do is create some rounded corners for the button3 to further make it stand out from the other elements:

.submit input {
	-webkit-border-radius: 5px;
	-moz-border-radius: 5px;
	}

The value is the radius the corners will be rounded by. The standard border-radius is intentionally left out since Opera seems to have some problem with it.

Result:

Step 5: The Other Browsers

Now we just need to take care of the browsers that don’t support CSS3 yet (IE), or only partly does (Opera).

We want the different versions (CSS3 and the normal) to look as similar as possible, and the simplest thing is to go back to the old way: images.

Simply take a screenshot of the beautiful CSS3 form and save a small portion of the gradient as an image.

Next, use it in the input and textarea as a background. As long as the CSS3 gradients comes after the background image, browsers that support CSS3 will ignore the image.

input, textarea { 
	background: #FFFFFF url('bg_form.png') left top repeat-x;
	}

And now we are done! Enjoy your form and I hope you have learned something.

Final Preview

Chrome (4.0), Firefox (3.6), Safari (4.0):

Opera (10.50b):

Internet Explorer (8):

Full CSS

input, textarea { 
	padding: 9px;
	border: solid 1px #E5E5E5;
	outline: 0;
	font: normal 13px/100% Verdana, Tahoma, sans-serif;
	width: 200px;
	background: #FFFFFF url('bg_form.png') left top repeat-x;
	background: -webkit-gradient(linear, left top, left 25, from(#FFFFFF), color-stop(4%, #EEEEEE), to(#FFFFFF));
	background: -moz-linear-gradient(top, #FFFFFF, #EEEEEE 1px, #FFFFFF 25px);
	box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;
	-moz-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;
	-webkit-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;
	}

textarea { 
	width: 400px;
	max-width: 400px;
	height: 150px;
	line-height: 150%;
	}

input:hover, textarea:hover,
input:focus, textarea:focus { 
	border-color: #C9C9C9; 
	-webkit-box-shadow: rgba(0, 0, 0, 0.15) 0px 0px 8px;
	}

.form label { 
	margin-left: 10px; 
	color: #999999; 
	}

.submit input {
	width: auto;
	padding: 9px 15px;
	background: #617798;
	border: 0;
	font-size: 14px;
	color: #FFFFFF;
	-moz-border-radius: 5px;
	-webkit-border-radius: 5px;
	}

Conclusion

That’s all there is to it! With minimal effort, and the power of CSS 3, we’ve turned a bland and ordinary form into something beautiful. Thanks so much for reading, and feel free to ask any questions that you might have below.

Write a Plus Tutorial

Did you know that you can earn up to $600 for writing a PLUS tutorial and/or screencast for us? We’re looking for in depth and well-written tutorials on HTML, CSS, PHP, and JavaScript. If you’re of the ability, please contact Jeffrey at nettuts@tutsplus.com.

Please note that actual compensation will be dependent upon the quality of the final tutorial and screencast.

Write a PLUS tutorial

Tags: forms
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://phisticcoat.com/ goku

    It’s very nice and useful. thanks for your nice tutorial.

  • Impressed

    This is awesome! Thanks so much for sharing!

  • http://www.pascalpoffet.ch Paskuu

    awesome tutorial…thank you for sharing!

  • http://www.key-logger.ws keylogger

    This is awesome! Thanks so much for sharing!

  • http://www.ad-deka.com ian

    thank’s a lot, very helpful

  • http://smkfarmasi-banisaleh.com Andhi

    It’s nice design. Thanks for sharing

  • http://www.twitter.com/CristianGRojas @CristianGRojas

    Good demostration. I like this tutorial! ;)

  • http://www.lasik-koeln.info/faltenbehandlung/botox/ Botox Klaus

    It’s nice design and tutorial, thank’s…

  • http://www.lkeria.com annonce algerie

    Wonderful. Thanks for the share

  • http://www.sonnydesign.com sawebdesigns

    thanks for sharing very good design

  • http://www.sonnydesign.com sawebdesigns

    Thank you for sharing this really great

  • Paul

    Great tut. Easy for consumption and practice.

  • http://www.lasik-koeln.info/faltenbehandlung/lidstraffung/ Lidstraffung

    It’s a really very nice design! The tutorial is also organized very well and it’s easy to understand. Thank you!

  • http://www.metacafe.com/w/5811852 antiviral immune

    Nice post. I learn one thing more difficult on totally different blogs everyday. It’s going to all the time be stimulating to learn content from different writers and practice just a little one thing from their store. I’d prefer to make use of some with the content material on my blog whether or not you don’t mind. Natually I’ll give you a hyperlink in your web blog. Thanks for sharing.

  • Franco Musso

    That was extremely helpful.
    Previously forms always let my css templates down, looking very amateur whereas now they look great.
    Particularly helpful was knowing how to cater for older browsers as I thought I’d have to stick to CSS2 for IE, but this is great… loving the gradients and shadows.
    Thanks again!

  • joe

    Hi,

    Trying to test this on test server using WAMP but keep getting these errors.

    Notice: Undefined variable: extra in C:\wamp\www\contact_form\contact_form\phpMailer\class.smtp.php on line 173

    Warning: stream_socket_enable_crypto() [streams.crypto]: this stream does not support SSL/crypto in C:\wamp\www\contact_form\contact_form\phpMailer\class.smtp.php on line 194

    Deprecated: Function eregi() is deprecated in C:\wamp\www\contact_form\contact_form\phpMailer\class.phpmailer.php on line 594

    Any ideas why?

    Thanks

  • http://www.uitmuntend-webdesign.nl Webdesign Eindhoven

    Awesome, thanks for making this tutorial!

  • http://twitter.com/bliozer luiz bliozer

    gradient from the tip to use IE:

    .submit {
    background-image: filter: progid:DXImageTransform.Microsoft.gradient(startColorStr=’#666666′, EndColorStr=’#000000′);
    -ms-filter: “progid:DXImageTransform.Microsoft.gradient(startColorStr=’#666666′, EndColorStr=’#000000′)”;
    }

    #666666 => top-color
    #000000 => bottom-color

    works well with buttons / submit

    ;)

  • http://meteo-algerie.org/ Annonce Algerie

    Thanks I will use this for my site immo Algerie

  • http://echeapvibrators.com Cheap Vibrators

    A very usefull article – A big thank you I hope you dont mind me blogging about this post on my blog I will also link back to this post Thanks

  • caztillo

    Thank you, very useful ;>

  • http://www.mojowebdesign.com.au Damian

    Great looking result. A lot of work especially if your in a commercial environment but it does give a nice end product

  • http://www.uwudamith.wordpress.com Damith wanninayake

    Great.Thanks for sharing knowledge.

  • http://www.csstrophy.com Mack

    Great form tutorial I am sure I’ll use it in my next website!

  • Joeyj01

    Design the prettiest forms with a form builder.http://bit.ly/lJrEzs

  • http://www.pina-colada.dk Thore

    Thanks you!! Just what i was looking for..

  • http://mastersofmobile.com/ Andy

    Love it, thank you!

  • http://paginainteligente.net Rafael

    incredibly neat and well explained. you are great. it s a pleasure to work like this. keep up the good good work.

    thank you very very very much.

  • http://www.megavox.dk Daniel Frank

    Great tutorial, but since I included this in my code, page continuously throws an exception.

    “Error in: http://localhost:3669/megavox/Css/bg_form.png. Error Message: File doesn’t exist.”

    But no “form_bg” in CSS, and I removed the file.

    padding: 9px;
    border: solid 1px #E5E5E5;
    outline: 0;
    font: normal 13px/100% Verdana, Tahoma, sans-serif;
    width: 200px;
    background: #FFFFFF left top repeat-x;
    background: -webkit-gradient(linear, left top, left 25, from(#FFFFFF), color-stop(4%, #EEEEEE), to(#FFFFFF));
    background: -moz-linear-gradient(top, #FFFFFF, #EEEEEE 1px, #FFFFFF 25px);
    box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;
    -moz-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;
    -webkit-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;

    That’s just great!

  • http://www.algerie-immobilier.com Immobilier Algerie

    This is awesome! and veruy usefull, Thanks so much for sharing!
    Great tutorial thanks again

  • http://svenskasakerhetsdorrar.se/?ap_id=pivot svenska säkerhetsdörrar

    Awesome tutorial. Thanks!

    Do you know if there is a standard for this now? (So we don’t have to use moz/webkit stuff?)

  • http://www.travelutb.com/rwenzori.html Rwenzori Mountain National Park

    Thank for the tutorial very helpful.

  • http://www.expresnt.com expresnt

    Thanks you!! Just what i was looking for..

  • Cameron Reid

    Nice post. I found this blog with quick tips to improve your web form conversionhttp://blog.caspio.com/webforms/how-to-create-a-web-form-that-converts/

  • http://www.dlaala.com/ dlaala

    Merci beaucoup pour ce très bon tutoriel, et pour la valeur ajoutée que vous apportez aux utilisateurs, merci encore pour ce partage
    http://www.dlaala.com
    L’équipe dlaala

  • http://youtube.com Walle

    Awesome tut! Helped alot :)

  • http://www.hdresim.net Kursad

    Thank for the tutorial very helpful.

  • http://www.istudio.com.mx/index.html Alfredo Ramirez

    Once again nettuts+ saved my day, thanks again!!

  • http://www.formation-aide-soignante.net formation aide soignante

    Hi thank you for all this information very nice blog and i find some think interesting here for waiting other information see you later

  • Jeff

    Not sure how to get this to function within a div layer? I linked the css file to my html, and when I put the code inside the div, it seems to loose the styles. It also makes the div layer scroll up and down, and ignores my styles that make the page expand. Anyone have a fix?

    Thanks

  • http://www.luisvillamarin.com Luis

    Very nice and helpful. Great source to learn cool css tricks for forms.

  • http://elyounssi.com/ Wahib El Younssi

    The form of a simple but wonderful !

  • himanshu verma

    nice tutorial .so much thanks to you

  • Prasad

    nice site

  • http://royjulien.com/its.swiss Otc

    Nice tut here.

    Another way to recreate the color-stop effect that only works on certain browsers you can add an outline to the border:

    input, textarea {
    border:1px solid white; /* Creates a white border around your field */
    outline:1px solid silver; /* Creates a silver border around the border */
    }

  • carolina buitrago

    excellent tutorial. I tried with a list but does not work, take some styles but not shown as the textarea. As in the case would be a list?

  • MIke

    Your code works fine which is great but how do you link this so it sends an e-mail to a desired employee with the form contents without opening up an e-mail package such as outlook.
    many thanks,
    Mike

    • julia wakeham

      Hi Mike, I was just wondering if you ever worked out how to get this to work so that it send an email to the desired person, ie if its on my website, it sends emails to my gmail account?
      Any suggestions you would have would be great.

      Thanks

      Julia

      • Radu

        You have to use a programming language for that, like PHP. This is just mark-up and design.

  • http://elive.vn elive

    nice form templates. Thank so much!

  • http://www.tuljabhavani.in Lasane Guruji, Tuljapur

    Thanks for doing this project.
    Well Done.

  • resul
    • julia wakeham

      Hello,
      Thanks for your reply. I tried clicking on the link and nothing happens. Just a blank screen. Any suggestions?
      Thanks again.

      • icank

        thanks for demo…. like. :)