HTML Input Attributes
HTML Input Attributes
Section titled “HTML Input Attributes”The <input> element supports numerous attributes to control its behavior, appearance, and validation.
Common Input Attributes
Section titled “Common Input Attributes”type: Defines the type of input control (e.g., text, email, password, checkbox, radio, submit, file, date, number). This is the most fundamental attribute.
id: Provides a unique identifier for the element. Crucial for associating <label> elements using the for attribute and for targeting with CSS and JavaScript.
name: Specifies the name of the input control. This name is sent along with the input’s value when the form is submitted. Required for data submission.
value: Defines the initial value of the input field, or the value sent upon submission for certain types (like radio or checkbox).
Example:
<form action="/update" method="post"> <div> <label for="user_email">Email:</label> <input type="email" id="user_email" name="email" value="default@example.com"> </div> <div> <input type="radio" id="opt_yes" name="subscribe" value="yes" checked> <label for="opt_yes">Yes, subscribe</label> </div> <div> <input type="radio" id="opt_no" name="subscribe" value="no"> <label for="opt_no">No, thank you</label> </div> <button type="submit">Update</button></form>Controlling Input State
Section titled “Controlling Input State”readonly (Boolean Attribute):
Specifies that the input field’s value cannot be changed by the user. However, the value is still submitted with the form. The element can receive focus.
<label for="ref_id">Reference ID:</label><input type="text" id="ref_id" name="reference" value="XYZ123" readonly>disabled (Boolean Attribute):
Specifies that the input field is disabled. A disabled element is unusable, un-clickable, cannot receive focus, and its value is not submitted with the form.
<label for="discount_code">Discount Code:</label><input type="text" id="discount_code" name="discount" value="APPLIED" disabled>checked (Boolean Attribute):
Specifies that a checkbox or radio button should be checked by default when the page loads.
<input type="checkbox" id="agree" name="terms" value="agreed" checked><label for="agree">I agree to the terms</label>Note on Boolean Attributes: For attributes like readonly, disabled, checked, required, multiple, novalidate, their presence implies true. You don’t need to set a value (e.g., readonly is sufficient, readonly="readonly" or readonly="" are equivalent but unnecessary).
Input Presentation and Hints
Section titled “Input Presentation and Hints”placeholder: Provides a short hint (e.g., a sample value or description) displayed in the input field before the user enters a value. Works with text, search, url, tel, email, password, and number types.
Important: The placeholder is not a substitute for a <label>. Labels are essential for accessibility.
<label for="search">Search:</label><input type="search" id="search" name="q" placeholder="e.g., HTML forms">size: Specifies the visible width of the input field, in average character widths. Primarily affects text, search, url, tel, email, password. Note: CSS (width property) is the preferred method for controlling input field size for better layout control.
<label for="zip">Zip Code:</label><input type="text" id="zip" name="zipcode" size="5"> <!-- Suggests width for 5 chars -->list: Specifies the id of a <datalist> element that contains predefined options for an input field. Provides autocomplete suggestions.
<label for="browser_choice">Choose browser:</label><input list="browsers" id="browser_choice" name="browser"><datalist id="browsers"> <option value="Chrome"> <option value="Firefox"> <option value="Safari"> <option value="Edge"> <option value="Opera"></datalist>Input Validation Attributes (HTML5)
Section titled “Input Validation Attributes (HTML5)”required (Boolean Attribute):
Specifies that the input field must be filled out before the form can be submitted. Works with most input types that accept text or require selection/files.
<label for="req_email">Email (required):</label><input type="email" id="req_email" name="email" required>minlength / maxlength: Specifies the minimum and maximum number of characters allowed in a text-based input (text, email, password, search, tel, url).
<label for="pwd">Password (8-16 chars):</label><input type="password" id="pwd" name="password" minlength="8" maxlength="16" required>min / max: Specifies the minimum and maximum acceptable values for numeric or date/time input types (number, range, date, month, week, time, datetime-local).
<label for="quantity">Quantity (1-10):</label><input type="number" id="quantity" name="qty" min="1" max="10" value="1">step: Specifies the legal number intervals for numeric or date/time input types. For number, step="3" allows values like -3, 0, 3, 6. For range, it affects the slider granularity.
<label for="rating">Rating:</label><input type="range" id="rating" name="rating" min="0" max="10" step="2" value="6">pattern: Specifies a regular expression that the input value must match. Works with text, search, url, tel, email, password. Use the title attribute to explain the required format to the user.
<label for="postcode">Postcode (UK):</label><input type="text" id="postcode" name="postcode" pattern="[A-Za-z]{1,2}[0-9Rr][0-9A-Za-z]? [0-9][A-Za-z]{2}" title="Enter a valid UK postcode (e.g., SW1A 0AA)" required>Reminder: HTML5 validation is helpful but not foolproof. Always validate data on the server-side.
Attributes for Specific Input Types
Section titled “Attributes for Specific Input Types”multiple (Boolean Attribute):
Allows the user to select or enter multiple values. Works with email (comma-separated list) and file (select multiple files). Also used on <select> elements.
<label for="attachments">Attach files:</label><input type="file" id="attachments" name="files" multiple>accept: Specifies the types of files the user can select when type="file". Values are comma-separated MIME types or file extensions.
<label for="avatar">Upload Image:</label><input type="file" id="avatar" name="avatar" accept="image/png, image/jpeg">height / width: Specify the dimensions for <input type="image">. Note: <input type="image"> acts as a graphical submit button but is less common now. Using <button type="submit"> with CSS background images or nested <img> tags is more flexible. CSS is preferred for sizing.
<!-- Less common usage --><input type="image" src="submit-icon.png" alt="Submit Form" width="32" height="32">alt: Required for <input type="image">, provides alternative text for accessibility, similar to <img>.
Form Association and Submission Attributes (HTML5)
Section titled “Form Association and Submission Attributes (HTML5)”These attributes allow input elements (especially submit buttons) to override default <form> behavior or associate elements with forms even if they are outside the <form> tags.
form: Specifies the id of the <form> the input element belongs to. Useful if the input is placed outside its associated form in the HTML structure.
<form action="/process" method="post" id="mainForm"> <!-- ... some fields ... --></form>
<!-- This field is outside the form tag but associated via the 'form' attribute --><label for="extra_field">Extra Info:</label><input type="text" id="extra_field" name="extra" form="mainForm">
<button type="submit" form="mainForm">Submit Main Form</button>formaction: Overrides the <form> element’s action attribute. Used on submit buttons (type="submit" or type="image") to send the form data to a different URL.
formenctype: Overrides the <form> element’s enctype attribute. Used on submit buttons.
formmethod: Overrides the <form> element’s method attribute (GET or POST). Used on submit buttons.
formnovalidate (Boolean Attribute): Overrides the <form> element’s novalidate attribute. If present on a submit button, the form is submitted without browser validation, even if the main form doesn’t have novalidate set.
formtarget: Overrides the <form> element’s target attribute. Used on submit buttons to specify where the response should be displayed (e.g., _blank).
Example using override attributes:
<form action="/standard-submit" method="post" id="complexForm"> <label for="data">Data:</label> <input type="text" id="data" name="data">
<button type="submit">Standard Submit (POST to /standard-submit)</button>
<button type="submit" formaction="/admin-submit" formmethod="post">Submit as Admin (POST to /admin-submit)</button>
<button type="submit" formaction="/view-data" formmethod="get" formtarget="_blank" formnovalidate> View Data (GET to /view-data, new tab, no validation) </button></form>Other HTML5 Attributes
Section titled “Other HTML5 Attributes”autocomplete: Controls browser autocompletion (on or off). Can be set on the <form> or individual inputs. Useful for turning off autocomplete for sensitive fields.
<input type="text" name="credit_card" autocomplete="off">autofocus (Boolean Attribute): Specifies that an input element should automatically receive focus when the page loads. Use sparingly, as it can be disorienting.
<label for="first_field">First Field:</label><input type="text" id="first_field" name="first" autofocus>