Creating a Form

To create a form in HTML, use the form element and include various input types to collect data from the user.

Forms are used to gather information from visitors to your website. These can include contact forms, search forms, or login forms. In HTML, a form acts as a container for all the elements that the user interacts with before submitting the data to a server.

The Basic Structure

A standard form requires three main parts: the form tag, at least one input for data capture, and a submit button. You also need to define where the data goes using the action attribute:

Key Attributes

Labels and Accessibility

While it's not required for the form to work, it's good practice to use the label element for each input field. By using the for attribute on the label and a matching id on the input, you link them together. This makes the form easier to use for everyone, including those using screen readers.

Full Working Example

Here is a basic form that collects a name and an email address:

View Output Full Screen Preview

Tip: The name Attribute

While the id is used for styling and link labels, the name attribute is what the server uses to identify the data. If you forget to include a name on an input, its value will not be sent when the form is submitted.