HTML - <form> Tag



A form can be created in HTML using the <form> tag to collect user input on a website. A user's name, address, and phone number are just a few of the possible options on this form that can be used to collect data about the user or to ask their opinions on a certain product.

These forms take data, which is then provided in HTML format to the URL that is supplied. These form tags can be used to build login functionality or registration forms on a website, etc.

Syntax

Following is the syntax for HTML <form> tag −

<form>.....</form>

Specific Attributes

The HTML <select > tag also supports the following additional attributes −

S.No. Attribute & Description
1

accept

Specifies a comma-separated list of content types that the server accepts.
2

accept-charset

Specifies a list of character encodings that the server accepts. The default value is "unknown".
3

action

Specifies a URI/URL of the back-end script that will process the form.
4

autocomplete

Specifies whether form should have autocomplete on or off<
5

enctype

The mime type used to encode the content of the form.
6

name

Defines a unique name for the form.
7

novalidate

Specifies that the form should not be validated when submitted.
8

method

Specifies the HTTP method to use when the form is submitted. Possible values

Example

In the following program, we are using HTML <form> tag to create a form for user input. This form (user login form) will have three input fields with type 'text', 'password', and ‘button’.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML form tag</title>
</head>
<body>
   <!--create a html form-->
   <h3>User login form</h3>
   <form> Username : <input type="text">
      <br>
      <br> Password : <input type="password">
      <br>
      <input type="button" value='Login'>
   </form>
</body>
</html>

When we run the above code, it will generate an output consisting of the input fields along with a click button on the webpage.

Example

Let's look at the following example, where we are going to use form tag and appling CSS to the input fields.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML form tag</title>
   <style>
      form {
         width: 300px;
         height: 260px;
         background-color: blueviolet;
         color: white;
         border-radius: 10px;
         margin: 10px auto;
      }

      form h3 {
         text-align: center;
         position: relative;
         top: 5px;
      }

      form label {
         position: relative;
         margin: 10px 10px;
         top: 10px;
      }

      form input {
         position: relative;
         top: 10px;
         margin: 10px 10px;
      }
   </style>
</head>
<body>
   <!--create a html form-->
   <form>
      <h3>User signup form</h3>
      <label>Name</label>
      <input type="text" placeholder="Name">
      <br>
      <label>Email</label>
      <input type="email" placeholder="Email">
      <br>
      <label>Phone</label>
      <input type="number" placeholder="Phone">
      <br>
      <label>Gender</label>
      <br>
      <label>Male</label>
      <input type="radio">
      <label>Female</label>
      <input type="radio">
      <br>
      <input type="button" value="Submit">
   </form>
</body>
</html>

On running the above code, the output window will pop up displaying the input field , radio buttons along with a click button applied with a CSS on the webpage.

html_tags_reference.htm
Advertisements