Home  >  Article  >  Web Front-end  >  Learn more about form controls in Bootstrap

Learn more about form controls in Bootstrap

青灯夜游
青灯夜游forward
2021-04-09 10:04:562855browse

This article will give you a detailed understanding of the form controls in Bootstrap. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Learn more about form controls in Bootstrap

Basic example


Individual form controls will be automatically assigned some global styles. All , and elements that have the .form-control class set will have the default width attribute set to width: 100%;. Wrapping the label element and the previously mentioned controls in a .form-group results in the best arrangement.

Learn more about form controls in Bootstrap

Learn more about form controls in Bootstrap

Do not mix form groups and input box groups. Do not mix form groups directly with input box groups. use. It is recommended to nest the input box group into the form group.

Related recommendations: "bootstrap tutorial"

Inline form


is Adding the .form-inline class to a element causes its content to be left aligned and behave as an inline-block level control. Only applies when the viewport is at least 768px wide (any smaller viewport width will cause the form to collapse).

You may need to set the width manually

In Bootstrap, the input box and radio/multi-select box controls are set to width: 100%; width by default . In the inline form, we set the width of these

elements to width: auto;, so multiple controls can be arranged on the same line. Depending on your layout needs, some

additional custom components may be required.

Be sure to add a label

If you do not set a label for each input control, screen readers will not recognize it correctly. For these inline forms, you can hide it by setting the .sr-only class for the

label. There are also assistive technologies that provide alternatives to label tags, such as aria-label, aria-labelledby or the title attribute. If none of these are present, screen readers may resort to using the

placeholder attribute, if present, to use the placeholder instead of other markup, but be aware that this approach is inappropriate.

<form class="form-inline">
  <div class="form-group">
    <label for="exampleInputName2">Name</label>
    <input type="text" class="form-control" id="exampleInputName2" placeholder="Jane Doe">
  </div>
  <div class="form-group">
    <label for="exampleInputEmail2">Email</label>
    <input type="email" class="form-control" id="exampleInputEmail2" placeholder="jane.doe@example.com">
  </div>
  <button type="submit" class="btn btn-default">Send invitation</button>
</form>
Learn more about form controls in Bootstrap

<form class="form-inline">
  <div class="form-group">
    <label class="sr-only" for="exampleInputEmail3">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail3" placeholder="Email">
  </div>
  <div class="form-group">
    <label class="sr-only" for="exampleInputPassword3">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword3" placeholder="Password">
  </div>
  <div class="checkbox">
    <label>
      <input type="checkbox"> Remember me
    </label>
  </div>
  <button type="submit" class="btn btn-default">Sign in</button>
</form>
Learn more about form controls in Bootstrap

<form class="form-inline">
  <div class="form-group">
    <label class="sr-only" for="exampleInputAmount">Amount (in dollars)</label>
    <div class="input-group">
      <div class="input-group-addon">$</div>
      <input type="text" class="form-control" id="exampleInputAmount" placeholder="Amount">
      <div class="input-group-addon">.00</div>
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Transfer cash</button>
</form>
Learn more about form controls in Bootstrap

Horizontally arranged form


By adding the
.form-horizontal

class to the form and using Bootstrap's preset grid class, you can add the label label and control Groups are laid out horizontally side by side. Doing so will change the behavior of .form-group so that it behaves as rows in a grid system, so there is no need to add an additional .row.

<form class="form-horizontal">
  <div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <div class="checkbox">
        <label>
          <input type="checkbox"> Remember me
        </label>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">Sign in</button>
    </div>
  </div>
</form>
Learn more about form controls in Bootstrap

Supported controls


The form layout example shows the supported controls standard form controls.


Input boxIncludes most form controls and text input field controls, and also supports all HTML5 types of input Controls:

text

, password, datetime, datetime-local, date, monthtimeweeknumberemailurlsearchtel and color.

Type declaration must be added

Only input controls with the type attribute correctly set can be given the correct style.

Learn more about form controls in Bootstrap

Input control group

If you need to add text content or button controls before or after the text input field, Please refer to Input Control Group.

Text fieldA form control that supports multi-line text. The

rows

property can be changed as needed.

多选和单选框

多选框(checkbox)用于选择列表中的一个或多个选项,而单选框(radio)用于从多个选项中只选择一个。

Disabled checkboxes and radios are supported, but to provide a "not-allowed" cursor on hover of the parent <label></label>, you'll need to add the .disabled class to the parent .radio, .radio-inline, .checkbox, or .checkbox-inline.

Learn more about form controls in Bootstrap

<div class="checkbox">
  <label>
    <input type="checkbox" value="">
    Option one is this and that&mdash;be sure to include why it&#39;s great
  </label>
</div>
<div class="checkbox disabled">
  <label>
    <input type="checkbox" value="" disabled>
    Option two is disabled
  </label>
</div>

<div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
    Option one is this and that&mdash;be sure to include why it&#39;s great
  </label>
</div>
<div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
    Option two can be something else and selecting it will deselect option one
  </label>
</div>
<div class="radio disabled">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios3" value="option3" disabled>
    Option three is disabled
  </label>
</div>

内联单选和多选框

通过将 .checkbox-inline.radio-inline 类应用到一系列的多选框(checkbox)或单选框(radio)控件上,可以使这些控件排列在一行。

Learn more about form controls in Bootstrap

<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox1" value="option1"> 1
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox2" value="option2"> 2
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox3" value="option3"> 3
</label>

<label class="radio-inline">
  <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> 1
</label>
<label class="radio-inline">
  <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> 2
</label>
<label class="radio-inline">
  <input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3"> 3
</label>

不带label文本的Checkbox 和 radio

如果需要 <label></label> 内没有文字,输入框(input)正是你所期望的。 目前只适用于非内联的 checkbox 和 radio。 请记住,仍然需要为使用辅助技术的用户提供某种形式的 label(例如,使用 aria-label)

1Learn more about form controls in Bootstrap

<div class="checkbox">
  <label>
    <input type="checkbox" id="blankCheckbox" value="option1" aria-label="...">
  </label>
</div>
<div class="radio">
  <label>
    <input type="radio" name="blankRadio" id="blankRadio1" value="option1" aria-label="...">
  </label>
</div>

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Learn more about form controls in Bootstrap. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete