Forms are one of the most important parts of any website. They allow users to enter information – like signing up for an account, logging in, submitting feedback, or making a purchase. In this tutorial, we will learn how to create basic forms using HTML.
Basic Form Structure
A form in HTML is created using the <form>
element. It acts like a container that holds all the input fields, labels, buttons, and other elements where a user can enter data. The form also needs instructions on where to send the data and how to send it. This is managed by attributes like action
and method
.
Example:
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Submit</button>
</form>
Explanation:
action
-> URL where the form data will be sent when submitted.method
-> HTTP method used to send data:get
(data visible in URL) orpost
(data hidden in body).<label>
-> Improves accessibility by linking text to an input.for
->for
attribute links the<label>
to a specific<input>
element. The value offor
should match theid
of the associated input.<input>
-> a field where the user can enter data.type
-> Specifies the type of input. Default istext
.id
-> Unique identifier for the form. Often used with<label for="id">
.name
-> Name of the input field. Used as the key when form data is submitted.<button>
-> used to submit forms or trigger scripts.
Common Input Types
The <input>
element supports many different types that change how users interact with a field. Instead of always using plain text fields, you can choose input types that match the kind of data you expect. For example, email
ensures that only a valid email address can be entered, while date
provides a date picker. This improves both usability and data validation.
Example:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<label for="age">Age:</label>
<input type="number" id="age" name="age">
<label for="dob">Date of Birth:</label>
<input type="date" id="dob" name="dob">
<label for="color">Favorite Color:</label>
<input type="color" id="color" name="color">
</form>
Radio Buttons and Checkboxes
Sometimes you want users to select from predefined options instead of typing text. That’s where radio buttons and checkboxes come in:
- Radio buttons allow users to select only one option from a list.
- Checkboxes allow users to select multiple options at the same time.
They are useful in forms such as surveys, sign-ups, or preference settings.
Example:
<form>
<p>Choose your gender:</p>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
<p>Select your hobbies:</p>
<input type="checkbox" id="reading" name="hobby" value="reading">
<label for="reading">Reading</label>
<input type="checkbox" id="sports" name="hobby" value="sports">
<label for="sports">Sports</label>
</form>
Dropdown Menus (Select Element)
When you need users to select one option from a long list, a dropdown menu is a cleaner and more space-saving option. The <select>
element creates the dropdown, and each choice inside it is defined with an <option>
.
Example:
<form>
<label for="city">Choose your city:</label>
<select id="city" name="city">
<option value="mumbai">Mumbai</option>
<option value="pune">Pune</option>
<option value="delhi">Delhi</option>
</select>
</form>
Buttons in Forms
Buttons are used to trigger actions in a form. The most common type is the submit button, which sends data to the server. Other types include reset, which clears all fields, and generic button elements, often used with JavaScript to perform custom tasks.
Example:
<form>
<input type="submit" value="Submit Form">
<input type="reset" value="Reset Form">
<button type="button">Click Me</button>
</form>
Best Practices for HTML Forms
Well-structured forms are not just about design, they are also about usability and accessibility. Adding labels ensures that screen readers can identify form fields. Using the correct input type improves mobile and desktop user experience. Grouping related fields helps keep forms organized. And adding built-in validation prevents bad data from being submitted.
Example:
<form>
<fieldset>
<legend>Login Form</legend>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="pwd">Password:</label>
<input type="password" id="pwd" name="pwd" required minlength="6">
<button type="submit">Login</button>
</fieldset>
</form>
<fieldset>
-> used to group related form controls together.<legend>
-> used inside a<fieldset>
to provide a caption or title for the grouped elements.
Styling Tip:
You can style the <fieldset>
and <legend>
using CSS:
/* Example CSS */
fieldset {
border: 2px solid #007BFF;
padding: 15px;
border-radius: 5px;
}
legend {
font-weight: bold;
color: #007BFF;
}
Summary
Element / Attribute | Description |
---|---|
<form> | Creates a form |
action | URL where data is sent |
method | Defines method (get or post ) |
<label> | Associates text with an input |
<input> | Creates a field (text, email, password, etc.) |
<select> | Creates a dropdown list |
<option> | Defines an option in dropdown |
<textarea> | Multi-line text input |
<button> | Creates a button |
<fieldset> | Groups related inputs |
<legend> | Title for fieldset group |
With this knowledge, you can now build user-friendly forms in HTML.
Practice Challenge
Create a student registration form with the following:
- Name, Email, Age (text/number inputs)
- Gender (radio buttons)
- Hobbies (checkboxes)
- City (dropdown select)
- Submit button
Add labels for each input, use required
attributes, and style the form neatly with CSS.