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
  • BS5 Tutorial
  • BS5 Interview Questions
  • BS5 Layout
  • BS5 Content
  • BS5 Components
  • BS5 Helpers
  • BS5 Utilities
  • BS4 Tutorial
  • BS Tutorial
  • Bootstrap Cheatsheet
  • Tailwind
  • CSS Frameworks
  • HTML Formatter
Open In App
Next Article:
How to Create Form Layouts with Bootstrap ?
Next article icon

How to create forms using Twitter Bootstrap ?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Every website that you visit will have some sort of form in it to collect data from the users. A Form consists of input fields that allow the user to enter the data. Bootstrap provides us with a very easy way to add customizable & responsive forms to our web pages. 

In this article, we will learn how to create forms using bootstrap 5.

Bootstrap Setup:

Step 1: Create a file with the name index.html and add the initial HTML5 boilerplate code to it.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <title>Bootstrap forms</title> </head>  <body></body>  </html> 

Step 2: Now to add bootstrap you need to include the following script tag in the body element of your HTML code:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"crossorigin="anonymous"></script>

And this link tag inside the head of your HTML code.

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"crossorigin="anonymous">

If you need the most up-to-date CDN links, go to the bootstrap website.

After doing this your index.html file should look like this:

HTML
<!DOCTYPE html> <html lang="en">  <head>     <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"            rel="stylesheet" integrity= "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"            crossorigin="anonymous">     <title>Bootstrap forms</title> </head>  <body>     <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"         integrity= "sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"         crossorigin="anonymous">     </script> </body>  </html> 

Input fields: A basic form contains text input fields, labels, textarea, select options, and a submit button.

To use bootstrap forms there are some standard rules that we can follow.

  • By default, the form created will be vertical
  • Add form-control class to all text inputs, and select tags.
  • For labels add the form-label class
  • For a checkbox add form-check class
  • For block-level texts use the form-text class

Example: The following code creates a vertical form with two text input fields, a checkbox, and a submit button with the primary bootstrap button.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <title>Bootstrap forms</title>     <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"            rel="stylesheet"           integrity= "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"            crossorigin="anonymous">     <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"             integrity= "sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"             crossorigin="anonymous">     </script> </head>   <body>     <!-- Adding it inside a container will center the form-->     <div class="container">         <h4>Basic vertical form with two text inputs a checkbox             and a submit button</h4>         <form>             <!-- mb-3 adds a margin bottom of 3 units to the div -->             <!--Email Input -->             <div class="mb-3">                 <label for="email"                         class="form-label">                   Email address:                 </label>                 <input type="email"                         class="form-control"                         id="email"                         placeholder="Enter your email id">             </div>             <!--Password input-->             <div class="mb-3">                 <label for="password"                         class="form-label">                     Password:                   </label>                 <input type="password"                         class="form-control"                         id="password"                         placeholder="Enter your password">                 <div id="password-help-block"                       class="form-text">                     Password needs to be 8 characters long                  </div>             </div>             <!--Checkbox -->             <div class="mb-3 form-check">                 <input type="checkbox"                         class="form-check-input"                         id="checkbox">                 <label class="form-check-label"                         for="checkbox">                     Check me!                   </label>             </div>             <!-- Submit Button -->             <button type="submit"                      class="btn btn-primary">               Submit             </button>         </form>     </div> </body>  </html> 

Output:

Basic bootstrap form

Dropdown menu: To add a dropdown using the form-select class on a normal select element and add options normally. All the styles would be applied.

Example: 

HTML
<!DOCTYPE html> <html lang="en">  <head>     <title>Bootstrap forms</title>     <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"            rel="stylesheet"         integrity= "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"            crossorigin="anonymous" />          <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"             integrity= "sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"             crossorigin="anonymous">     </script> </head>  <body>     <div class="container">         <h4>Dropdown menu</h4>         <form>             <select class="mb-3 form-select">                 <option selected>Default Option</option>                 <option value="option1">Option 1</option>                 <option value="option2">Option 2</option>             </select>         </form>     </div>  </body>  </html> 

Output:

dropdown-ezgifcom-optimize
Output

Toggle Switch: To add a toggle switch add both form-check and form-switch class to a checkbox input.

Example:

HTML
<!DOCTYPE html> <html lang="en">  <head>     <title>Bootstrap forms</title>     <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"            rel="stylesheet"         integrity= "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"           crossorigin="anonymous" />     <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"             integrity= "sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"             crossorigin="anonymous">     </script> </head>  <body>     <div class="container">         <h4 class="mt-3">Toggle Switches</h4>         <form>             <div class="form-check form-switch">                 <input class="form-check-input" type="checkbox"                         id="switch" />                 <label class="form-check-label" for="switch">                     Toggle Me                   </label>             </div>         </form>     </div> </body>  </html> 

Output:

Toggle Switch using bootstrap

Creating a complete form with input validation: To add user validation for input fields there are two methods.

  • By adding was-validated class to the form element if validation needs to be done on the client-side or,
  • by adding needs-validation class to the form element if validation is to be done on the server-side.

We can also add valid-feedback or an invalid-feedback message to tell the user what's missing before/after submitting the form.

Example: The following code creates two text input fields and a checkbox with client-side input validations.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <title>Bootstrap forms</title>     <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"            rel="stylesheet"         integrity= "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"            crossorigin="anonymous" />     <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"         integrity= "sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"         crossorigin="anonymous">     </script> </head>  <body>     <div class="container">         <h4>Form Validation</h4>          <form class="was-validated">             <!-- Email Address Input -->             <div class="mb-3">                 <label for="email"                         class="form-label">                   Email Address:                 </label>                 <input type="email"                         class="form-control"                         id="email"                         placeholder="Enter your email id"                         required />                 <div class="valid-feedback">                   Valid Email id                 </div>                 <div class="invalid-feedback">                   Please fill out this field.                 </div>             </div>             <!-- Password Input -->             <div class="mb-3">                 <label for="password"                         class="form-label">                   Password:                 </label>                 <input type="password"                         class="form-control"                         id="password"                         placeholder="Enter your password"                     minlength="8" required />                 <div class="form-text"                       id="password-help-block">                     Password needs to be 8 characters long.                 </div>                 <div class="valid-feedback">                   Valid Password.                 </div>                 <div class="invalid-feedback">                   Please fill out this field.                 </div>             </div>             <!-- Checkbox -->             <div class="form-check mb-3">                 <input class="form-check-input"                         type="checkbox"                         id="checkbox" required />                 <label class="form-check-label"                        for="checkbox">                     I agree on T & C.                   </label>                 <div class="valid-feedback">                   You Agree to the T & C.                 </div>                 <div class="invalid-feedback">                   Check this checkbox to continue.                 </div>             </div>             <button type="submit"                      class="btn btn-primary">               Submit             </button>         </form>     </div> </body>  </html> 

Output: This code will create the following form.


Next Article
How to Create Form Layouts with Bootstrap ?

C

cjain8060
Improve
Article Tags :
  • Web Technologies
  • Bootstrap
  • Bootstrap-Questions

Similar Reads

  • How to Create a Bootstrap Form Validation ?
    A Bootstrap Form Validation ensures that user input complies with established standards before submission, improves the user experience, and minimizes server-side processing for incorrect inputs. We can add client-side validation to their forms by using two different methods including Bootstrap buil
    3 min read
  • How to Create Form Layouts with Bootstrap ?
    Form Layouts in Bootstrap refer to the arrangement and presentation of form elements within a web page. To create Form Layouts with Bootstrap, utilize grid system classes to structure forms horizontally or vertically, and enhance them with input groups, inline forms, and responsive designs for impro
    5 min read
  • How to create a Simple Footer using Bootstrap 5 ?
    Bootstrap 5 Footers can be used for displaying Contact links, Social media links, Service links, Company Logos, and other sections. The <footer> tag can be used with built-in classes for making the responsive footer layout. For accomplishing this task, there are 2 approaches, i.e., by using Bo
    4 min read
  • How to create a vertical or basic form using Bootstrap ?
    Forms are used on almost every website that you visit. It is generally used to collect data from the users. It consists of various interactive controls that enable users to input the data. These controls are wrapped in the <form> tag and also can be designed or styled according to the need by
    4 min read
  • How to create a web page using Bootstrap ?
    Bootstrap is an open-source CSS framework for creating a responsive and customizable frontend for websites and web applications. Using Bootstrap's grid system one can easily create a web page very fast. Any webpage these days needs to have a navbar for user navigation, some content & a form for
    6 min read
  • Create a To Do List using Bootstrap 5
    A To-Do List is a tool for organizing tasks, allowing users to list, prioritize, and manage their activities, ensuring efficiency and productivity in completing them. Here we will create a ToDo list using Bootstrap. We will create our layout or component using Bootstrap predefined utilities and comp
    3 min read
  • How to create a form within dropdown menu using Bootstrap ?
    A Dropdown menu is used to allow the user to choose the content from the menu as per their requirement and it is used to save screen space and avoid the long scrolling of the web pages. In web design, we often come in a scenario where we need to create a form within the dropdown menu to get some use
    3 min read
  • How to Create ToDo App using HTML, CSS, JS and Bootstrap ?
    We will create a basic todo app to understand the basics of JavaScript. In this web app, one can create a todo list and can remove specific elements from the list.Features or Functionalities to implement:   Interactive and Responsive designResponsive Grid SystemStore and Delete itemsPrerequisites: B
    2 min read
  • How to write a form in different line using Bootstrap ?
    Bootstrap is an open-source front-end framework for developing standard & decorative websites or web applications that are fast and user-friendly. Bootstrap includes templates based on HTML and CSS for tables, forms, buttons, etc. Bootstrap also uses JavaScript and helps us to create responsive
    2 min read
  • How to Create Custom Input File with Bootstrap?
    This article will show you how to create a custom input file using the Bootstrap 5 library. Bootstrap 5 has various styling file input classes and functionalities that can enhance the visual appearance of custom file input. There are two different approaches along with their implementation. In both
    3 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