DEV Community

Cover image for Day 5: Creating Forms in HTML
Dipak Ahirav
Dipak Ahirav

Posted on

Day 5: Creating Forms in HTML

Welcome to Day 5 of your journey to mastering HTML and CSS! Today, we will explore how to create forms in HTML. Forms are essential for collecting user input, whether it's for a sign-up page, a search box, or feedback submission. By the end of this post, you'll be able to create and style various types of forms for your web pages.

Basic Form Structure

An HTML form is defined with the <form> tag. Inside the form, you can add input fields, labels, buttons, and other elements to collect user data.

Here's a simple example of an HTML form:

<form action="/submit-form" method="post">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name"><br><br>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email"><br><br>
    <input type="submit" value="Submit">
</form>
Enter fullscreen mode Exit fullscreen mode

Form Elements

Let's break down the components of the form:

1.Form Tag: The <form> tag defines the start of a form. The action attribute specifies the URL where the form data will be sent, and the method attribute specifies the HTTP method (GET or POST) to use when sending the form data.

2.Labels: The <label> tag defines a label for an input element. The for attribute should match the id of the corresponding input element.

3.Input Fields: The <input> tag is used to create input fields. There are various types of input fields:
- type="text": Single-line text input.
- type="email": Email input, which validates email format.
- type="password": Password input, which hides the entered text.
- type="submit": Submit button to send the form data.

4.Textarea: The <textarea> tag is used for multi-line text input.

   <label for="message">Message:</label>
   <textarea id="message" name="message" rows="4" cols="50"></textarea>
Enter fullscreen mode Exit fullscreen mode

5.Select: The <select> tag is used to create a drop-down list.

   <label for="country">Country:</label>
   <select id="country" name="country">
       <option value="usa">USA</option>
       <option value="canada">Canada</option>
       <option value="uk">UK</option>
   </select>
Enter fullscreen mode Exit fullscreen mode

6.Radio Buttons: The <input type="radio"> tag is used to create radio buttons.

   <label for="gender">Gender:</label>
   <input type="radio" id="male" name="gender" value="male">
   <label for="male">Male</label>
   <input type="radio" id="female" name="gender" value="female">
   <label for="female">Female</label>
Enter fullscreen mode Exit fullscreen mode

7.Checkboxes: The <input type="checkbox"> tag is used to create checkboxes.

   <label for="subscribe">Subscribe to newsletter:</label>
   <input type="checkbox" id="subscribe" name="subscribe">
Enter fullscreen mode Exit fullscreen mode

Creating Your HTML Form

Let's create an HTML form that incorporates various input elements:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Form</title>
</head>
<body>
    <h1>Contact Us</h1>
    <form action="/submit-form" method="post">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name"><br><br>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email"><br><br>
        <label for="message">Message:</label>
        <textarea id="message" name="message" rows="4" cols="50"></textarea><br><br>
        <label for="country">Country:</label>
        <select id="country" name="country">
            <option value="usa">USA</option>
            <option value="canada">Canada</option>
            <option value="uk">UK</option>
        </select><br><br>
        <label for="gender">Gender:</label>
        <input type="radio" id="male" name="gender" value="male">
        <label for="male">Male</label>
        <input type="radio" id="female" name="gender" value="female">
        <label for="female">Female</label><br><br>
        <label for="subscribe">Subscribe to newsletter:</label>
        <input type="checkbox" id="subscribe" name="subscribe"><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Summary

In this blog post, we learned how to create and style forms in HTML. We explored various form elements, including input fields, labels, text areas, drop-down lists, radio buttons, and checkboxes. Practice creating forms to collect user data effectively.

Stay tuned for Day 6, where we will cover semantic HTML and its benefits. Happy coding!

Feel free to leave your comments or questions below. If you found this guide helpful, please share it with your peers and follow me for more web development tutorials. Happy coding!

Follow and Subscribe:

Top comments (0)