Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 9

Building a Basic HTML Form

Recommended resources

Welcome to the final lesson in our module on structuring web pages. In the previous lessons, you've learned to organize content with headings, paragraphs, lists, and images. Now, we'll explore one of the most powerful features of HTML: the ability to collect information from users. This lesson introduces you to HTML forms, the primary way websites become interactive.

You'll learn how to build a simple form using the essential tags for text inputs, labels, and a submit button. This skill is the first step toward creating contact pages, login screens, search bars, and countless other interactive features that are central to modern web development.

What are Web Forms?

At its core, a web form is a section of a document containing controls that allow a user to enter and submit information to a web server. You encounter them every day, whether you're signing up for a newsletter, posting a comment, or searching for a video.

To get a formal definition, let's turn to the Mozilla Developer Network (MDN), a definitive resource for web developers.

Your first form - Learn web development | MDN

This article from MDN provides a comprehensive introduction to building your first HTML form. We will use it as our main guide for this lesson.

Start by reading the section What are web forms? to understand their purpose and the basic components involved.

As you've just read, a form is built from one or more form controls like text fields, buttons, and checkboxes. Let's learn how to create the most common ones.


The Building Blocks of a Simple Form

Creating a form involves combining a few key HTML elements. Each has a specific role, and they work together to create a functional and user-friendly experience.

  1. The <form> Element: This is the container for all your form controls. It formally defines the beginning and end of the form. Anything you want to be part of the form—input fields, labels, buttons—must be placed inside the <form> and </form> tags.

  2. The <input> Element: This is the most versatile form element. Its behavior is determined by its type attribute. For this lesson, we'll focus on type="text", which creates a single-line text input field.

  3. The <label> Element: A label provides a description for a form control. It's crucial for two reasons:

    • Usability: Users can click on the label to activate, or "focus," the corresponding input field.
    • Accessibility: Screen readers use the label to announce the purpose of an input field to visually impaired users.
  4. The Submit Button: This button, when clicked, sends the form's data to the server. You can create it using <button type="submit"> or <input type="submit">.

The following image shows how these elements combine in code to produce a simple, rendered form. Notice how each <label> corresponds to an <input> field.

This image displays the HTML code for a basic form on the left and the resulting web page on the right. Yellow lines connect each HTML element, like `<label>` and `<input>`, to its visual output, clearly demonstrating how the code creates the form structure.

Creating Your First Form: A Guided Walkthrough

Now let's build a form step-by-step. We will use a combination of reading and watching a video to see the concepts in practice.

First, let's get the foundational code from the MDN article. We'll focus on setting up the form container, the text inputs with their labels, and the submit button.

Your first form - Learn web development | MDN

This part of the MDN article walks you through the HTML needed for a simple contact form.

First, read The <form> element section to understand the container tag. Don't worry about the action and method attributes for now; we'll touch on those later. Next, read the section on labels, inputs, and textareas. Pay close attention to how the <label>'s for attribute is linked to the <input>'s id attribute. This connection is what makes them work together. Finally, read The <button> element section to learn how to add a submit button.

To see these concepts in action, let's watch a video where a developer builds a similar form from scratch. Watching the process can help solidify your understanding.

[HTML-Tutorial-19] Basic Form | form, input, label, button elements | Web Development for Beginners

This video from Code Range provides a clear, practical demonstration of creating a basic form.

Watch from the beginning to the introduction to hear about the purpose of forms. Next, watch the segment on creating labels, inputs, and a button. Pay close attention to the explanation of how to connect labels and inputs. This reinforces the for/id concept you just read about. Finally, watch the part about the type attribute from this section. Notice how using type="email" provides automatic, built-in browser validation for email addresses.


Essential Attributes: name, action, and method

You may have noticed a few key attributes being used: name on the <input> tags, and action and method on the <form> tag.

  • name: This attribute is crucial. When a form is submitted, the data is sent as a series of name/value pairs. The name attribute provides the "name" for each piece of data. If you omit the name attribute from an input, its data will not be submitted.

  • action: This attribute tells the browser where to send the form data when it is submitted. The value is typically a URL pointing to a script on a server (e.g., in PHP, Python, or another backend language).

  • method: This specifies the HTTP method to use when submitting the form. The two most common values are GET and POST.

For now, you don't need to worry about setting up a server or writing scripts to handle the action. The key takeaway is to always include the name attribute on your input fields.

HTML Forms

This W3Schools article gives a very direct explanation of why the name attribute is necessary.

Read the short section titled The Name Attribute for .


Practice Time

Let's put everything together. Open your index.html file (or create a new one) and build a simple "Contact Me" form.

  1. Start with a <form> element. You can leave the action and method attributes off for now.
  2. Inside the form, add the following fields:
    • A text input for "Name".
    • A text input for "Email".
    • A multi-line text area for "Message". (Hint: Use the <textarea> tag you saw in the MDN article.)
  3. For each input field and the textarea, add a corresponding <label>.
  4. Make sure to connect each <label> to its control using the for and id attributes.
  5. Give each input and the textarea a unique name attribute (e.g., name="userName").
  6. For the email field, set the input type to "email" to get that free browser validation.
  7. Add a submit button at the end of your form using <button type="submit">Send</button>.

After writing your code, save the file and open it in your browser. Try clicking on the labels—does your cursor jump to the correct input field? If you submit the form with an invalid email, do you see a warning?


Conclusion

Congratulations on completing the HTML structuring module! You now have the power to not only display information but also to collect it. Forms are a gateway to creating dynamic, user-driven websites.

Key Takeaways:

  • Forms are created using the <form> element as a container.
  • The <input> element is used to create form controls; type="text" is for single-line text fields.
  • <label> tags are essential for accessibility and usability. They are linked to inputs via the for and id attributes.
  • A submit button, created with <button type="submit"> or <input type="submit">, is used to send the form's data.
  • The name attribute on an input is required for its data to be included in the form submission.

You've built a solid foundation in HTML, learning how to structure everything from simple text to complex forms. However, our pages still look quite plain. In the next module, we will dive into Cascading Style Sheets (CSS). You'll learn how to take the structures you've built and transform them into visually appealing, professionally designed web pages.