HTML
Forms Overview
Build interactive contact forms and understand how users send data to servers
Think of forms like order sheets at a restaurant. The waiter hands you a paper with blank spaces for your name, table number, and meal choice. You fill it out and hand it back. HTML forms work the same way — they collect information from visitors and send it somewhere for processing. Alex wants to add a contact form to their portfolio so potential clients can reach out. But before diving into specific input types, we need to understand what forms are and how they work behind the scenes.What Forms Do
A form is a container that holds different types of input fields. When someone fills out these fields and clicks a submit button, the browser packages all that data and sends it to a web server for processing. Common examples you see every day: - Google's search box (you type, click search, get results) - Facebook login (username, password, submit) - Amazon checkout (address, payment info, place order) - Newsletter signups (email address, subscribe button) The<form> tag wraps around all the input fields, just like a folder holds related papers together.
Basic Form Structure
Every form needs these essential parts: a form container, input fields for data collection, labels to describe each field, and a submit button to send the data.<!-- Contact form for Alex's portfolio -->
<form action="mailto:alex@example.com" method="post">
<!-- Name input field -->
<label for="name">Your Name:</label>
<input type="text" id="name" name="name">
<!-- Email input field -->
<label for="email">Your Email:</label>
<input type="email" id="email" name="email">
<!-- Message textarea -->
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea>
<!-- Submit button -->
<button type="submit">Send Message</button>
</form>What just happened?
The browser created a contact form with three input fields and a submit button. Each label connects to its input field using matching for and id attributes. Try typing in the fields — they accept different types of data based on their input type.
Form Attributes Explained
The<form> tag uses several important attributes that control how data gets processed and where it goes.
Action Attribute
Theaction attribute is like the mailing address on an envelope — it tells the browser where to deliver the form data when someone hits submit.
Common action values:
- action="contact.php" — sends to a server script
- action="mailto:you@email.com" — opens email client
- action="#" — submits to same page (placeholder)
- No action — defaults to current page
Method Attribute
Themethod attribute determines how the browser packages and sends the form data. Think of it like choosing regular mail versus overnight delivery — different methods for different needs.
GET Method
Adds form data to the URL. Perfect for search forms where you want bookmarkable results. Data shows in address bar.
POST Method
Hides form data inside the request. Better for contact forms, passwords, or large amounts of text. More secure and private.
Labels and Accessibility
Labels are like name tags for form fields — they tell users what information to enter in each input. But they do more than just display text. Thefor attribute on a label must match the id attribute on its input field. This creates an invisible connection that helps screen readers and makes the form clickable.
<!-- Properly connected label and input -->
<form>
<!-- Label connects to input via matching for/id -->
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<!-- Alternative: wrap input inside label -->
<label>
Password:
<input type="password" name="password">
</label>
<button type="submit">Login</button>
</form>Pro tip: Click on the "Username:" label text in the example above. Notice how it automatically focuses the input field? That's the power of proper label-input connections. Screen readers also announce the label when users navigate to the field.
How Form Submission Works
When someone clicks a submit button, several things happen behind the scenes. Understanding this process helps you build better forms and debug problems.User fills out form fields and clicks submit button
Browser collects all form data using field names as keys
Browser sends data to the URL specified in action attribute
Server processes data and sends back a response or confirmation
name attribute on each input field becomes the key that identifies the data. If you have <input name="email"> and someone types "alex@example.com", the server receives email=alex@example.com.
Common Form Patterns
Real websites use predictable form patterns that users recognize. Following these conventions makes your forms more intuitive and trustworthy.Contact Forms
Name, email, subject, message, submit button. Keep it simple and ask only for essential information.
Newsletter Signup
Just email address and submit. Sometimes first name. The simpler, the better conversion rates.
Login Forms
Username/email, password, remember me checkbox, login button, forgot password link.
Registration Forms
Username, email, password, confirm password, terms checkbox, create account button.
Form Styling Basics
HTML forms work perfectly fine without any styling, but they look pretty bland. A few simple CSS rules can make your forms look professional and inviting.<!-- Styled contact form for Alex's portfolio -->
<style>
/* Form container styling */
.contact-form { max-width: 500px; margin: 0 auto; padding: 20px; }
.form-group { margin-bottom: 20px; }
/* Label styling */
.contact-form label { display: block; margin-bottom: 5px; font-weight: bold; }
/* Input and textarea styling */
.contact-form input, .contact-form textarea {
width: 100%; padding: 10px; border: 2px solid #e2e8f0;
border-radius: 6px; font-size: 16px;
}
/* Submit button styling */
.submit-btn {
background: #c2410c; color: white; padding: 12px 24px;
border: none; border-radius: 6px; cursor: pointer; font-weight: bold;
}
</style>
<form class="contact-form" action="#" method="post">
<div class="form-group">
<label for="client-name">Your Name</label>
<input type="text" id="client-name" name="name" required>
</div>
<div class="form-group">
<label for="client-email">Email Address</label>
<input type="email" id="client-email" name="email" required>
</div>
<div class="form-group">
<label for="project-details">Project Details</label>
<textarea id="project-details" name="message" rows="5" required></textarea>
</div>
<button type="submit" class="submit-btn">Send Message</button>
</form>What just happened?
The CSS transformed a basic form into a polished contact form. Notice the consistent spacing, proper focus states (click in a field to see the orange border), and hover effects on the button. The required attribute makes fields mandatory — try submitting without filling them out.
Watch out for mobile
Always include font-size: 16px on mobile form inputs. iOS Safari automatically zooms in on inputs with smaller font sizes, which can break your layout. This simple rule prevents that annoying zoom behavior.
Real-World Form Examples
Major websites keep their forms simple and focused. Google's search form has one input and one button. Twitter's tweet composer is basically a large text area with a character counter. GitHub's login form follows the standard username-password pattern. The secret is asking for only essential information upfront. You can always collect more details later through profile pages or progressive disclosure. Every additional field reduces the chance someone will complete your form. Alex's portfolio contact form should focus on three core pieces of information: who they are (name), how to reach them back (email), and what they need (message). That's it. No phone numbers, company names, or project budgets required — those conversations happen after the initial contact.Quiz
What does the action attribute do in a form tag?
Why should a label's "for" attribute match an input's "id" attribute?
What's the main difference between GET and POST methods for Alex's contact form?
Up Next: Input Types
Explore different input types like email, password, number, and date to collect specific data formats with built-in validation.