Bootstrap Creating a form (i)

Source: Internet
Author: User

Bootstrap form types are divided into three formats: vertical or basic forms, inline forms, horizontal forms.

Vertical or basic form (display:block;)

The basic form structure is Bootstrap, and individual form controls automatically receive some global styles. The steps to create a basic form are listed below:

    • < Span style= "Background-color:inherit" > add  role= "to parent <form> elements" Form ".
    • put tags and controls in a form-group with class. <div > in. This is required to get the best spacing.
    • to all text elements <input>, <textarea > and <select> Add Class. Form-control.
<!doctype html>
The effect is as follows:


If we remove the Form-control of select and add Form-control to input type = "File", see how it works.


Can you see the difference with the above effect? This way you probably understand the role of Form-control, which is to set the perimeter border effect, including width, height, and get

The style of the form when focusing.


inline form (all on the same line, display is Inline-block)

If you need to create a form, all its elements are inline, aligned to the left, and the labels are side-by, add class to the <form> tag. Form-inline.
Because the head part of the content is the same, so we only list the body part of the content.

<body style= "padding:20px;" > <form role = "form" class= "Form-inline" > <div class = "Form-group" > <label for = "name" &G t; name </label> <input type = "text" class = "Form-control" id = "name" placeholder = "Please enter name" &GT;&LT;/INPUT&G    T </div> <div class = "Form-group" > <label for = "TEL" > Phone number </label> <input type= "Text" class= "Form-control" id = "tel" placeholder = "Please enter your phone number" ></input> </div> <div class = "for M-group "> <label for =" Idcard "> Please upload id </label> <input type =" file "id =" Idcard "></        Input> </div> <div class = "Form-group" > <label for = "Profession" > select Occupation </label> <select id = "profession" class = "Form-control" > <option> software engineer </option> < Option> Test Engineer </option> <option> hardware Engineer </option> <option> quality analyst &Lt;/option> </select> </div> <div class= "Form-group" > <button type = "Submit" C lass= "Btn-info btn-lg" > Submit </button> </div></form></body>

Display effect:


    • By default, input, select, and textarea in Bootstrap have a width of 100%. When using a horizontal form, you need to set a width on the form control.
    • Using Class . sr-only, you can hide labels for inline forms.

Note: Sr-only is shown to screen readers, not to ordinary readers. other elements when the form class is Form-inline, display is inline-block, but input tyoe = "File" is still display:block, you can see that its layout is problematic , the display for Inline-block can be set separately at this time;

horizontal form (form elements such as label and input are on the same line)

Horizontal forms differ from other forms not only in the number of marks, but also in the form. To create a horizontal layout form, follow these steps:

1. Add class to Parent <form> element. form-horizontal. 2. Place the labels and controls in a <div> with class . Form-group . 3. Add class to tag. Control-label. 4 . Set the width of the label and its sibling div (because the default width, such as input, is 100%).

<body style= "padding:20px;" > <form role = "form" class= "Form-horizontal" > <div class = "Form-group" > <label class= "col -sm-2 Control-label "for =" name "> name </label> <div class=" col-sm-2 "> <input type =" Tex T "class =" Form-control "id =" name "placeholder =" Please enter name "></input> </div> < /div> <div class = "Form-group" > <label class= "col-sm-2 control-label" for = "TEL" > Phone number </label                     > <div class= "col-sm-2" > <input type= "text" class= "Form-control" id = "Tel"        placeholder = "Please enter your phone number" ></input> </div> </div> <div class = "Form-group" >            <label class= "Col-sm-2 control-label" for = "Idcard" > Please upload ID </label> <div class= "Col-sm-2" > <input type = "file" id = "Idcard" style= "Display:inline-block;" ></input> </div>    </div> <div class = "Form-group" > <label class= "col-sm-2 control-label" for = "profession"                 Select Occupation </label> <div class= "Col-sm-2" > <select id = "profession" class = "Form-control" > <option> Software engineer </option> <option> test Engineer </option> <op Tion> Hardware Engineer </option> <option> quality analyst </option> </select> </div&    Gt </div> <div class= "Form-group" > <div class= "col-sm-2 col-sm-offset-2" > <button type = "Submi T "class=" Btn-info btn-lg "> Submit </button> </div> </div></form> </body>

Effect:



Bootstrap Creating a form (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.