How to create Forms [Vertical, Horizontal and Inline Form] using bootstrap
What Will I Learn?
- You will learn some class that provided by bootstrap to create a form
- You will learn how to create vertical form
- You will learn how to create horizontal form
- You will learn how to create inline form
Requirements
- You have basic about HTML
- You have basic about CSS
- You have basic about JavaScript
- You should Install or host the bootstrap file. If you don't want do that, you can add the bootstrap CDN.
- Text Editor and a Browser
Difficulty
- Basic
Tutorial Contents
In web design, Form is a very important thing. Almost all websites use forms. Therefore, Bootstrap provides three types of form layouts. They are Vertical Form (default form), Horizontal Form and Inline Form. For more detail, Lets pay attention steps bellow :
Open your Text Editor Software [in this tutorial I use visual studio code]
Create new file and save as html extention. For ex
form.htmlAs we know for using bootstrap we should use HTML5 Doctype. Add the HTML5 element on your file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Pagination</title>
</head>
<body>
</body>
</html>
- For using bootstrap framework we should install or host bootstrap file first. If we don't want to do that, you can also add Bootstrap CDN as a replacement. You can visit bootstrap official website to get it. add and put it in
<head>element
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
After adding all element above, Now we can start to code bootstrap.
To create a form. Open the
<form>tag as usual
<form>
</form>
- Wrap labels and form controls in
<div class="form-group">.
<div class="form-group">
</div>
- Add the label in
<div class="form-group">. And don't forget to addforatribut in<label>tag with the value is id of its textual
<label for="email">Email:</label>
- Then add a textual under
<label>element. To have a width of 100% we should add.form-controlto all textual element.
<input type="email" class="form-control" id="email" placeholder="Enter your email">
- Add more form group under the email. For example I add password from under email.
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter your password">
</div>
- As usual, Every form has a button to submit all input from user. So create a button Under all textual. To create button, Bootstrap has provided
.btnclass
<button type="submit" class="btn">Submit</button>
We already created a form with default style from bootstrap.It is Vertical Form. Now try to create Inline Form. To create Inline Form we need to add
.form-inlineclass like this
<h2>Inline form</h2>
<form class="form-inline">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter your password">
</div>
<button type="submit" class="btn">Submit</button>
</form>
- The Vertical and Inline Form are already Created. Now try to create Horizontal Form. To create Horizontal form we should add
.form-horizontalclass in<form>tag
<form class="form-horizontal">
- Then add
.control-labeland.col-sm-2class in all<label>element.
<label class="control-label col-sm-2" for="email">Email:</label>
- The Last Put all textual in
<div class="col-sm-10">element. Like this
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Vertical (Default) form</h2>
<form>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter your password">
</div>
<button type="submit" class="btn">Submit</button>
</form>
<h2>Inline form</h2>
<form class="form-inline">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter your password">
</div>
<button type="submit" class="btn">Submit</button>
</form>
<h2>Horizontal form</h2>
<form class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" placeholder="Enter your email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" placeholder="Enter your password">
</div>
</div>
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn">Submit</button>
</div>
</form>
</div>
</body>
</html>
LIVE DEMO
Curriculum
- How to create alert message [success, info, warning, danger] using bootstrap
- How to create Fixed Navigation Bar (top and bottom) using bootstrap
- How to create a badge using Bootstrap framework
- How to create Dynamic Tabs (Tabs Toggleable) Using Bootstrap
- How to create SlideShow (carousel) using bootstrap
- How to create Progress Bars Using Bootstrap [colored, striped and animated Progress Bars]
- How to create login Form on Modal using Bootstrap Framework
- How to Create Toggle Night / Day mode on the your website page Using Bootstrap
Posted on Utopian.io - Rewarding Open Source Contributors



bereh @sogata
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @sogata I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x