Home arrow HTML arrow Page 2 - Styling HTML Forms with CSS
HTML

Styling HTML Forms with CSS


This web design tutorial will focus on teaching you how to style your HTML forms and form elements with CSS (cascading style sheets).

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 24
July 08, 2011
TABLE OF CONTENTS:
  1. · Styling HTML Forms with CSS
  2. · Adding CSS to an HTML Form

print this article
SEARCH DEVARTICLES

Styling HTML Forms with CSS - Adding CSS to an HTML Form
(Page 2 of 2 )

Defining a few straightforward styles: polishing the form’s visual presentation

As you may have guessed, improving the visual presentation of the earlier contact form is as easy as assigning some basic styles to its elements and the wrapping list.

However, the best manner to grasp the underlying logic of the process in question is by showing some functional code. In keeping with this idea, below I defined a short snippet, which takes advantage of the functionality offered by some CSS2/CSS3 properties to make the form look a bit more polished. Take a peek at it:

/* form styles */
form {
    width: 450px;
    padding: 30px;
    background: #d5cea6 url("../images/bg_form.png") left top repeat-x;
    border-radius: 13px;
    box-shadow: 4px 4px 8px #565555;
    -moz-box-shadow: 4px 4px 8px #565555;
    -webkit-box-shadow: 4px 4px 8px #565555;
    border: 1px solid #3a3723;
}
form fieldset {
    border: none;
}
form fieldset ol {
    list-style: none;
    padding: 0;
    margin: 0;
}
form fieldset ol li {
    margin-bottom: 10px;
}
form fieldset ol label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
    text-transform: capitalize;
    color: #3a3723;
}
form fieldset ol label em {
    font-style: normal;
    font-weight: normal;
    color: #3a3723;
}
form fieldset ol input.text {
    width: 250px;
    padding: 3px;
    border: 1px solid #3a3723;
}
form fieldset ol textarea {
    width: 250px;
    padding: 3px;
    border: 1px solid #3a3723;
}
form fieldset ol input.submit {
    width: 90px;
    height: 30px;
    background: #5b8b41 url("../images/bg_button.png") left top repeat-x;
    font-weight: bold;
    color: #fff;
    border: 1px solid #3a3723;
    cursor: pointer;
    cursor: hand;
}

As you can see, all this CSS fragment does is style the list that wraps the sample form, along with the pertaining elements, including the <fieldset>, <label>, <input> and <textarea> selectors. Aside from adding some fancy shadows and a few nifty rounded corners to the form via CSS3, in this case I utilized a couple of background images, which have been included below:

While at this stage I’m sure that you already grasped how the previous CSS styles do their thing, admittedly they’re pretty useless, at least from a didactical standpoint, as it’s hard to see the impact that they produce in the form’s visual presentation. But don’t feel concerned; in the segment to come I’ll be showing you how the form looks after adding the aforementioned styles.

Putting all of the pieces together: showing the form’s finished version

If you’re anything like me, it’s probable that at this moment you feel eager and want to see how the contact form is rendered on screen, once its visual presentation has been improved with the previous CSS styles. Well, leave your anxiety behind and check the following code samples, which show the form’s final version:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Styling an HTML form with CSS</title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
</head>
<body>
<div id="wrapper">
    <div id="header">
        <h1>Styling an HTML form with CSS</h1>
        <h2>Header section</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
    </div>
    <div id="content">
        <h2>Contact form</h2>
        <form action="processform.php" method="post">
            <fieldset>
                <ol>
                    <li>
                        <label for="subject">subject: <em>[required]</em></label>
                        <input type="text" name="subject" id="subject" class="text" />
                    </li>
                    <li>
                        <label for="email">email: <em>[required]</em></label>
                        <input type="email" name="email" id="email" class="text" />
                    </li>
                    <li>
                        <label for="comments">comments:</label>
                        <textarea rows="10" cols="20" name="comments" id="comments"></textarea>
                    </li>
                    <li>
                        <input type="submit" name="send" value="Send" class="submit" />
                    </li>
                </ol>
            </fieldset>
        <div id="errors"></div>
        </form>
    </div>
    <div id="footer">
        <h2>Footer section</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
    </div>
</div>
</body>
</html>

 

(css/styles.css)

/* layout and heading elements styles */
body {
    padding: 0;
    margin: 0;
    background: #000;
    font: 0.8em Arial, Helvetica, sans-serif;
    color: #fff;
}
h1 {
    font-size: 4.5em;
    text-shadow: 0 0 4px #3c3c3c;
}
h2 {
    padding-bottom: 10px;
    margin: 0 0 30px 0;
    font-size: 3em;
    color: #fff;
    text-shadow: 0 0 4px #3c3c3c;
}
h3 {
    font-size: 1.5em;
    color: #fff;
}
/* main wrapper */
#wrapper {
    width: 650px;
    margin: 0 auto;
    background: #42652f;
}
/* main sections */
#header, #content, #footer {
    padding: 20px;
}
/* form styles */
form {
    width: 450px;
    padding: 30px;
    background: #d5cea6 url("../images/bg_form.png") left top repeat-x;
    border-radius: 13px;
    box-shadow: 4px 4px 8px #565555;
    -moz-box-shadow: 4px 4px 8px #565555;
    -webkit-box-shadow: 4px 4px 8px #565555;
    border: 1px solid #3a3723;
}
form fieldset {
    border: none;
}
form fieldset ol {
    list-style: none;
    padding: 0;
    margin: 0;
}
form fieldset ol li {
    margin-bottom: 10px;
}
form fieldset ol label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
    text-transform: capitalize;
    color: #3a3723;
}
form fieldset ol label em {
    font-style: normal;
    font-weight: normal;
    color: #3a3723;
}
form fieldset ol input.text {
    width: 250px;
    padding: 3px;
    border: 1px solid #3a3723;
}
form fieldset ol textarea {
    width: 250px;
    padding: 3px;
    border: 1px solid #3a3723;
}
form fieldset ol input.submit {
    width: 90px;
    height: 30px;
    background: #5b8b41 url("../images/bg_button.png") left top repeat-x;
    font-weight: bold;
    color: #fff;
    border: 1px solid #3a3723;
    cursor: pointer;
    cursor: hand;
}

If at this point, you try out the above web page using a browser that supports the “text-shadow” and “box-shadow” CSS3 properties, the appearance of the form should be quite similar to the one depicted by the below snapshot:

Taking into account that the whole styling process only required to define a few basic CSS styles, along with a couple of background images, the result is more than acceptable. And best of all, the form was seated from the very beginning on good and clean markup, which is a big bonus that definitively contributes to its accessibility and usability.

So, armed with the code samples that were coded before, you shouldn’t have major problems making your own forms look stylish and elegant in a few simple steps. Thus, wait are you waiting for? Go ahead and do it! 

Final thoughts

Over the course of this tutorial, I provided you with some basic pointers, which hopefully will help you to build semantic HTML forms that look engaging and sleek as well. As you just saw, the entire styling process is fairly simple to master, even though it might be a bit more complex, specially when dealing with selects. In either case, make sure to take advantage of the brand new properties bundled with CSS3, something that will save you from opening Photoshop for creating a few background images.

See you in the next web design tutorial!  

 


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
HTML ARTICLES

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials