Form Basics
form, input, label, submit
Skills taught: html.forms, html.input, html.label, html.submit
Every form starts with the **form** element, which wraps all inputs and defines where and how to send data.
<form action="/submit" method="POST">
<!-- controls go here -->
</form>
The **input** element creates a field. The **type** attribute controls what kind of input it is:
- **type="text"** — single-line text
- **type="email"** — email address (validated by browser)
- **type="password"** — masked text
- **type="number"** — numeric input with arrows
- **type="tel"** — telephone number
- **type="url"** — web address
- **type="search"** — search field
Every input needs a **label**. Link them with matching **for** and **id** attributes — this makes the label clickable and tells screen readers what the field is for.
<label for="username">Username</label>
<input type="text" id="username" name="username" placeholder="e.g. prashantkeshr">
Warning:
<form action="/login" method="POST">
<div>
<label for="email">Email address</label>
<input type="email" id="email" name="email" autocomplete="email" required>
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" name="password" autocomplete="current-password" required>
</div>
<button type="submit">Log in</button>
</form>
Tip:
Frequently Asked Questions
What is the difference between GET and POST?
GET appends data to the URL — good for search forms (shareable results). POST sends data in the body — required for passwords, file uploads, or any data that modifies server state.
Do I need action and method on every form?
action defaults to the current URL. method defaults to GET. For most forms you should set both explicitly to be clear about intent.
Can I style form elements with CSS?
Yes, but form elements are notoriously tricky to style consistently across browsers. CSS resets like appearance: none give you a blank slate to restyle inputs and selects.