HTML Form Elements
π What Are HTML Form Elements?
HTML form elements are the building blocks of interactive forms on web pages. They allow users to input and submit data such as text, selections, files, and more. All these elements are placed inside the <form> tag. π§°
>>βForm elements give users a voice on your site β use them wisely.β π€
π§± Common HTML Form Elements
Here's a breakdown of the most frequently used form elements:
| Element | Description |
|---|---|
| <input> | Accepts various user inputs like text, number, date, etc. |
| <textarea> | Allows multi-line text input. |
| <label> | Defines a label for an input field. |
| <select> | Creates a drop-down menu. |
| <option> | Defines options in a drop-down menu. |
| <button> | Represents a clickable button. |
| <fieldset> | Groups related form elements. |
| <legend> | Defines a caption for a <fieldset>. |
βοΈ Input Element Variants
The <input> element has many type attributes for different data:
- text β single-line text input
- password β hides input text
- email β validates an email address
- number β numeric input
- checkbox β multiple choice selection
- radio β single choice in a group
- file β upload a file
- submit β submit the form
π Example Form
HTML Form Example
<form action="/submit" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required />
<label for="bio">Bio:</label>
<textarea id="bio" name="bio"></textarea>
<label for="gender">Gender:</label>
<select id="gender" name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<label>
<input type="checkbox" name="subscribe" /> Subscribe to newsletter
</label>
<button type="submit">Submit</button>
</form>π‘ Accessibility Tips
- Always pair <label> with inputs using the for and id attributes.
- Use fieldset and legend to group related inputs (especially radio buttons).
Note
β οΈ Always include the name attribute on inputs β it's essential for submitting data!
π Resources
>>βYour form elements should guide users β not confuse them.β π§