Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
HTML Responsive Web Design
Next article icon

HTML Forms

Last Updated : 04 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

HTML Forms use the <form> tag to collect user input through various interactive controls. These controls range from text fields, numeric inputs, and email fields to password fields, checkboxes, radio buttons, and submit buttons.

Table of Content

  • Form Elements
  • Commonly Used Input Types in HTML Forms
  • HTML Forms Example
    • Basic HTML Forms
    • Advance HTML Forms

Syntax:

<form>
<!--form elements-->
</form>

Form Elements

The HTML <form> comprises several elements, each serving a unique purpose. For instance, the <label> element defines labels for other <form> elements. On the other hand, the <input> element is versatile and can be used to capture various types of input data such as text, password, email, and more simply by altering its type attribute.

Elements

Descriptions

<label>It defines labels for <form> elements.
<input>It is used to get input data from various types such as text, password, email, etc by changing its type.
<button>It defines a clickable button to control other elements or execute a functionality.
<select>It is used to create a drop-down list.
<textarea>It is used to get input long text content.
<fieldset>It is used to draw a box around other form elements and group the related data.
<legend>It defines a caption for fieldset elements
<datalist>It is used to specify pre-defined list options for input controls.
<output>It displays the output of performed calculations.
<option>It is used to define options in a drop-down list.
<optgroup>It is used to define group-related options in a drop-down list.

Commonly Used Input Types in HTML Forms

In HTML forms, various input types are used to collect different types of data from users. Here are some commonly used input types:

Input Type

Description

<input type=”text”>

Defines a one-line text input field

<input type=”password”>

Defines a password field

<input type=”submit”>

Defines a submit button

<input type=”reset”>

Defines a reset button

<input type=”radio”>

Defines a radio button

<input type=”email”>

Validates that the input is a valid email address.

<input type=”number”>

Allows the user to enter a number. You can specify min, max, and step attributes for range.

<input type=”checkbox”>

Used for checkboxes where the user can select multiple options.

<input type=”date”>

Allows the user to select a date from a calendar.

<input type=”time”>

Allows the user to select a time.

<input type=”file”>

Allows the user to select a file to upload.

HTML Forms Example

Example 1: Basic HTML Forms

Example: This HTML form collects user personal information such as username and password with a button to submit the form.

HTML
<head>     <title>Html Forms</title> </head>  <body>     <h2>HTML Forms</h2>     <form>         <label for="username">Username:</label><br>         <input type="text" id="username" name="username"><br><br>          <label for="password">Password:</label><br>         <input type="password" id="password" name="password"><br><br>          <input type="submit" value="Submit">     </form> </body> 

Output:

HTML Forms

Basic HTML Form

In this example:

  • HTML Structure: The code has a basic HTML structure with a title “HTML Forms.”
  • Heading: The <h2> tag displays “HTML Forms” as the main heading on the page.
  • Form Tag: The <form> tag defines a form for user input.
  • Username Field: A text input field for the username with a label.
  • Password Field & Submit: A password input field and a submit button to send the form data.

Example 2: Advance HTML Forms

This HTML form collects users personal information, including name, email, password, gender, date of birth, and address. It features proper styling for input fields and submission buttons.

HTML
<head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>HTML Form</title>     <style>         body {             display: flex;             justify-content: center;             align-items: center;             height: 100vh;             margin: 0;             background-color: #f0f0f0;         }         form {             width: 400px;             background-color: #fff;             padding: 20px;             border-radius: 8px;             box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);         }         fieldset {             border: 1px solid black;             padding: 10px;             margin: 0;         }         legend {             font-weight: bold;             margin-bottom: 10px;         }         label {             display: block;             margin-bottom: 5px;         }         input[type="text"],         input[type="email"],         input[type="password"],         textarea,         input[type="date"] {             width: calc(100% - 20px);             padding: 8px;             margin-bottom: 10px;             box-sizing: border-box;             border: 1px solid #ccc;             border-radius: 4px;         }         .gender-group {             margin-bottom: 10px;         }         .gender-group label {             display: inline-block;             margin-left: 10px;         }         input[type="radio"] {             margin-left: 10px;             vertical-align: middle;         }         input[type="submit"] {             padding: 10px 20px;             border-radius: 5px;             cursor: pointer;         }     </style> </head> <body>     <form>         <fieldset>             <legend>User Personal Information</legend>             <label for="name">Enter your full name:</label>             <input type="text" id="name" name="name" required />             <label for="email">Enter your email:</label>             <input type="email" id="email" name="email" required />             <label for="password">Enter your password:</label>             <input type="password" id="password" name="pass" required />             <label for="confirmPassword">Confirm your password:</label>             <input type="password" id="confirmPassword" name="confirmPass" required />             <label>Enter your gender:</label>             <div class="gender-group">                 <input type="radio" name="gender" value="male" id="male" required />                 <label for="male">Male</label>                 <input type="radio" name="gender" value="female" id="female" />                 <label for="female">Female</label>                 <input type="radio" name="gender" value="others" id="others" />                 <label for="others">Others</label>             </div>             <label for="dob">Enter your Date of Birth:</label>             <input type="date" id="dob" name="dob" required />             <label for="address">Enter your Address:</label>             <textarea id="address" name="address" required></textarea>             <input type="submit" value="Submit" />         </fieldset>     </form> </body> 

Output:

HTMLForm3

HTML Forms Example Output

Here are some of the key attributes that can be used with the <form> element:

  1. action: This attribute specifies where to send the form-data when a form is submitted. The value of this attribute is typically a URL.
  2. method: This attribute defines the HTTP method used to send the form-data. The values can be “get” or “post”.
  3. target: This attribute specifies where to display the response received after submitting the form. The values can be “_blank”, “_self”, “_parent”, “_top”, or the name of an iframe.
  4. enctype: This attribute is used when method=“post”. It specifies how the form-data should be encoded when submitting it to the server. The values can be “application/x-www-form-urlencoded”, “multipart/form-data”, or “text/plain”.
  5. autocomplete: This attribute specifies whether a form should have autocomplete on or off. When autocomplete is on, the browser automatically completes values based on values that the user has entered before.
  6. novalidate: This Boolean attribute specifies that the form-data should not be validated on submission.


Next Article
HTML Responsive Web Design
author
vikashgautam11
Improve
Article Tags :
  • Class 10
  • HTML
  • Web Technologies
  • HTML-Basics
  • HTML5

Similar Reads

  • HTML Tutorial
    HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
    10 min read
  • HTML Introduction
    HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It defines the structure of a webpage by using a series of elements, tags, and attributes to organize text, images, links, and other multimedia elements. HTML is a markup language,
    7 min read
  • HTML Editors
    An HTML Editor is a software application designed to help users create and modify HTML code. It often includes features like syntax highlighting, tag completion, and error detection, which facilitate the coding process. There are two main types of HTML editors: Text-Based Editors - Allow direct codi
    5 min read
  • HTML Basics
    HTML (HyperText Markup Language) is the standard markup language for creating and structuring web pages. It defines the structure of a webpage using elements and tags.HTML is responsible for displaying text, images, and other content.It serves as the foundation for building websites and web applicat
    6 min read
  • HTML Comments
    HTML comments are used to add notes or explanations in the HTML code that are not displayed by the browser. They are useful for documenting the code, making it easier to understand and maintain.To add a comment, use the syntax <!-- your comment here -->. [GFGTABS] HTML <!-- This is a commen
    4 min read
  • HTML Elements
    An HTML Element consists of a start tag, content, and an end tag, which together define the element's structure and functionality. Elements are the basic building blocks of a webpage and can represent different types of content, such as text, links, images, or headings. For example, the <p> el
    5 min read
  • HTML Attributes
    HTML Attributes are special words used within the opening tag of an HTML element. They provide additional information about HTML elements. HTML attributes are used to configure and adjust the element's behavior, appearance, or functionality in a variety of ways. Each attribute has a name and a value
    9 min read
  • HTML Headings
    HTML headings are used to define the titles and subtitles of sections on a webpage. They help organize the content and create a structure that is easy to navigate. Proper use of headings enhances readability by organizing content into clear sections.Search engines utilize headings to understand page
    4 min read
  • HTML Paragraphs
    A paragraph in HTML is simply a block of text enclosed within the <p> tag. The <p> tag helps divide content into manageable, readable sections. It’s the go-to element for wrapping text in a web page that is meant to be displayed as a distinct paragraph. Syntax: <p> Content</p
    5 min read
  • HTML Text Formatting
    HTML text formatting refers to the use of specific HTML tags to modify the appearance and structure of text on a webpage. It allows you to style text in different ways, such as making it bold, italic, underlined, highlighted, or struck-through. Table of Content Categories of HTML Text FormattingLogi
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences