Create A Clean and Stylish CSS3 Contact Form

Chris Creed / 29th June 2010

Create A Clean and Stylish CSS3 Contact Form Create A Clean and Stylish CSS3 Contact Form

Building stylish contact forms typically requires the use of images (and maybe some JavaScript) to create something that looks professional. However, with CSS3 it’s now much easier to create some nice effects that don’t require the use of any images, yet can still look clean and stylish.

In this tutorial we’re going to look at a range of different CSS3 effects – in particular, we’ll be creating background gradients, adding shadows to elements, applying some rounded corners, and adding a couple of simple animation effects.

Click the button below to view the live demo – it works in best in browsers powered by the WebKit rendering engine (i.e. Chrome and Safari), but also degrades well in Firefox, Opera, and IE.

Here’s a screenshot of what we’ll be creating:

Create The HTML Structure

To start with, create a new HTML document and add the following code inside the <body>:

<form action="#"> 
 
        <fieldset id="user-details">			
 
            <label for="name">Name:</label>
	    <input type="text" name="name" value="" /> 
 
	    <label for="email">Email:</label> 
	    <input type="email" name="email" value=""  /> 
 
	    <label for="phone">Phone:</label> 
	    <input type="text" name="phone" value=""  /> 
 
	    <label for="website">Website:</label> 
	    <input type="url" name="website" value=""  /> 
 
	</fieldset><!--end user-details-->
 
	<fieldset id="user-message">
 
	    <label for="message">Your Message:</label> 
	    <textarea name="message" rows="0" cols="0"></textarea> 
 
	    <input type="submit" value="Submit Message" name="submit" class="submit" />		
 
	</fieldset><!-- end user-message -->
 
</form>

This is quite straightforward – we’re simply creating a form that has two containers – one that displays input fields for user details (#user-details) and another that displays a textarea for users to enter their message (#user-message).

Now add the following line in your <head>:

<link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz' rel='stylesheet' type='text/css' />

This will make a nice font available from the Google Fonts directory – we then need to add some basic styling for the main document elements:

 
* { margin: 0px; padding: 0px; }
 
body { 
    margin: 0 auto; 
    background: #f5f5f5; 	
    color: #555;	 
    width: 800px; 
 
    /* make reference to the Yanone Kaffeesatz font */
    font-family: 'Yanone Kaffeesatz', arial, sans-serif;
}
 
h1 { 
    color: #555; 
    margin: 0 0 20px 0; 
}	
 
label {	
    font-size: 20px;
    color: #666; 
}
 
fieldset { border: none; }
 
#user-details { 
    float: left;
    width: 230px; 
}
 
#user-message { 
    float: right;
    width: 405px;
}
 
textarea {		
    width: 390px; 
    height: 175px; 		 		
}

Apply Some CSS3 Magic

Now onto styling the form – in each part we’ll initially add the standard CSS code first and then move on to discuss the CSS3 code that helps to enhance everything. So, to start with, add the following CSS code to style the form:

form { 
    float: left;
    border: 1px solid #ddd; 
    padding: 30px 40px 20px 40px; 
    margin: 75px 0 0 0;
    width: 715px;
    background: #fff;
}

If you check in the browser you should have something that looks like the following image:

We now want to add some rounded corners, a linear gradient background, and a drop shadow to the form – to achieve this we’re going to make use of some CSS3 code. Add the following code at the bottom of your “form” element:

form {
 
...
 
/* -- CSS3 - define rounded corners -- */	
-webkit-border-radius: 10px;
-moz-border-radius: 10px; 
border-radius: 10px;
 
/* -- CSS3 - create a background gradient -- */
background: -webkit-gradient(linear, 0% 0%, 0% 40%, from(#EEE), to(#FFF)); 
background: -moz-linear-gradient(0% 40% 90deg,#FFF, #EEE); 
 
/* -- CSS3 - add a drop shadow -- */
-webkit-box-shadow:0px 0 50px #ccc;
-moz-box-shadow:0px 0 50px #ccc; 
box-shadow:0px 0 50px #ccc;
 
}

In the above code, the “-moz” prefix is used to target Gecko based browsers (e.g. Firefox and Flock) whilst the “-webkit” prefix is used for browsers powered by the WebKit rendering engine (e.g. Chrome and Safari).

Once you’ve added this code your form should look something like the following screenshot:

As you can see, with just a few simple lines of CSS3 we’ve already significantly enhanced the appearance of the form. Now let’s add the basic code for the <input> and <textarea> elements:

input, textarea { 		
    padding: 8px; 
    margin: 4px 0 20px 0; 
    background: #fff; 
    width: 220px; 
    font-size: 14px; 
    color: #555; 
    border: 1px #ddd solid;
}

Following this, we want to add a drop shadow to each input element and a CSS3 transition effect (i.e. an animation), so that we can fade in a different background color whenever the user hovers over an input/textarea element. To do this, we initially need to add the following code:

input, textarea {
 
...
 
/* -- CSS3 Shadow - create a shadow around each input element -- */ 
-webkit-box-shadow: 0px 0px 4px #aaa;
-moz-box-shadow: 0px 0px 4px #aaa; 
box-shadow: 0px 0px 4px #aaa;
 
/* -- CSS3 Transition - define what the transition will be applied to (i.e. the background) -- */		
-webkit-transition: background 0.3s linear;					
}

The transition effect will be triggered when the user hovers over the input and textarea elements – we therefore need to add the following code to define the action to be taken on this event:

input:hover, textarea:hover { 
    background: #eee; 
}

If you now hover over an input field or the textarea you should see the background color change to grey over a short delay (0.3 seconds). Your form should now look almost complete:

It’s now time to work on the submit button – again we’ll add our standard CSS code first:

input.submit { 	
    width: 150px; 
    color: #eee; 
    text-transform: uppercase; 
    margin-top: 10px;
    background-color: #18a5cc;
    border: none;
}

Then add a CSS3 transition, gradient, and some rounded corners:

input.submit {
 
... 
 
/* -- CSS3 Transition - define which property to animate (i.e. the shadow)  -- */
-webkit-transition: -webkit-box-shadow 0.3s linear;
 
/* -- CSS3 Shadow - create a shadow around each input element -- */ 
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#18a5cc), to(#0a85a8)); 
background:  -moz-linear-gradient(25% 75% 90deg,#0a85a8, #18a5cc); 
 
/* -- CSS3 - Rounded Corners -- */
-webkit-border-radius: 4px;
-moz-border-radius: 4px; 
border-radius: 4px;
 
}

We then need to trigger the animation on the :hover event, so let’s add the code for that:

input.submit:hover { 		
    -webkit-box-shadow: 0px 0px 20px #555;
    -moz-box-shadow: 0px 0px 20px #aaa; 
    box-shadow: 0px 0px 20px #555;		
    cursor:  pointer; 
}

If you now test the code in your browser you’ll see a fairly large shadow around the button whenever you hover over it (note that this effect is only available for WebKit browsers, so you’ll need to test it in Chrome or Safari to see it working).

Your form should now be complete:

All Done …

That’s everything done – you now have a clean contact form made with nothing other than CSS. Of course, there are still some issues with implementing CSS3 in that it wont work in all browsers, but the form has been designed to degrade well in older browsers.

You can use the buttons below to take a closer look through the live demo – you can also download and have a play around with the source code.

I hope you found this useful – let me know in the comments below if you have any comments/suggestions/feedback.

Enjoy This Post? Please Consider Sharing :-)

  • Twitter
  • StumbleUpon
  • Facebook
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

About The Author

Chris Creed

Hi there, I'm Chris and the Founder of Voosh Themes. I've been designing & developing websites for over 10 years and am interested in just about anything related to design. I also have a PhD in Computer Science (with a particular focus on Interaction Design).

22 Comments

  1. June 30th, 2010 at 6:46 am

    OK, cool…thx!

    But the hardiest elements to stylish in a form are errors… ;-)

  2. KV Prajapati

    June 30th, 2010 at 7:08 am

    Awesome! great art work, indeed.

  3. June 30th, 2010 at 7:38 am

    Great looking contact form! Is it possible to integrate the form into a WordPress theme in a easy way?

    • June 30th, 2010 at 11:15 am

      Yes, integrating the form into a WordPress theme would be straightforward to do. You could use a number of different approaches – perhaps the easiest would be to create a new page template and then incorporate this code into that new template. You could then assign the template to a “Contact” page that you create in your WordPress admin area.

  4. June 30th, 2010 at 11:33 am

    Nice looking form, good work, but accessibility fail. Missing “for” attributes and of course labels.

  5. June 30th, 2010 at 11:47 am

    Nice tutorial. However, a couple things.

    First, you should also use the non-vendor specific tags as well.

    Also, instead of using DIV to split your form, you should instead use FIELDSET.

  6. Nathaniel Tucker

    June 30th, 2010 at 12:06 pm

    Why the Mozilla hate? Firefox 4 previews have css 3 transitions, add some -moz-transition in there.

  7. June 30th, 2010 at 12:09 pm

    Couple fails on an overall good tutorial..

    Never ever use * { margin: 0px; padding: 0px; }

    When you define border-radius for webkit and mozilla you forgot the actual css3 call for border-radius, both chrome and opera already support it, so you are essentially ignoring the browsers that you should be targeting. Your code should have been,

    /* — CSS3 – define rounded corners for the form — */
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;

    Same thing for box shadow

    /* — CSS3 – add a drop shadow — */
    -webkit-box-shadow:0px 0 50px #ccc;
    -moz-box-shadow:0px 0 50px #ccc;
    box-shadow:0px 0 50px #ccc;

  8. Rich

    June 30th, 2010 at 1:22 pm

    You should really use input type = email for the email and type = url for the website. You can then use the :valid and :invalid pseudo classes to add validation information.

    Also, you should add the require attributes to any things you want to be filled in – modern browsers won’t submit the form until these are filled.

  9. kl

    June 30th, 2010 at 1:46 pm

    Labels don’t use HTML label element. That’s basic thing!

    You’re using only proprietary -xx-border-radius and -xx-box-shadow properties. Include standard CSS3 ones!

  10. chase sechrist

    June 30th, 2010 at 3:09 pm

    Great, looks fine and degrades okay in IE, except for the ugly duckling, 6.
    The message box doesn’t come out aligned, and actually comes out below the contact form a little odd.

    Of course, in 2010, it’s hard to still support this browser, but there’s a considerable amount of people still using it. :(

  11. June 30th, 2010 at 3:37 pm

    Invalid markup for a form.

    Sure, there’s many ways to create forms and a lot of people debate between using ’s to split up fields, and or ’s, and or “’s”; however you missed one thing that should *always* be satisfied.

    Instead of using a tag you’re using a tag. This is no good. Label’s are meant to be in place next to fields for many reasons due to standards. One obvious functional one is that if a label is properly tied to a field and you click on that label, it’ll make that respective field active.

  12. justin

    June 30th, 2010 at 4:28 pm

    haha, your markup needs major work

  13. June 30th, 2010 at 4:32 pm

    Thanks everyone for the comments and feedback. As highlighted, there are clearly issues with this form from an accessibility perspective – my primary focus in the tutorial was to highlight the CSS3 functionality – when creating a contact form like this for a “real world” project many of the suggestions made would certainly need to be incorporated into the code/design.

  14. June 30th, 2010 at 5:13 pm

    I’ve been designing web pages for 15 years, and even I know that your form fields are labeled wrong. You have the labels as paragraph elements, when in fact, they should be elements. It’s logical, and proper HTML. Styling them as defies the contents, when they are sub-set of the form elements themselves.

    As noted before, separating the two sets of form fields with DIV elements is amateur, too. Use the HTML standard FIELDSET, which is logical.

    Then of course, you deviate from standards again by suggesting forking the CSS for 2 independent browsers (for rounded corner effects!). Fail #3.

  15. June 30th, 2010 at 7:56 pm

    I love the way it looks, but I can’t bring myself to use it because of the forking for specific browsers. I can’t wait until there is a standard implementation!

  16. July 1st, 2010 at 1:46 am

    Nice looking, but don’t use legend für the headline (h1), no fieldset and you don’t use labels (p). Nice looking is one site, the other and more important site is developing accessible and semantik code.

  17. xoxy

    July 3rd, 2010 at 5:36 am

    Two big minuses:
    * message textbox bottom is not aligned with box in left column
    * submit button is not aligned with box in left column

  18. July 3rd, 2010 at 11:26 am

    Nice looking form

  19. kkc

    July 9th, 2010 at 12:59 pm

    is it possible to link to a php page send code cant get it to work

  20. July 13th, 2010 at 3:46 pm

    NIce CSS3 “work”. I am not complaining about the XHTML, you focused on the CSS3 part and that was done nice :-)

  21. JAY

    July 20th, 2010 at 3:38 pm

    Can this be adapted to incorporate into Google Sites? Google sites don’t allow uploading of CSS and HTML files but to a certain extent one can work in Code View.

Trackbacks/Pingbacks

  1. June 30th, 2010 at 12:41 am

    [...] Create A Clean and Stylish CSS3 Contact Form [...]

  2. June 30th, 2010 at 12:07 pm

    [...] Create A Clean and Stylish CSS3 Contact Form | Voosh Themes. [...]

  3. June 30th, 2010 at 4:44 pm

    [...] more: Create A Clean and Stylish CSS3 Contact Form | Voosh Themes 29 June 2010 | Uncategorized | Trackback | del.icio.us | Stumble it! | View Count : 0 Next Post [...]

  4. June 30th, 2010 at 10:07 pm

    [...] Create A Clean and Stylish CSS3 Contact Form | Voosh Themes (tags: css3 webdesign forms web) [...]

  5. June 30th, 2010 at 11:34 pm

    [...] Contact Form | Voosh Themes Posted by moniq2ndqusef under Uncategorized Leave a Comment  Create A Clean and Stylish CSS3 Contact Form | Voosh Themes [...]

  6. July 1st, 2010 at 1:09 am

    [...] Clean and Stylish CSS3 Contact Form In this tutorial look at a range of different CSS3 effects – in particular,  creating background gradients, adding shadows to elements, applying some rounded corners, and adding a couple of simple animation effects. [...]

  7. July 2nd, 2010 at 7:34 am

    [...] Create A Clean and Stylish CSS3 Contact Form | Voosh Themes [...]

  8. July 2nd, 2010 at 8:25 am

    [...] the contact us page. I really like what is possible using css3 now, take a look at this example. Create A Clean and Stylish CSS3 Contact Form | Voosh Themes Reply With Quote + Reply to Thread « Fictional Designer's [...]

  9. July 3rd, 2010 at 12:55 pm

    [...] to Mention Your Ph.DBy Daniel Miessler on July 3rd, 2010: Tagged as PresentationI was scoping out a nifty little contact form done completely in CSS3 and got to the bottom where it talked about the author of the site. [...]

  10. July 5th, 2010 at 2:03 am

    [...] Css3: creare un modulo contatti clean & stilish [...]

  11. July 6th, 2010 at 3:06 am

    [...] Stack Gallery with jQuery and CSS3Creating Bar Graphs using jQuery UISimple AJAX Commenting SystemCreate A Clean and Stylish CSS3 Contact FormSuper Cool CSS Flip Effect with Webkit AnimationCreate an interactive Google map widget with a css [...]

  12. July 7th, 2010 at 4:35 am

    [...] Create A Clean and Stylish CSS3 Contact Form [...]

  13. July 9th, 2010 at 10:28 am

    [...] Create A Clean and Stylish CSS3 Contact Form | Voosh Themes [...]

  14. July 23rd, 2010 at 4:27 pm

    [...] 29. Create A Clean and Stylish CSS3 Contact Form [...]

  15. August 11th, 2010 at 3:49 pm

    [...] 49. Create A Clean and Stylish CSS3 Contact Form [...]

  16. August 15th, 2010 at 1:19 pm

    [...] 49. Create A Clean and Stylish CSS3 Contact Form [...]

  17. August 30th, 2010 at 2:55 pm

    [...] Create A Clean and Stylish CSS3 Contact Form [...]

Leave a Reply