
- HTML - Home
- HTML - Roadmap
- HTML - Introduction
- HTML - History & Evolution
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - Input Attributes
The HTML <input>
tag provides different attributes to define its characteristics and behavior.
Input Attributes
The HTML input attributes define the characteristics and behavior of the <input> element. These input attributes are used with the different types of input fields, such as text, email, password, date, number, and so forth. Note that the input element is used to create interactive controls for the web-based forms so that it can accept data from the user.
The <input>
element requires only an opening tag, and it will work only if we add it in between the <form>
tags. In this tutorial, we are going to explore the attributes that are used with the <input>
element.
The attributes of the <input>
element are as follows −
- type and name
- value
- size
- maxlength
- readonly
- disabled
- min and max
- accept and multiple
- placeholder
- required
- autofocus
- list
The 'type' and 'name' Attributes
The type
attribute indicates the type of input control, like text, password, email, and so on. The name
attribute of an input element assigns an identifier to the form control that enables the server to recognize and retrieve the value.
Example
The following HTML code illustrates the use of type
and name
attributes:
<!DOCTYPE html> <html> <head> <title>The type and name Attributes</title> </head> <body> <form > First name: <input type = "text" name = "first_name" /> <br><br> Last name: <input type = "text" name = "last_name" /> </form> </body> </html>
The 'value' Attribute
The value
attribute is used to provide an initial value inside the input control.
Example
In the following example, we are creating two input fields with initial value as " first name..." and " last name...":
<!DOCTYPE html> <html> <head> <title>The value Attribute</title> </head> <body> <form > First name: <input type = "text" name = "first_name" value = "first name..." /> <br><br> Last name: <input type = "text" name = "last_name" value = "last name..."/> </form> </body> </html>
The 'size' Attribute
The size
attribute allows you to specify the width of the text-input control in terms of characters. The default size is 20 characters.
Example
In this example, the size of the text-input control is set to 40:
<!DOCTYPE html> <html> <head> <title>The size Attribute</title> </head> <body> <form > First name: <input type = "text" name = "first_name" size = "40" /> <br><br> Last name: <input type = "text" name = "last_name" size = "40"/> </form> </body> </html>
The 'maxlength' Attribute
The maxlength
attribute allows you to specify the maximum number of characters a user can enter into the text box.
Example
The following example demonstrates how to set the maxlength
of an input field:
<!DOCTYPE html> <html> <head> <title>The maxlength Attribute</title> </head> <body> <form > First name: <input type = "text" name = "first_name" /> <br><br> Last name: <input type = "text" name = "last_name" /> <br><br> Contact: <input type = "text" name = "phone" maxlength = "10"/> </form> </body> </html>
The 'readonly' Attribute
The readonly
attribute of an input field indicates the field as read-only. Although the content of a read-only field cannot be altered, users can still select it and copy the text. Also, the value of a read-only field is included when the form is submitted.
Example
The following example shows the use of the readonly
attribute of the <input>
element:
<!DOCTYPE html> <html> <head> <title>The readonly Attribute</title> </head> <body> <form > Emp. Name: <input type = "text" name = " your_name" value = "your name..."/> <br><br> Emp. Email: <input type = "text" name = "mail" value = "your email..."/> <br><br> Organization: <input type = "text" name = "organization" value = "Tutorialspoint" readonly/> </form> </body> </html>
The 'disabled' Attribute
The disabled
attribute of an input field indicates the field as disabled. Unlike readonly, the value of a disabled field will not be included when the form is submitted.
Example
In this example, the field containing the organization name is marked as disabled:
<!DOCTYPE html> <html> <head> <title>The disabled Attribute</title> </head> <body> <form > Emp. Name: <input type = "text" name = "your_name" value = "your name..."/> <br><br> Emp. Email: <input type = "email" name = "mail" value = "your email..."/> <br><br> Organization: <input type = "text" name = "organization" value = "Tutorialspoint" disabled/> </form> </body> </html>
The 'min' and 'max' Attributes
The min
and max
attributes determine the minimum and maximum values, respectively, of an input field like number, date, week, and so on. If we use them together, they will allow users to enter an input within a predefined range.
Example
In the following example, we are mentioning the minimum working hours as 3 and maximum as 8 by using the min
and max
attributes:
<!DOCTYPE html> <html> <head> <title>The min and max Attribute</title> </head> <body> <form > Emp. Name: <input type = "text" name = "your_name" value = "your name..."/> <br><br> Emp. Email: <input type = "email" name = "mail" value = "your email..."/> <br><br> Organization: <input type = "text" name = "organization" value = "Tutorialspoint" readonly/> <br><br> Working Hrs: <input type = "number" name = "working_hours" min="3" max="8"/> </form> </body> </html>
The 'accept' and 'multiple' Attributes
The accept
attribute specifies the types of files that the server will take in. If we use the multiple
attribute, it will allow the users to upload more than one file.
Example
The following HTML code can accept multiple image files:
<!DOCTYPE html> <html> <head> <title>The accept and multiple Attributes</title> </head> <body> <form> <input type = "file" name = "fileupload" accept = "image/*" multiple /> </form> </body> </html>
The 'placeholder' Attribute
The placeholder
attribute of an input field, like text, search, and email, briefly outlines the desired value of the field. Its predefined value is displayed in the input field until the user begins to enter their own value.
Example
In the following example, we are using the placeholder
attribute for the email input field:
<!DOCTYPE html> <html> <head> <title>The placeholder Attribute</title> </head> <body> <form> Emp. Name: <input type = "text" name = "your_name"/> <br><br> Emp. Email: <input type = "email" name = "mail" placeholder = "[email protected]"/> </form> </body> </html>
The 'required' Attribute
The required
attribute in an input field like text, search, password, and email signifies that the field must contain some values for the form to be successfully submitted. In other words, it indicates the mandatory field.
Example
The following example illustrates the use of the required
attribute. Without filling the mandatory fields, users will not be able to submit the form:
<!DOCTYPE html> <html> <head> <title>The required Attribute</title> </head> <body> <form > <p>The * Star represents mandatory field</p> Emp. Name: <input type = "text" name = "your_name" required/>* <br><br> Emp. Email: <input type = "email" name = "mail" placeholder = "[email protected]" required/>* <br><br> <input type = "submit"> </form> </body> </html>
The 'autofocus' Attribute
The autofocus
attribute in an input field ensures that the field must be selected automatically once the webpage loads completely. It implies that the cursor will be positioned to the specified input field. In cases where multiple elements use the autofocus
attribute, only the first element will acquire the focus.
Example
Following is the example of the autofocus
attribute:
<!DOCTYPE html> <html> <head> <title>The autofocus Attribute</title> </head> <body> <form > Emp. Name: <input type = "text" name = "your_name" autofocus/> <br><br> Emp. Email: <input type = "email" name = "mail" placeholder = "[email protected]" /> <br><br> <input type = "submit"> </form> </body> </html>
The 'list' Attribute
The list
attribute defines a set of predefined options for an <input>
element, which are defined within a <datalist>
element. The <input>
element uses a specific string as an ID to create a link to the corresponding <datalist>
element.
Example
In this example, we are creating a list of cities with the help of the list
attribute:
<!DOCTYPE html> <html> <head> <title>The list Attribute</title> </head> <body> <form > Emp. Name: <input type = "text" name = "your_name" autofocus/> <br><br> Emp. Email: <input type = "email" name = "mail" placeholder = "[email protected]" /> <br><br> Location − <input list="location" name="cities"> <datalist id = "location"> <option value="Banglore"> <option value="Hyderabad"> <option value="Patna"> <option value="Delhi"> </datalist> <input type = "submit"> </form> </body> </html>