Live Preview
A simple contact form with name, email, and submit button:

By the end of this tutorial, you’ll build interactive forms that collect and submit user input with semantic HTML.
The HTML <form> tag is a cornerstone element in web development, empowering developers to create interactive and user-friendly web forms. This guide covers syntax, attributes, accessibility, and best practices for beginners and web developers alike.
Wrap controls in <form> opening and closing tags.
Send data to a URL with GET or POST.
Collect names, emails, and usernames.
Capture multi-line messages and comments.
Trigger form submission to the server.
Accessible, submittable form controls.
<form> Tag?The form element (<form>) encapsulates interactive controls for collecting user input. It provides the structure for input fields, textareas, selects, buttons, and other elements, and defines where and how data is sent when the user submits the form.
Only form controls with a name attribute are included in submission data. Always pair inputs with label and name for accessibility and server handling.
Forms facilitate communication between users and web servers. You can also handle submission with JavaScript using the submit event, but the semantic form element remains the foundation.
Encapsulate form elements within opening and closing <form> tags:
<form action="/submit" method="post">
<!-- Your form elements go here -->
</form>form element.action to the URL that processes the data.method="post" for sensitive or large submissions; get for searches.button type="submit" or input type="submit").| Pattern | Code Snippet | Notes |
|---|---|---|
| Basic form | <form action="/submit" method="post"> | Standard submission |
| Text input | <input type="text" name="username"> | Single-line text |
| Textarea | <textarea name="message"></textarea> | Multi-line text |
| Submit | <button type="submit">Submit</button> | Sends the form |
| Label pairing | <label for="id">...</label> | Required for a11y |
| Default method | get | Use post for sensitive data |
<form> vs <div>Both can wrap inputs visually, but only form handles submission semantics:
| Feature | <form> | <div> |
|---|---|---|
| Semantic role | Form submission container | Generic container |
| Submit behavior | Enter key and submit button work natively | Requires manual JavaScript |
| action / method | Built-in attributes | Not available |
| Best for | Login, contact, search, checkout | Layout-only groupings |
method="get" vs method="post"Choose the HTTP method based on what the form does:
| Method | Data location | When to use |
|---|---|---|
get | Appended to URL as query string | Search boxes, filters, bookmarkable queries |
post | Sent in request body | Login, registration, contact forms, file uploads |
| Security note | Never send passwords with GET — use POST over HTTPS | |
| Default | get | Explicitly set method="post" when needed |
The <form> tag supports these key attributes plus global attributes such as class and id:
action Required*URL where form data is sent on submit. Use a server endpoint or handler script.
action="/submit-form"method HTTPHTTP verb: get or post. Defaults to get.
method="post"enctype UploadsEncoding type. Use multipart/form-data when uploading files.
enctype="multipart/form-data"name / id GlobalIdentify the form for scripting or associating external controls via form attribute.
id="signup-form"autocomplete UXEnable or disable browser autofill for the entire form.
autocomplete="on"novalidate BooleanSkip built-in browser validation (you may validate with JavaScript instead).
novalidate<form action="/submit-form" method="post" id="contact-form">
<!-- Your form elements go here -->
</form>Avoid target="_blank" on forms unless necessary. If used, add rel="noopener" on any resulting navigation for security.
Contact forms, text inputs, textareas, and submit buttons with copy-ready code and live previews.
A simple contact form with name, email, and submit button:
A complete form that sends name and email to a server endpoint using POST.
<form action="/submit-form" 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>Use <form> for login and registration, contact and feedback forms, search bars, newsletter signups, checkout flows, survey questions, and any page where users enter data that should be sent to a server or processed by JavaScript.
Use the <input> element with type="text" to create a single-line text field inside a form.
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</form>For multi-line text input, use the <textarea> element inside a form.
<form>
<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="50"></textarea>
</form>Include a submit button so users can send the form. The button must have type="submit" and live inside the form.
<form action="/submit-form" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<button type="submit">Submit</button>
</form>Style the form container and its controls for a polished user experience:
max-width Readable form widthlabel Clear field labelsinput:focus Focus ring on fieldsbutton Primary submit style/* Form container */
form.contact-form {
max-width: 420px;
padding: 1.25rem;
border: 1px solid #cbd5e1;
border-radius: 8px;
background: #fff;
}
form label {
display: block;
margin: 0.65rem 0 0.25rem;
font-size: 0.8125rem;
font-weight: 500;
}
form input,
form textarea {
width: 100%;
padding: 0.45rem 0.6rem;
border: 1px solid #cbd5e1;
border-radius: 6px;
}
form button[type="submit"] {
margin-top: 0.75rem;
padding: 0.5rem 1rem;
background: #2563eb;
color: #fff;
border: none;
border-radius: 6px;
cursor: pointer;
}Live styled form
Accessible forms work for keyboard and screen reader users:
label for="id" matching the control’s id.name attribute identifies data on submit.fieldset and legend for sections.Add labeled inputs, textareas, and a submit button inside form.
Browser collects values from named controls as the user types.
Submit sends data to action using method (GET or POST).
The backend processes input and responds with a new page or JSON.
The <form> element is fully supported in all browsers, including legacy Internet Explorer.
From legacy Internet Explorer to the latest mobile browsers — the form element is a foundational building block for interactive web pages.
Bottom line: Ship interactive forms with confidence. The <form> element is safe to use in every production environment today.
Mastering the HTML <form> tag is fundamental for creating dynamic and user-friendly web forms. By understanding its syntax, attributes, and best practices, you’ll build effective forms that enhance user interaction on your website.
Always use labels and name attributes, choose GET or POST appropriately, validate on the server, and pair forms with related tags like fieldset, button, and label.
label elements for every fieldname on all submittable controlsmethod="post" for sensitive datamethod="get"form<form>Bookmark these before you ship — they’ll keep your forms usable and secure.
<form> wraps all form controls.
Where submitted data is sent.
AttributeChoose method for search vs sensitive data.
HTTPOnly named fields are submitted.
SubmissionPair every input with a label.
AccessibilityWorks in every browser, including legacy IE.
Compatibilityname attribute are included when the form is submitted.Create contact forms with labels, inputs, and submit buttons in the HTML editor.
6 people found this page helpful