Definition
The <form> tag is used to create an HTML form that contains controls for submitting user information.
Example
<style>
.fieldset input:invalid {
background-color: red;
}
.fieldset input:valid {
background-color: green;
</style>
<form action="" method="get" class="form">
<fieldset class="fieldset">
<label for="name">Name: </label>
<input type="text" name="name" id="name" required>
</fieldset>
<div class="form-example">
<input type="submit" value="Subscribe!">
</div>
</form>Usage
- The
<form>element can contain one or more of the following form elements: - The opening and closing tags are required.
- You can not put a
<form>element as a child of a<form>element.
Attributes
The Form element supports the Global Attributes, as well as the following:
accept-charsetThe
accept-charsetattribute specifies the character encoding to be used for the form submission.actionThe
actionattribute specifies where to send the form data on submission.These values can be overriden by the
formactionattributes on some elements, including<button>,<input type="submit">, or<input type="image">.autocompleteThe
autocompleteattribute indicates whether the browser can automatically populate input elements. These settings will be ignored on any form elements ifautocompleteis set on those elements directly.Values can include:
offon
Note: - Some browsers may ignore the
offsetting for login forms.enctypeThe
enctypeattribute specifies how to encode the form on submission formethod="post"HTTP methods.Values can include:
application/x-www-form-urlencodedmultipart/form-datatext/plain
These values can be overriden by the
formenctypeattributes on some elements, including<button>,<input type="submit">, or<input type="image">.methodThe
methodattribute specifies the HTTP method to use for form submission.Values can include:
getpostdialog
These values can be overriden by the
formmethodattributes on some elements, including<button>,<input type="submit">, or<input type="image">.nameThe
nameattribute specifies the name of the form. It must be unique and not shared with any of the form elements.novalidateThe
novalidateattribute is a boolean that indicates whether the form should not be validated on submission. If the attribute is omitted, then validation is undertaken.It can be overriden by using the
formnovalidateattribute on some individual elements within the form, including<button>,<input type="submit">, or<input type="image">.relThe
relattribute specifies the relationship between the two documents.Values can include:
- alternate
- author
- bookmark
- external
- help
- license
- next
- nofollow
- noreferrer
- noopener
- prev
- search
- tag
- sponsored
- ugc
targetThe
targetattribute specifies how to open the linked document.Values can include:
- _blank
- _parent
- _self
- _top
Best Practices
- You can use the
:validand:invalidCSS pseudo-classes to style a<form>element based their validation state. For example:input:invalid { background-color: red; } input:valid { background-color: green; }
Specification
Browser Support
Desktop
| Chrome | Edge | Firefox | IE | Opera | Safari |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Mobile
| Android Webview | Chrome Android | Firefox Android | Opera Android | iOS Safari | Samsung Internet |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
