Home arrow HTML arrow HTML5 Form Elements
HTML

HTML5 Form Elements


In this web development tutorial, I will show you how to use the new HTML5 form elements. Specifically, you will learn how to use the “placeholder”, “autofocus”, “email” and “url” attributes.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
June 08, 2011
TABLE OF CONTENTS:
  1. · HTML5 Form Elements
  2. · HTML5 autofocus attribute
  3. · HTML5 email and url attributes

print this article
SEARCH DEVARTICLES

HTML5 Form Elements
(Page 1 of 3 )

Even though most of the spotlight is shining on the bunch of new markup elements included with HTML5, such as <header>, <navbar>, <footer> and so forth (to name a few), it’s fair to mention that the specification comes bundled with other goodies that permit you, at least in theory, to code cleaner and more functional web forms, without having to put one single foot into client-side scripting.

The standard package offers a brand new set of attributes, like “placeholder”, “autofocus”, “email”, “url” and “number” (again, the list goes on) which, when coupled with the “<input> element, can be used to make a certain field in a form is filled in with a valid email or web address, a number or even a well-formatted date.

Of course, not all is so fun and delightful when it comes to using these attributes in, as at the time being many modern browsers (with a few scarce exceptions, like Google Chrome, Safari and Firefox in some cases) aren’t capable of rendering them as intended. However, there’s no reason to feel down, as the good news is that if a particular browser doesn’t understand a specific attribute, it will degrade it gracefully, in most cases to a simple text box.

This means that you can start including the attributes in your HTML5 pages without being afraid of breaking up your carefully-crafted layouts. And to give you proof of my claim, in this two-part tutorial I’ll be setting up some examples that will show you how to enrich the functionality of your web forms with the new input attributes that HTML5 provides.     

Getting started: working with the “placeholder” input attribute

As the old saying goes, a long walk starts with a small step, and in this case this roundup showcasing the new input attributes bundled with HTML5 will begin by covering one called “placeholder”. As its name suggests, when tied to the <input> element with some appropriate text, the attribute will display and hide (alternately) the text within the element’s box when focusing and blurring on it (pretty similar to what you can do with JavaScript).

To better grasp the default behavior of “placeholder”, take a look at the following web page, which shows how to use in a simple search form:

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example using the 'placeholder' input attribute</title>
<style type="text/css">
body {
    padding: 0;
    margin: 0;
    background-color: #fff;
    font: normal 0.9em Arial, Helvetica, sans-serif;
    color: #000;
}
h2 {
    margin: 0 0 10px 0;
    font-size: 2em;
    color: #666; 
}
p {
    margin: 0 0 15px 0;
    line-height: 1.3em;
}
/* main wrapper */
#wrapper {
    width: 780px;
    margin: 0 auto;
    background-color: #fff;
}
/* header, main and footer elements */
#header, #main, #footer {
    padding: 20px;
}
</style>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <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="main">
        <h2>Sample form</h2>
        <form action="processform.php" method="get">
            <fieldset>
                <input name="query" placeholder="Search Articles...">
                <input type="submit" name="search" value="Search" />
            </fieldset>
        </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>

As the above code fragment demonstrates, the use of the “placeholder” attribute is pretty easy to understand. In this particular case, it’s been dropped inside a typical search form, but naturally it can be used in a different context as well.

Besides, it has the additional blessing of being currently supported by most browsers (of course, feel free to exclude IE from the list), which turns it into a good candidate for being used in production environments. What’s more, if you test the previous HTML5 document with Chrome or Firefox, the text placed inside the search box should be hidden and shown accordingly, as you focus and blur with the mouse or with the tab key. And best of all, you don’t need to include a single piece of JavaScript to achieve this classic behavior.

The following image recreates this situation:

Now that you have a more intimate background on how to use the handy “placeholder” HTML5 attribute, it’s time to move on and start discussing the next one on the list. If you’re a designer who cares about accessibility and usability and want to make your forms get focus right after the whole web page has finished loading, you’ll appreciate the functionality of the “autofocus” attribute, as it implements this behavior right out of the box.


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-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials