HTML <form> Tag

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

Definition and Usage

The <form> element represents a section with interactive controls for submitting information — typically <input>, <textarea>, <select>, and <button>, each paired with a <label>.

Set action to the URL that handles the data, and method to get or post (or dialog inside a <dialog>). You can style validity with :valid / :invalid on the form.

Baseline · Widely available.

Not deprecated. Core form support has been stable across browsers since about July 2015 (and much earlier in practice).

Deprecated: accept on <form>.

Do not use the form-level accept attribute. Put accept on file <input type="file"> instead (MDN).

HTML Syntax

syntax.html
<form action=" url" method="get|post">…</form>

Both start and end tags are required. Content is flow content, but not another nested <form>.

Attributes

Attributes you will use most often on <form>:

AttributeDescriptionTry it
actionURL that processes the submission (ignored when method="dialog")Try it
methodget (default), post, or dialogTry it
enctypeEncoding for post: default urlencoded, or multipart/form-data for filesTry it
novalidateBoolean — skip built-in HTML validation on submitTry it
autocompleteDefault autocomplete for controls: on or offTry it
nameForm name (non-empty, unique among forms in the collection)Try it
targetWhere to show the response: _self, _blank, etc.Try it
accept-charsetCharacter encoding for submission; use UTF-8See MDN / reference

Global Attributes

The <form> tag also supports the Global Attributes in HTML.

Live Preview

Change method, fill the fields, and submit. Submission is intercepted here so the page does not navigate.

A real <form> with labels. Submit to see a summary of what would be sent.

Form



method="get" action="/search"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic GET Form

Default method appends fields to the action URL as a query string.

HTML
<form action="/search" method="get">
  <label for="q">Search:</label>
  <input type="search" id="q" name="q">
  <button type="submit">Go</button>
</form>
Try It Yourself

How It Works

  1. Open <form> with action and method="get".
  2. Label the control with matching for / id.
  3. Give the input a name so its value is included in the query.
  4. Submit. The browser requests something like /search?q=hello.

Example 2 — POST Form

Send field values in the request body — typical for sign-up or login.

HTML
<form action="/subscribe" method="post">
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" autocomplete="email" required>
  <button type="submit">Subscribe</button>
</form>
Try It Yourself

How It Works

  1. Set method="post" so data goes in the body, not the URL.
  2. Use a fitting input type (email, password, etc.).
  3. Add autocomplete hints when they match the field.
  4. Validate on the server too — client checks are a help, not a guarantee.

Example 3 — Fieldset and Legend

Group related controls with <fieldset> and name the group with <legend>.

HTML
<form method="post">
  <fieldset>
    <legend>Do you agree to the terms?</legend>
    <label><input type="radio" name="agree" value="yes"> Yes</label>
    <label><input type="radio" name="agree" value="no"> No</label>
  </fieldset>
  <button type="submit">Continue</button>
</form>
Try It Yourself

How It Works

  1. Wrap related controls in <fieldset>.
  2. Name the group with <legend>.
  3. Share one name on radio buttons so only one can be chosen.
  4. Submit as usual with a button inside the form.

Example 4 — Labels and Required Fields

Visible labels plus required give clear, accessible validation.

HTML
<form action="/contact" method="post">
  <p>
    <label for="name">Name</label><br>
    <input type="text" id="name" name="name" required>
  </p>
  <p>
    <label for="msg">Message</label><br>
    <textarea id="msg" name="msg" rows="3" required></textarea>
  </p>
  <button type="submit">Send</button>
</form>
Try It Yourself

How It Works

  1. Label every field before asking for input.
  2. Add required where empty values are not allowed.
  3. Try submit empty — the browser blocks and shows a message.
  4. Use novalidate on the form only when you intentionally skip HTML checks.

Edge Cases & Pitfalls

Most form bugs are missing names, missing labels, or the wrong method for the job.

a11y

Labels beat placeholders

Placeholders disappear when typing and are easy to miss. Always provide a real <label>.

data

Controls need name

Without a name, a successful control is not included in the submitted data set.

structure

No nested forms

A <form> must not contain another <form>. Split forms or use the form attribute on controls.

method

GET is not for secrets

Query strings appear in history, logs, and referrers. Use POST (over HTTPS) for passwords and private data.

files

File uploads need multipart

With <input type="file">, set method="post" and enctype="multipart/form-data".

Browser Support

The <form> element is core HTML in every browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<form>

Widely available since about July 2015 (and long before). Chrome, Firefox, Safari, Edge, and Opera fully support form submission attributes and native validation.

98% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<form> Widely supported

Bottom line: Safe everywhere for classic form posts. Always validate and sanitize on the server regardless of browser features.

Key Takeaways

  • Role: <form> groups controls and defines how data is submitted.
  • Wire: action + method; every successful control needs a name.
  • Access: pair every input with a visible <label>.
  • Choose method wisely: GET for safe queries; POST for changes and private data.

One line: a good form has clear labels, named controls, and the right action / method for the job.

Default CSS Settings

Forms are block-level containers. Exact UA styles for controls vary; a simple idea:

default.css
form {
  display: block;
  margin-top: 0;
}

Keep study CSS minimal. Style spacing and labels for readability; never remove focus outlines on controls.

Frequently Asked Questions

The form element groups interactive controls (inputs, buttons, selects, and more) so users can enter data and submit it to a URL defined by action, using an HTTP method such as get or post.
GET (default) appends data to the action URL as a query string — good for safe searches with no side effects. POST sends data in the request body — better for creating or changing data, passwords, and larger payloads.
Yes for accessibility. Associate each control with a label using for/id or by wrapping the control. Placeholder text is not a substitute for a visible label.
No. Nested form elements are invalid. Keep one form per submission group, or use the form attribute on controls to associate them with a form elsewhere.
The Boolean novalidate attribute skips built-in HTML constraint validation on submit. Use it carefully — validation still matters on the server.
When the form includes a file input (type="file"). Otherwise the default application/x-www-form-urlencoded is usually fine for text fields.

Next: frame

Learn why the obsolete frame element was replaced by iframe and modern layouts.

frame tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful