HTML <form> Tag

By Jonathan Griffin. Editor, SEO Consultant, & Developer.

· 3 min read

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