Home arrow HTML arrow Page 2 - 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 - HTML5 autofocus attribute
(Page 2 of 3 )

Keeping focus: introducing the “autofocus” attribute

You don't need to be a rocket scientist to realize that the “autofocus” attribute does  exactly what its name suggests: effectively it puts the focus on its associated form field, pretty similar to what can be achieved through JavaScript.

It’s worth creating an example that shows how this attribute works. Below I coded one that “autofocuses” the field of a search form, similar to the one coded before. Check it out:  

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example using the 'autofocus' 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" autofocus>
                <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 previous code fragment shows, it’s really easy to build a form field that gets focus on itself, thanks to the use of the “autofocus” attribute. What’s more, at this moment the attribute is supported by most browsers (with the exception of Internet Explorer, naturally). When rendered on screen, it produces an output similar to this one:  

In the next section I’ll be setting up a couple of examples that will demonstrate how to use the “email” and “url” attributes, which obviously come in handy for letting users enter email and web addresses in a form.


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