The <input> element in HTML has many attributes that control how the input field behaves. Here are the most common ones:
| Attribute | Description | Example |
|---|---|---|
type | Specifies the type of input field. | <input type="text"> |
name | Gives the input a name for form submission. | <input name="username"> |
id | Unique identifier for the input. | <input id="email"> |
value | Sets the default value. | <input value="John"> |
placeholder | Displays hint text inside the field. | <input placeholder="Enter your name"> |
required | Makes the field mandatory. | <input required> |
readonly | Prevents editing of the value. | <input readonly> |
disabled | Disables the input field. | <input disabled> |
maxlength | Maximum number of characters allowed. | <input maxlength="20"> |
minlength | Minimum number of characters required. | <input minlength="5"> |
min | Minimum value (for number, date, etc.). | <input type="number" min="1"> |
max | Maximum value. | <input type="number" max="100"> |
step | Specifies interval between valid values. | <input type="number" step="5"> |
size | Width of the input in characters. | <input size="30"> |
pattern | Regular expression for input validation. | <input pattern="[A-Za-z]+"> |
autocomplete | Enables or disables autocomplete. | <input autocomplete="off"> |
autofocus | Automatically focuses the field when the page loads. | <input autofocus> |
multiple | Allows multiple values (used with email or file). | <input type="file" multiple> |
accept | Specifies allowed file types. | <input type="file" accept=".pdf,.jpg"> |
checked | Pre-selects a checkbox or radio button. | <input type="checkbox" checked> |
Example
<form> <label>Name:</label> <input type="text" name="name" placeholder="Enter your name" required> <label>Age:</label> <input type="number" min="1" max="100"> <label>Email:</label> <input type="email" autocomplete="on"> <input type="submit" value="Submit"> </form>
These attributes help control validation, appearance, user interaction, and form submission.
No comments:
Post a Comment