The HTML <input> tag is used to create interactive form controls that allow users to enter data in a webpage. It supports multiple input types such as text, password, email, number, and more for collecting user information.
- Used inside the <form> element to take user input.
- Can include attributes such as placeholder, required, readonly, and disabled.
- Plays a crucial role in form validation and user interaction.
<html>
<body>
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</form>
</body>
</html>
- The <label> element with the for attribute associates the label "Username:" with the <input> field, enhancing accessibility.
- The <input> element of type "text" creates a single-line text input field where users can enter their username.
Syntax:
<input type="text" ... />Note: The <input> Tag supports the Global Attributes & the Event Attributes in HTML.
Input Tag Types
<input> types specify the kind of data a user can enter in a form, such as text, email, password, or number.
| Input Type | Description |
|---|---|
| <input type="text"> | Single-line text input |
| <input type="password"> | Masked text input for passwords |
| <input type="checkbox"> | Toggle for selecting multiple options |
| <input type="radio"> | Single selection from multiple options |
| <input type="submit"> | Button to submit form data |
| <input type="button"> | General-purpose button |
| <input type="file"> | Input for uploading files |
| <input type="number"> | Input for numerical values |
| <input type="date"> | Input for selecting dates |
| <input type="email"> | Input for email addresses |
| <input type="color"> | Input for selecting colors |
| <input type="range"> | Slider for selecting a numeric value within a range |
| <input type="hidden"> | Hidden input for form data |
| <input type="image"> | Input using an image for form submission |
Attributes
Attributes provide additional information and control the behavior of HTML elements, such as type, name, value, and required for the <input> tag.
Attributes | Descriptions |
|---|---|
It is used to specify the type of the input element. Its default value is text. | |
It is used to specify the value of the input element. | |