Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap Forms

Bootstrap Forms

Introduction

In this tutorial, you will learn how to use the Bootstrap 2.0 toolkit to create forms.

Bootstrap has defined styles for form controls such as input, textarea, and select, supports lists and checkboxes, defines styles for disabled form controls, and includes error, warning, and success states for each form control.

Since version 2.0, Bootstrap has provided four types of form layouts -

In the previous version of Bootstrap, the default form layout was horizontal. However, since version 2.0, the vertical layout is the default.

Creating a Vertical Form Layout

Styles are defined for the Bootstrap default form layout (i.e., the vertical form) via the .form-vertical class in bootstrap.css. But since this is the default form layout, you do not need to specify the .form-vertical class when creating a form with the default layout. The following example demonstrates a form created with the Bootstrap 2.0 default form layout.

The .well class is used to create a container for the form (of course, it has other uses as well). This class can be found at lines 1650 to 1663 in bootstrap.css. For this layout, the input fields areblock levelThe following example demonstrates how to use Bootstrap to create a default form layout.

Example

    

    

    Related help documentation!

    

         Check me

    

    

View online

View the example above in different browser windows.

Creating a Search Form Layout

Use the .form-search class, located at lines 962 to 1003 in bootstrap.css (these lines also contain the styles for .form-inline), to create a search form. For this layout, the input fields areblock levelHere is an example:

Example

View online

View the example above in different browser windows.

Creating an Inline Form Layout

Use the .form-inline class, located at lines 962 to 1003 in bootstrap.css (these lines also contain the styles for .form-search), to create an inline form. For this layout, the input fields areinlineHere is an example:

Example

    

    

    

         Remember me

    

    

View online

View the example above in different browser windows.

Creating a Horizontal Form Layout

Use the .form-horizontal class in bootstrap.css to create a horizontal form. For this layout, the input fields areblock levelHere is an example:

Example

    

        Controls Supported by Bootstrap

        

            

            

                

                

Besides free-form text, some HTML5 text-based inputs are rendered like this.

            

        

        

            

            

                

                    

                    Select the option to confirm this item is correct.

                

            

        

        

            

            

                

            

        

        

            

            

                

            

        

        

            

            

                

            

        

        

            

            

                

            

        

        

            

            

        

    

View online

View the example above in different browser windows.

Browser States of Bootstrap Form Controls

When an input field gains focus, or is disabled, or is read-only, Bootstrap has defined styles for these. In bootstrap.css, lines 677 to 697 define styles for focused input boxes and input field elements.

First, Webkit used "outline" as the value for ":focus", but now it has changed to "box-shadow".

The following is an example showing how the styles of a focused input box, a read-only input box, a disabled input box, a disabled checkbox, and a disabled button change.

Example

    

        Controls Supported by Bootstrap

        

            

            

                

                

Besides free-form text, some HTML5 text-based inputs are rendered like this.

            

        

        

            

            

                

                

Besides free-form text, some HTML5 text-based inputs are rendered like this.

            

        

        

            

            

                

                

Besides free-form text, some HTML5 text-based inputs are rendered like this.

            

        

        

            

            

                

                    

                    Select the option to confirm this item is correct.

                

            

        

        

            

            

        

    

View online

View the example above in different browser windows.

Defining Styles for Form Validation

Bootstrap 2.0 can define styles for error, warning, and success states when validating forms. This is a great feature, because when a user submits form data with errors, or when a warning needs to be generated, or even when notifying the user of successful submission, different styles need to be shown to the user.

Here is an example:

Example

    

        Form validation error, warning, or success

        

            

            

                

                Please correct the error

            

        

        

            

            

                

                Some errors occurred

            

        

        

            

            

                

                Successful input

            

        

        

            

            

                

                Selection successful

            

        

    

View online

View the example above in different browser windows.

Click here to download all the HTML, CSS, JS, and image files used in this tutorial.