- HTML Form Code
- Example Form with No Styles
- Same Form Styled with Top-Aligned Labels
- Left-Aligned Labels
- Right-Aligned Labels
- Inline Form
- Accessible Compact Form
- Bootstrap Forms
- Responsive Grid System
- Validation Styles & Feedback Icons
- Form-Associated Elements
- The Types
- Related
- HTML Forms
- Example
- The Element
- The Element
- Text Fields
- Example
- The Element
- Radio Buttons
- Example
- Checkboxes
- Example
- The Submit Button
- Example
- Example
- : The Form element
- Try it
- Attributes
- Attributes for form submission
- Examples
- Result
- Technical summary
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- HTML Tag
- Definition and Usage
- Browser Support
- Attributes
HTML Form Code
You can use the following HTML code to create a form within your HTML document.
Example Form with No Styles
This example uses raw HTML — no CSS has been applied.
Same Form Styled with Top-Aligned Labels
This is the same form, but this one has CSS applied to ensure the labels appear above the input fields. Some other light styling has also been appled.
Left-Aligned Labels
This form has left-aligned labels.
Right-Aligned Labels
This enquiry form has right-aligned labels.
Inline Form
This form has its elements displayed inline.
Accessible Compact Form
Therefore, we use a little CSS trick to «hide» the label from visual browsers, while still presenting them to screen readers.
Bootstrap Forms
Bootstrap includes a bunch of components for dealing with forms. Here are two.
Responsive Grid System
You can combine Bootstrap forms with Bootstrap grids to allow you more control over the layout of your forms. This is particularly useful for developing responsive forms.
Validation Styles & Feedback Icons
You can also take advantage of Bootstrap’s validation styles and feedback icons so that you can provide visual feedback to the user based on the data they enter.
Form-Associated Elements
Here are the form-associated elements.
The Types
Here’s a complete list of the input types available in HTML5.
Value | Data Type | Control Type |
---|---|---|
hidden | An arbitrary string | N/A |
text | Text with no line breaks | Text field |
search | Text with no line breaks | Text field |
tel | Text with no line breaks | Text field |
url | An absolute URL | Text field |
An email address or list of email addresses | Text field | |
password | Text with no line breaks (sensitive information) | Text field that obscures data entry (eg, hides the password by using asterisks (******) or similar) |
datetime | A date and time (year, month, day, hour, minute, second, fraction of a second) with the time zone set to UTC | Date and time control |
date | A date (year, month, day) with no time zone | Date control |
month | A date consisting of a year and a month with no time zone | A month control |
week | A date consisting of a week-year number and a week number with no time zone | A week control |
time | A time (hour, minute, seconds, fractional seconds) with no time zone | A time control |
datetime-local | A date and time (year, month, day, hour, minute, second, fraction of a second) with no time zone | Date and time control |
number | A numerical value | Text field or spinner control |
range | A numerical value, with the extra semantic that the exact value is not important | Slider control or similar |
color | An sRGB color with 8-bit red, green, and blue components | A color well. Enables the user to select a color. |
checkbox | A set of zero or more values from a predefined list | Checkbox |
radio | An enumerated value | Radio button |
file | Zero or more files each with a MIME type and optionally a file name | A label and a button |
submit | An enumerated value, with the extra semantic that it must be the last value selected and initiates form submission | Button |
image | A coordinate, relative to a particular image’s size, with the extra semantic that it must be the last value selected and initiates form submission | Either a clickable image, or a button |
reset | N/A | Button |
button | N/A | Button |
Related
HTML Forms
An HTML form is used to collect user input. The user input is most often sent to a server for processing.
Example
The Element
The HTML element is used to create an HTML form for user input:
The element is a container for different types of input elements, such as: text fields, checkboxes, radio buttons, submit buttons, etc.
All the different form elements are covered in this chapter: HTML Form Elements.
The Element
The HTML element is the most used form element.
An element can be displayed in many ways, depending on the type attribute.
Type | Description |
---|---|
Displays a single-line text input field | |
Displays a radio button (for selecting one of many choices) | |
Displays a checkbox (for selecting zero or more of many choices) | |
Displays a submit button (for submitting the form) | |
Displays a clickable button |
All the different input types are covered in this chapter: HTML Input Types.
Text Fields
The defines a single-line input field for text input.
Example
A form with input fields for text:
This is how the HTML code above will be displayed in a browser:
Note: The form itself is not visible. Also note that the default width of an input field is 20 characters.
The Element
Notice the use of the element in the example above.
The tag defines a label for many form elements.
The element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focuses on the input element.
The element also helps users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) — because when the user clicks the text within the element, it toggles the radio button/checkbox.
The for attribute of the tag should be equal to the id attribute of the element to bind them together.
Radio Buttons
The defines a radio button.
Radio buttons let a user select ONE of a limited number of choices.
Example
A form with radio buttons:
Choose your favorite Web language:
This is how the HTML code above will be displayed in a browser:
Choose your favorite Web language:
Checkboxes
The defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example
This is how the HTML code above will be displayed in a browser:
I have a bike
I have a car
I have a boat
The Submit Button
The defines a button for submitting the form data to a form-handler.
The form-handler is typically a file on the server with a script for processing input data.
The form-handler is specified in the form’s action attribute.
Example
A form with a submit button:
This is how the HTML code above will be displayed in a browser:
The Name Attribute for
Notice that each input field must have a name attribute to be submitted.
If the name attribute is omitted, the value of the input field will not be sent at all.
Example
This example will not submit the value of the «First name» input field:
: The Form element
The HTML element represents a document section containing interactive controls for submitting information.
Try it
It is possible to use the :valid and :invalid CSS pseudo-classes to style a element based on whether the elements inside the form are valid.
Attributes
This element includes the global attributes.
Comma-separated content types the server accepts.
Note: This attribute has been deprecated and should not be used. Instead, use the accept attribute on elements.
Space-separated character encodings the server accepts. The browser uses them in the order in which they are listed. The default value means the same encoding as the page. (In previous versions of HTML, character encodings could also be delimited by commas.)
A nonstandard attribute used by iOS Safari that controls how textual form elements should be automatically capitalized. autocapitalize attributes on a form elements override it on . Possible values:
- none : No automatic capitalization.
- sentences (default): Capitalize the first letter of each sentence.
- words : Capitalize the first letter of each word.
- characters : Capitalize all characters — that is, uppercase.
Indicates whether input elements can by default have their values automatically completed by the browser. autocomplete attributes on form elements override it on . Possible values:
- off : The browser may not automatically complete entries. (Browsers tend to ignore this for suspected login forms; see The autocomplete attribute and login fields.)
- on : The browser may automatically complete entries.
The name of the form. The value must not be the empty string, and must be unique among the form elements in the forms collection that it is in, if any.
Controls the annotations and what kinds of links the form creates. Annotations include external , nofollow , opener , noopener , and noreferrer . Link types include help , prev , next , search , and license . The rel value is a space-separated list of these enumerated values.
Attributes for form submission
The following attributes control behavior during form submission.
The URL that processes the form submission. This value can be overridden by a formaction attribute on a , , or element. This attribute is ignored when method=»dialog» is set.
If the value of the method attribute is post , enctype is the MIME type of the form submission. Possible values:
- application/x-www-form-urlencoded : The default value.
- multipart/form-data : Use this if the form contains elements with type=file .
- text/plain : Useful for debugging purposes.
This value can be overridden by formenctype attributes on , , or elements.
The HTTP method to submit the form with. The only allowed methods/values are (case insensitive):
- post : The POST method; form data sent as the request body.
- get (default): The GET ; form data appended to the action URL with a ? separator. Use this method when the form has no side effects.
- dialog : When the form is inside a , closes the dialog and causes a submit event to be fired on submission, without submitting data or clearing the form.
This value is overridden by formmethod attributes on , , or elements.
This Boolean attribute indicates that the form shouldn’t be validated when submitted. If this attribute is not set (and therefore the form is validated), it can be overridden by a formnovalidate attribute on a , , or element belonging to the form.
Indicates where to display the response after submitting the form. It is a name/keyword for a browsing context (for example, tab, window, or iframe). The following keywords have special meanings:
- _self (default): Load into the same browsing context as the current one.
- _blank : Load into a new unnamed browsing context. This provides the same behavior as setting rel=»noopener» which does not set window.opener .
- _parent : Load into the parent browsing context of the current one. If no parent, behaves the same as _self .
- _top : Load into the top-level browsing context (i.e., the browsing context that is an ancestor of the current one and has no parent). If no parent, behaves the same as _self .
This value can be overridden by a formtarget attribute on a , , or element.
Examples
form method="get"> label> Name: input name="submitted-name" autocomplete="name" /> label> button>Savebutton> form> form method="post"> label> Name: input name="submitted-name" autocomplete="name" /> label> button>Savebutton> form> form method="post"> fieldset> legend>Do you agree to the terms?legend> label>input type="radio" name="radio" value="yes" /> Yeslabel> label>input type="radio" name="radio" value="no" /> Nolabel> fieldset> form>
Result
Technical summary
Content categories | Flow content, palpable content |
---|---|
Permitted content | Flow content, but not containing elements |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts flow content |
Implicit ARIA role | form if the form has an accessible name, otherwise no corresponding role |
Permitted ARIA roles | search , none or presentation |
DOM interface | HTMLFormElement |
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jun 13, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
HTML Tag
An HTML form with two input fields and one submit button:
More «Try it Yourself» examples below.
Definition and Usage
The tag is used to create an HTML form for user input.
The element can contain one or more of the following form elements:
Browser Support
Attributes
Attribute | Value | Description |
---|---|---|
accept-charset | character_set | Specifies the character encodings that are to be used for the form submission |
action | URL | Specifies where to send the form-data when a form is submitted |
autocomplete | on off | Specifies whether a form should have autocomplete on or off |
enctype | application/x-www-form-urlencoded multipart/form-data text/plain | Specifies how the form-data should be encoded when submitting it to the server (only for method=»post») |
method | get post | Specifies the HTTP method to use when sending form-data |
name | text | Specifies the name of a form |
novalidate | novalidate | Specifies that the form should not be validated when submitted |
rel | external help license next nofollow noopener noreferrer opener prev search | Specifies the relationship between a linked resource and the current document |
target | _blank _self _parent _top | Specifies where to display the response that is received after submitting the form |