Table of Content
The HTML input tag lets you add fields for user data in forms. You use this tag to collect text, numbers, passwords, files, and other data. It forms the backbone of any interactive form on a website.
This field gives a direct way for users to share information, which the server can then process for many purposes such as sign-up, search, or payment.
Understand the <input> element
The <input> element is an HTML tag that creates an interactive field in a form. It does not have content between an opening and closing tag because it is a self-closing element. It tells the browser what kind of user data you want to collect.
Here is the basic syntax:
<input type="text">You can add attributes to control its behavior:
<input type="email" name="userEmail" required>You place <input> inside a <form> so the browser knows how to collect and send the data.
You use <input> to create fields where people can enter or select data. Without this element, you cannot build forms that let users log in, search, upload files, or sign up.
HTML defines many types for the <input> tag. Each type tells the browser what kind of data to expect.
- text
- password
- number
- file
Let’s take each one in depth.
The text Input Tag in HTML
The text input type creates a single-line field for general text. You use it for names, search queries, or any plain text. The browser shows a basic textbox for user input.
<input type="text" name="username">Here is an example:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</form>This example collects a user’s name. It has a label to describe the field clearly.
For another example:
<form>
<label for="search">Search:</label>
<input type="text" id="search" name="search">
</form>This field lets users enter a search term.
You use it for any text data that does not need special rules like email or password.
The password Input Tag in HTML
The password type hides user input. It shows dots or asterisks instead of the characters. This keeps passwords private in the entry.
Here is the syntax:
<input type="password" name="userPassword">For example:
<form>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
</form>This example asks users to enter a secure password.
You use it when you want to hide sensitive text like passwords or security codes.
The email Input Tag in HTML
The email type ensures the user enters a valid email address. Many browsers check for the @ and other parts of a valid address.
Syntax:
<input type="email" name="userEmail">Here is an example:
<form>
<label for="contactEmail">Contact Email:</label>
<input type="email" id="contactEmail" name="contactEmail" required>
</form>This version ensures the user must fill in a valid email to submit. You can use it for sign-ups, contact forms, or any place you need a verified email.
The number Input Tag in HTML
The number type creates a field for numeric values. It shows arrows for increment and decrement. You can set min, max, and step values.
Here is an example:
<form>
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="0" max="120">
</form>This example limits age between 0 and 120. It lets users choose quantities in whole numbers.
The file Input Tag in HTML
The file type lets users choose a file from their device. The browser shows a button to open the file picker.
For example:
<form>
<label for="resume">Upload Resume:</label>
<input type="file" id="resume" name="resume">
</form>This form lets users upload one file.
Here is another example for multiple file uploads:
<form>
<label for="photos">Upload Photos:</label>
<input type="file" id="photos" name="photos" multiple>
</form>This version allows multiple files at once.
The Common Attributes
Fields have many attributes to help you do things like set the type of input, add placeholder text, limit values, control size, and handle user data. Let’s take each one in detail.
The name Attribute
It defines the key for submitted data. The server uses it to identify the field.
<input type="text" name="username">The placeholder Attribute
It shows a hint or example text in the field before the user types.
<input type="text" placeholder="Enter your name">The required Attribute
Forces the user to fill the field before submission.
<input type="email" required>The readonly Attribute
Makes the field uneditable but sends its value with the form.
<input type="text" value="FixedValue" readonly>The disabled Attribute
Disables the field completely. The user cannot interact with it, and the form does not send its value.
<input type="text" disabled>The maxlength Attribute
Limits the number of characters the user can type.
<input type="text" maxlength="10">Style <input> Elements with CSS
CSS helps you control the appearance of borders, padding, colors, and more. This way, your forms can match your website’s design.
Here is an example of how to style an <input> field with CSS:
<style>
input {
border: 2px solid #4CAF50;
padding: 10px 14px;
border-radius: 6px;
font-size: 16px;
outline: none;
transition: border-color 0.3s, box-shadow 0.3s;
}
input:focus {
border-color: #45a049;
box-shadow: 0 0 5px rgba(69, 160, 73, 0.5);
}
input::placeholder {
color: #888;
font-style: italic;
}
</style>
<input type="text" placeholder="Styled Input">This CSS does the following:
- Sets a green border with rounded corners.
- Adds padding to make the field easier to use.
- Increases the font size for better readability.
- Removes the default outline and replaces it with a soft glow on focus.
- Style the placeholder text in a softer color and italic style.
Validate Input Tag Patterns in HTML
HTML offers simple validation with attributes like:
requiredpatterntype(like email or number)
Here is an example with a pattern:
<input type="text" pattern="[A-Za-z]{3,}">This only accepts letters with at least three characters.
You can also use JavaScript to write your own rules:
<form onsubmit="return validateForm()">
<input type="text" id="username">
<button type="submit">Submit</button>
</form>
<script>
function validateForm() {
let value = document.getElementById('username').value;
if (value.length < 3) {
alert('Username must have at least 3 characters.');
return false;
}
return true;
}
</script>Examples
Simple Login Form:
<form>
<input type="text" placeholder="Username" required>
<input type="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>This form asks for a username and password before login.
Email Sign-up Form:
<form>
<input type="email" placeholder="Enter your email" required>
<button type="submit">Subscribe</button>
</form>This ensures users enter valid emails for a newsletter.
Number Selector for Quantity:
<form>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" step="1">
</form>This lets users choose how many items to order.
File Upload Form:
<form>
<label for="photo">Upload Photo:</label>
<input type="file" id="photo" name="photo">
</form>This allows users to upload a single photo.
Form with Placeholder and Maxlength:
<form>
<input type="text" placeholder="Enter name" maxlength="10">
</form>This limits the user’s input to ten characters.
Wrapping Up
In this article you learned about the HTML <input> tag, its types, attributes, validation, and examples. Here is a quick recap:
- The
<input>tag creates fields in forms for user data. - It supports many types like text, password, email, number, and file.
- Attributes such as name, placeholder, required, readonly, disabled, and maxlength help customize fields.
- Validation can be built-in with HTML or custom with JavaScript.
- Examples showed common uses to help you understand real-world form design.
FAQs
What is the tag used for in HTML?
<input type="text">What are the different types of fields?
- text – single-line text
- password – hidden text
- email – email address
- number – numeric input
- checkbox – toggle option
- radio – select one option
- file – upload a file
- submit – submit the form
How do I make an input field required?
required attribute to the input tag. This forces the user to fill in the field before the form can submit.
<input type="text" required>What does the placeholder attribute do?
placeholder attribute shows hint text inside the input field before the user types anything. It guides the user about what to enter.
<input type="text" placeholder="Enter your name">
How can I set a default value in an input field?
value attribute to set a default text that appears in the input box when the page loads. The user can edit or keep this value.
<input type="text" value="Default Text">What is the difference between readonly and disabled inputs?
- A readonly input prevents the user from changing the value but includes the value when submitting the form.
- A disabled input blocks user interaction and excludes the value from form submission.
<input type="text" value="Visible to user" readonly>
Disabled: visible, uneditable, but not submitted
<input type="text" value="Ignored by form" disabled>Similar Reads
HTML has three main list tags: <ul>, <ol>, and <dl>. Each one shows a different kind of group. In this…
The HTML canvas tag draws shapes and images. It also helps you draw text with code. It gives control over…
HTML attributes add extra detail to elements so the browser knows how to handle them. Here you will see examples…
You use the style attribute to add CSS to a single HTML element without using a stylesheet or class. What…
The HTML samp tag shows the result of a computer program or command. It helps readers know which text came…
The <wbr> tag in HTML helps the browser decide where it can break a word. It does not force a…
The <dialog> tag is used to build modal popups in HTML. It shows alerts and messages without third-party scripts. Understand…
The HTML span tag acts as a simple container for inline text or other inline elements. It groups text and…
In this article, you will learn how HTML legend works in the fieldset tag with examples. HTML fieldset and legend…
The HTML em tag shows stress or emphasis in text. It changes meaning for readers and screen readers. What is…