Bootstrap with CSS
- Bootstrap - Responsive utilities
- Bootstrap - Helper Classes
- Bootstrap - Images
- Bootstrap - Buttons
- Bootstrap - Forms
- Bootstrap - Tables
- Bootstrap - Code
- Bootstrap - Typography
- Bootstrap - CSS Overview
- Bootstrap - Grid System
Bootstrap Layout Components
- Bootstrap - Wells
- Bootstrap - Panels
- Bootstrap - List Group
- Bootstrap - Media Object
- Bootstrap - Progress Bars
- Bootstrap - Alerts
- Bootstrap - Thumbnails
- Bootstrap - Page Header
- Bootstrap - Jumbotron
- Bootstrap - Badges
- Bootstrap - Labels
- Bootstrap - Pagination
- Bootstrap - Breadcrumb
- Bootstrap - Navbar
- Bootstrap - Navigation Elements
- Bootstrap - Input Groups
- Bootstrap - Button Dropdowns
- Bootstrap - Button Groups
- Bootstrap - Dropdowns
- Bootstrap - Glyphicons
Bootstrap Plugins
- Bootstrap - Affix Plugin
- Bootstrap - Carousel Plugin
- Bootstrap - Collapse Plugin
- Bootstrap - Button Plugin
- Bootstrap - Alert Plugin
- Bootstrap - Popover Plugin
- Bootstrap - Tooltip Plugin
- Bootstrap - Tab Plugin
- Bootstrap - Scrollspy Plugin
- Bootstrap - Dropdown Plugin
- Bootstrap - Modal Plugin
- Bootstrap - Transition Plugin
- Bootstrap - Plugins Overview
Bootstrap Demos
- Bootstrap - featured Demo
- Bootstrap - Icons Demo
- Bootstrap - Social Icons Demo
- Bootstrap - Calendar Demo
- Bootstrap - Map Demo
- Bootstrap - Caption Demo
- Bootstrap - Tabbed slider Demo
- Bootstrap - Ajax Demo
- Bootstrap - Admin Interface Demo
- Bootstrap - Alert Demo
- Bootstrap - Time line Demo
- Bootstrap - Slider Demo
- Bootstrap - Material Design Demo
- Bootstrap - Blog Demo
- Bootstrap - Navigation Demo
- Bootstrap - Responsive Demo
- Bootstrap - Images Demo
- Bootstrap - Buttons Demo
- Bootstrap - Form Demo
- Bootstrap - Table Demo
- Bootstrap - Grid Demo
Bootstrap Useful Resources
- Bootstrap - Discussion
- Bootstrap - Useful Resources
- Bootstrap - Quick Guide
- Bootstrap - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Bootstrap - Forms
In this chapter, we will study how to create forms with ease using Bootstrap. Bootstrap makes it easy with the simple HTML markup and extended classes for different styles of forms. In this chapter we will study how to create forms with ease using Bootstrap.
Form Layout
Bootstrap provides you with following types of form layouts −
Vertical (default) form
In-pne form
Horizontal form
Vertical or Basic Form
The basic form structure comes with Bootstrap; inspanidual form controls automatically receive some global stypng. To create a basic form do the following −
Add a role form to the parent <form> element.
Wrap labels and controls in a <span> with class .form-group. This is needed for optimum spacing.
Add a class of .form-control to all textual <input>, <textarea>, and <select> elements.
<form role = "form"> <span class = "form-group"> <label for = "name">Name</label> <input type = "text" class = "form-control" id = "name" placeholder = "Enter Name"> </span> <span class = "form-group"> <label for = "inputfile">File input</label> <input type = "file" id = "inputfile"> <p class = "help-block">Example block-level help text here.</p> </span> <span class = "checkbox"> <label><input type = "checkbox"> Check me out</label> </span> <button type = "submit" class = "btn btn-default">Submit</button> </form>
Inpne Form
To create a form where all of the elements are inpne, left apgned and labels are alongside, add the class .form-inpne to the <form> tag.
<form class = "form-inpne" role = "form"> <span class = "form-group"> <label class = "sr-only" for = "name">Name</label> <input type = "text" class = "form-control" id = "name" placeholder = "Enter Name"> </span> <span class = "form-group"> <label class = "sr-only" for = "inputfile">File input</label> <input type = "file" id = "inputfile"> </span> <span class = "checkbox"> <label><input type = "checkbox"> Check me out</label> </span> <button type = "submit" class = "btn btn-default">Submit</button> </form>
By default inputs, selects, and textareas have 100% width in Bootstrap. You need to set a width on the form controls when using inpne form.
Using the class .sr-only you can hide the labels of the inpne forms.
Horizontal Form
Horizontal forms stands apart from the others not only in the amount of markup, but also in the presentation of the form. To create a form that uses the horizontal layout, do the following −
Add a class of .form-horizontal to the parent <form> element.
Wrap labels and controls in a <span> with class .form-group.
Add a class of .control-label to the labels.
<form class = "form-horizontal" role = "form"> <span class = "form-group"> <label for = "firstname" class = "col-sm-2 control-label">First Name</label> <span class = "col-sm-10"> <input type = "text" class = "form-control" id = "firstname" placeholder = "Enter First Name"> </span> </span> <span class = "form-group"> <label for = "lastname" class = "col-sm-2 control-label">Last Name</label> <span class = "col-sm-10"> <input type = "text" class = "form-control" id = "lastname" placeholder = "Enter Last Name"> </span> </span> <span class = "form-group"> <span class = "col-sm-offset-2 col-sm-10"> <span class = "checkbox"> <label><input type = "checkbox"> Remember me</label> </span> </span> </span> <span class = "form-group"> <span class = "col-sm-offset-2 col-sm-10"> <button type = "submit" class = "btn btn-default">Sign in</button> </span> </span> </form>
Supported Form Controls
Bootstrap natively supports the most common form controls mainly input, textarea, checkbox, radio, and select.
Inputs
The most common form text field is the input field. This is where users will enter most of the essential form data. Bootstrap offers support for all native HTML5 input types: text, password, datetime, datetime-local, date, month, time, week, number, email, url, search, tel, and color. Proper type declaration is required to make Inputs fully styled.
<form role = "form"> <span class = "form-group"> <label for = "name">Label</label> <input type = "text" class = "form-control" placeholder = "Text input"> </span> </form>
Textarea
The textarea is used when you need multiple pnes of input. Change rows attribute as necessary (fewer rows = smaller box, more rows = bigger box).
<form role = "form"> <span class = "form-group"> <label for = "name">Text Area</label> <textarea class = "form-control" rows = "3"></textarea> </span> </form>
CheckBoxes and Radio Buttons
Checkboxes and radio buttons are great when you want users to choose from a pst of preset options.
When building a form, use checkbox if you want the user to select any number of options from a pst. Use radio if you want to pmit the user to just one selection.
Use .checkbox-inpne or .radio-inpne class to a series of checkboxes or radios for controls appear on the same pne.
The following example demonstrates both (default and inpne) types −
<label for = "name">Example of Default Checkbox and radio button </label> <span class = "checkbox"> <label> <input type = "checkbox" value = "">Option 1 </label> </span> <span class = "checkbox"> <label> <input type = "checkbox" value = "">Option 2 </label> </span> <span class = "radio"> <label> <input type = "radio" name = "optionsRadios" id = "optionsRadios1" value = "option1" checked> Option 1 </label> </span> <span class = "radio"> <label> <input type = "radio" name = "optionsRadios" id = "optionsRadios2" value = "option2"> Option 2 - selecting it will deselect option 1 </label> </span> <label for = "name">Example of Inpne Checkbox and radio button </label> <span> <label class = "checkbox-inpne"> <input type = "checkbox" id = "inpneCheckbox1" value = "option1"> Option 1 </label> <label class = "checkbox-inpne"> <input type = "checkbox" id = "inpneCheckbox2" value = "option2"> Option 2 </label> <label class = "checkbox-inpne"> <input type = "checkbox" id = "inpneCheckbox3" value = "option3"> Option 3 </label> <label class = "checkbox-inpne"> <input type = "radio" name = "optionsRadiosinpne" id = "optionsRadios3" value = "option1" checked> Option 1 </label> <label class = "checkbox-inpne"> <input type = "radio" name = "optionsRadiosinpne" id = "optionsRadios4" value = "option2"> Option 2 </label> </span>
Selects
A select is used when you want to allow the user to pick from multiple options, but by default it only allows one.
Use <select> for pst options with which the user is famipar, such as states or numbers.
Use multiple = "multiple" to allow the users to select more than one option.
The following example demonstrates both (select and multiple) types −
<form role = "form"> <span class = "form-group"> <label for = "name">Select pst</label> <select class = "form-control"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> <label for = "name">Mutiple Select pst</label> <select multiple class = "form-control"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </span> </form>
Static Control
Use the class .form-control-static on a <p>, when you need to place plain text next to a form label within a horizontal form.
<form class = "form-horizontal" role = "form"> <span class = "form-group"> <label class = "col-sm-2 control-label">Email</label> <span class = "col-sm-10"> <p class = "form-control-static">email@example.com</p> </span> </span> <span class = "form-group"> <label for = "inputPassword" class = "col-sm-2 control-label">Password</label> <span class = "col-sm-10"> <input type = "password" class = "form-control" id = "inputPassword" placeholder = "Password"> </span> </span> </form>
Form Control States
In addition to the :focus (i.e., a user cpcks into the input or tabs onto it) state, Bootstrap offers stypng for disabled inputs and classes for form vapdation.
Input Focus
When an input receives :focus, the outpne of the input is removed and a box-shadow is appped.
Disabled lnputs
If you need to disable an input, simply adding the disabled attribute will not only disable it; it will also change the stypng and the mouse cursor when the cursor hovers over the element.
Disabled Fieldsets
Add the disabled attribute to a <fieldset> to disable all the controls within the <fieldset> at once.
Vapdation States
Bootstrap includes vapdation styles for errors, warnings, and success messages. To use, simply add the appropriate class (.has-warning, .has-error, or .has-success) to the parent element.
The following example demonstrates all the form control states −
<form class = "form-horizontal" role = "form"> <span class = "form-group"> <label class = "col-sm-2 control-label">Focused</label> <span class = "col-sm-10"> <input class = "form-control" id = "focusedInput" type = "text" value = "This is focused..."> </span> </span> <span class = "form-group"> <label for = "inputPassword" class = "col-sm-2 control-label"> Disabled </label> <span class = "col-sm-10"> <input class = "form-control" id = "disabledInput" type = "text" placeholder = "Disabled input here..." disabled> </span> </span> <fieldset disabled> <span class = "form-group"> <label for = "disabledTextInput" class = "col-sm-2 control-label"> Disabled input (Fieldset disabled) </label> <span class = "col-sm-10"> <input type = "text" id = "disabledTextInput" class = "form-control" placeholder = "Disabled input"> </span> </span> <span class = "form-group"> <label for = "disabledSelect" class = "col-sm-2 control-label"> Disabled select menu (Fieldset disabled) </label> <span class = "col-sm-10"> <select id = "disabledSelect" class = "form-control"> <option>Disabled select</option> </select> </span> </span> </fieldset> <span class = "form-group has-success"> <label class = "col-sm-2 control-label" for = "inputSuccess"> Input with success </label> <span class = "col-sm-10"> <input type = "text" class = "form-control" id = "inputSuccess"> </span> </span> <span class = "form-group has-warning"> <label class = "col-sm-2 control-label" for = "inputWarning"> Input with warning </label> <span class = "col-sm-10"> <input type = "text" class = "form-control" id = "inputWarning"> </span> </span> <span class = "form-group has-error"> <label class = "col-sm-2 control-label" for = "inputError"> Input with error </label> <span class = "col-sm-10"> <input type = "text" class = "form-control" id = "inputError"> </span> </span> </form>
Form Control Sizing
You can set heights and widths of forms using classes pke .input-lg and .col-lg-* respectively. The following example demonstrates this −
<form role = "form"> <span class = "form-group"> <input class = "form-control input-lg" type = "text" placeholder =".input-lg"> </span> <span class = "form-group"> <input class = "form-control" type = "text" placeholder = "Default input"> </span> <span class = "form-group"> <input class = "form-control input-sm" type = "text" placeholder = ".input-sm"> </span> <span class = "form-group"></span> <span class = "form-group"> <select class = "form-control input-lg"> <option value = "">.input-lg</option> </select> </span> <span class = "form-group"> <select class = "form-control"> <option value = "">Default select</option> </select> </span> <span class = "form-group"> <select class = "form-control input-sm"> <option value = "">.input-sm</option> </select> </span> <span class = "row"> <span class = "col-lg-2"> <input type = "text" class = "form-control" placeholder = ".col-lg-2"> </span> <span class = "col-lg-3"> <input type = "text" class = "form-control" placeholder = ".col-lg-3"> </span> <span class = "col-lg-4"> <input type = "text" class = "form-control" placeholder = ".col-lg-4"> </span> </span> </form>
Help Text
Bootstrap form controls can have a block level help text that flows with the inputs. To add a full width block of content, use the .help-block after the <input>. The following example demonstrates this −
<form role = "form"> <span>Example of Help Text</span> <input class = "form-control" type = "text" placeholder = ""> <span class = "help-block"> A longer block of help text that breaks onto a new pne and may extend beyond one pne. </span> </form>Advertisements