If you find this helpful, please click the Google Button to the left, if it is white, to make it turn blue or red. Thank you! (It also helps find this page again more easily.) |
The <input> Tag in HTML 5
The <input> tag is used to create an interactive control in an HTML form. For example, <input type="text"> creates a single-line text input field. For multiple lines of text input, use the <textarea> tag instead.
This is an actual working demo of the <input> example code below.
Since autofocus
automatically positions the cursor to the input field, the placeholder
text for the Username field will not appear unless you tab to the masked password field or click elsewhere. If your browser does not support the HTML 5 placeholder
attribute, none of the placeholders will appear.
Note: The first browser where placeholder text in form input fields actually works is Chrome. It may not work yet in other browsers.
<input/> Tag Syntax
<body> ... ... flow content expected ... <form id="form-id" method="GET|POST|etc." action="target-URL"> ... phrasing content expected ...<input .../>... ... </form> ... ... phrasing content expected ...<input form="form-id"/>... ... </body>
Rules for coding HTML input elements
Make sure you understand the difference between a tag and element and are familiar with the definitions of namespace and other HTML terms.
- Code the input element where phrasing content is expected, usually inside a form element.
- The input element consists of a standalone <input/> tag. The element name uses lower case letters and should be in the HTML namespace, which it will pick up automatically from the
xmlns
attribute on the <html> tag. - If the input is for a form that can be submitted but is outside that form element, include a
form
attribute referencing the form the input is to be associated with. - Include any other attributes on the <input> tag as appropriate.
- Since the input element is a void element, it should always be coded as a self-closing tag terminated with the delimiter string
/>
.
<input/> Content Model
Contents of the input element
Content: Empty. All properties are coded using attributes.
Since the <input/>
tag is a void element, it is not allowed to have any content, even HTML comments and therefore should always be coded as a self-closing standalone tag, ending with the delimiters />
rather than just >
(<input .../>
).
<input> Tag Attributes
Attributes of the <input> tag
global attributes | In addition to the personal attributes of the <input> tag below, any of the common HTML attributes can also be coded. |
accept |
|
alt |
|
autocomplete |
|
autofocus="autofocus" |
Sets the value of the <input autofocus> boolean attribute to true . Omitting it sets to false . |
checked="checked" |
Sets the value of the <input checked> boolean attribute to true . Omitting it sets to false . |
disabled="disabled" |
Sets the value of the <input disabled> boolean attribute to true . Omitting it sets to false . |
form |
|
formaction |
a URI reference (or IRI reference depending on the Use percent escape codes as explained in the URL Encoding Tutorial for any special characters in the URI reference. If the value of the
If the protocol scheme, username, host name/IP address and port number are omitted the default is the current host - the same server as the base of the current document. If the path starts with a slash |
formenctype="application/x-www-form-urlencoded" formenctype="multipart/form-data" formenctype="text/plain" |
|
formmethod="GET" formmethod="POST" formmethod="PUT" formmethod="DELETE" |
|
formnovalidate |
|
formtarget |
|
height |
|
list |
An <input> tag with a list attribute that references the id attribute of a <datalist> tag is used for creating an HTML combo box. |
max |
|
maxlength |
|
min |
|
multiple="multiple" |
Sets the value of the <input multiple> boolean attribute to true . Omitting it sets to false . |
name |
The name that will be sent with a GET or POST request, which allows the server to access the value of the input field. For a group of radio buttons, all input elements in the same radio button group should have the same value for their name attribute. For other <input type>s, the value of the name attribute should be unique. |
pattern |
|
placeholder |
|
readonly="readonly" |
Sets the value of the <input readonly> boolean attribute to true . Omitting it sets to false . |
required="required" |
Sets the value of the <input required> boolean attribute to true . Omitting it sets to false . |
size |
|
src |
a URI reference that resolves to the URL of a two-dimensional image resource Use percent escape codes as explained in the URL-encoding tutorial for any special characters in the URI reference. If the value of the
If the protocol scheme, username, host name/IP address and port number are omitted the default is the current host - the same server as the base of the current document. If the path starts with a slash |
step |
|
type="button" type="checkbox" type="color" type="date" type="datetime" type="datetime-local" type="email" type="file" type="hidden" type="image" type="month" type="number" type="password" type="radio" type="range" type="reset" type="search" type="submit" type="tel" type="text" type="time" type="url" type="week"
|
The
|
value |
|
width |
<input> Tag Examples
Examples of the input
tag in HTML 5
Input field with placeholder text and autofocus on input field in HTML form
(see input demo above)
<p style="text-align: right"> <label for="ex1lang">Current language:</label> <select id="ex1lang" form="#ex1form"> <option value="en" selected="selected">English</option> ... </select> </p> ... <form id="ex1form"> <style scoped="scoped"> input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { font-family: Verdana; color: #006633; font-style: italic } input:-moz-placeholder, textarea:-moz-placeholder { font-family: Verdana; color: #006633; font-style: italic } </style> <fieldset> <legend>Login credentials</legend> <label for="ex1user">Username:</label> <input id="ex1user" name="username" required="required" autofocus="autofocus" size="30" maxlength="15" placeholder="Your username" /><br/> <label for="ex1pass">Password: </label> <input id="ex1pass" name="password" required="required" type="password" size="30" maxlength="15" placeholder="Your password" /> </fieldset> <fieldset> <legend>Contact information</legend> <label for="ex1addr">Email address:</label> <input id="ex1addr" name="emailaddr" required="required" type="email" size="60" maxlength="80" placeholder="Your e-mail address" /><br/> <label for="ex1site">Web address: </label> <input id="ex1site" name="siteurl" required="required" type="url" size="60" maxlength="80" placeholder="Your web site URL" /> </fieldset> <label for="ex1date">Date:</label> <input id="ex1date" name="date" type="date"/>   <label for="ex1age1">Age:</label> <input id="ex1age1" type="range" min="18" max="99" value="25" onchange="document.getElementById('ex1age2').value = this.value" /> <input id="ex1age2" size="3" value="25" onchange="if (this.value < 18) this.value = 18; if (this.value > 99) this.value = 99; document.getElementById('ex1age1').value = this.value" /> </form>
Since the Current language
field is outside the HTML form, it is associated with the form with a form
attribute.
The "username" field is a type="text"
input field by default.
WebKit (which is used by Chrome) ignores CSS styles which have a selector that it does not recognize, even if the other CSS selectors are valid. Therefore, the CSS style for the placeholder attributes must be duplicated for Chrome and Firefox because following CSS code does not work:
<style scoped="scoped"> input::-webkit-input-placeholder, textarea::-webkit-input-placeholder, input:-moz-placeholder, textarea:-moz-placeholder { font-family: Verdana; color: #006633; font-style: italic } </style>
Changes in HTML 5 - <input> Tag
What's new in HTML 5
New input types:
- <input type="color" .../>
- <input type="date" .../>
- <input type="datetime" .../>
- <input type="datetime-local" .../>
- <input type="email" .../>
- <input type="month" .../>
- <input type="number" .../>
- <input type="range" .../>
- <input type="search" .../>
- <input type="tel" .../>
- <input type="time" .../>
- <input type="url" .../>
- <input type="week" .../>
A new set of attributes have been added which can be used to override various attributes on the <form> tag.
<input> attribute | overrides <form> |
---|---|
formaction | action |
formenctype | enctype |
formmethod | method |
formnovalidate | novalidate |
formtarget | target |
In addition, the autocomplete="autocomplete", autofocus="autofocus", form, list, max, min, multiple="multiple", pattern, placeholder, required="required" and step attributes have been added.
Differences between HTML 5 and earlier versions of HTML
The following attributes should not be coded on the <input> tag because they either have been deprecated or were never officially supported:
align
ismap
- use the <img> tag and itsismap
attribute insteadusemap
- use the <img> tag and itsusemap
attribute insteadvalign
The 2000-2010 Recommendations from the W3C HTML Working Group defined the HTML namespace for the input element type name along with the names of all HTML element types. In older (pre-2000) versions of HTML, element type names were not associated with a namespace.