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.
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.