HTML / HTML Tags

HTML <Form> Tag

Dec 12, 2022
3 min read
HTML form Tag

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

Attributes

The Form element supports the Global Attributes, as well as the following:

Best Practices

  • You can use the :valid and :invalid CSS 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

ChromeEdgeFirefoxIEOperaSafari
YesYesYesYesYesYes

Mobile

Android WebviewChrome AndroidFirefox AndroidOpera AndroidiOS SafariSamsung Internet
YesYesYesYesYesYes