表单布局(Layout)

使用我们的表单布局选项,为表单提供从内联到水平再到自定义网格实现的一些结构。

Forms

每组表单字段都应该位于form元素中。Bootstrap没有为form元素提供默认样式,但是默认情况下提供了一些强大的浏览器功能。

  • 不熟悉浏览器窗体?考虑查看 MDN表单文档以获得可用属性的概述和完整列表。
  • form中的button默认为type="submit",因此要尽量具体并始终包含一个type
  • 您可以使用form上的disabled属性禁用表单中的每个表单元素。

由于Bootstrap将display:block和width:100%应用于几乎所有的表单控件,因此默认情况下表单将垂直堆叠。附加类可用于根据每个窗体更改此布局。

通用类

边距实用程序是向窗体添加某些结构的最简单方法。它们提供标签、控件、可选表单文本和表单验证消息的基本分组。我们建议坚持使用边距底部工具,并在整个表单中使用单一方向以保持一致性。

您可以随意使用fieldset、div或几乎任何其他元素构建表单。

<div class="mb-3">
        <label for="formGroupExampleInput" class="form-label">Example label</label>
        <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input placeholder">
        </div>
        <div class="mb-3">
        <label for="formGroupExampleInput2" class="form-label">Another label</label>
        <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input placeholder">
        </div>

表单网格

使用我们的网格类可以构建更复杂的表单。对于需要多列、不同宽度和其他对齐选项的窗体布局,请使用这些选项。需要启用$enable-grid-classes变量(默认为on)。

<div class="row">
        <div class="col">
        <input type="text" class="form-control" placeholder="First name" aria-label="First name">
        </div>
        <div class="col">
        <input type="text" class="form-control" placeholder="Last name" aria-label="Last name">
        </div>
        </div>

间隙

通过添加间隙修改器类,可以控制内联方向和块方向上的间隙宽度。还需要启用$enable-grid-classes Sass变量(默认为on)。

<div class="row g-3">
        <div class="col">
        <input type="text" class="form-control" placeholder="First name" aria-label="First name">
        </div>
        <div class="col">
        <input type="text" class="form-control" placeholder="Last name" aria-label="Last name">
        </div>
        </div>

也可以使用网格系统创建更复杂的布局。

<form class="row g-3">
        <div class="col-md-6">
        <label for="inputEmail4" class="form-label">Email</label>
        <input type="email" class="form-control" id="inputEmail4">
        </div>
        <div class="col-md-6">
        <label for="inputPassword4" class="form-label">Password</label>
        <input type="password" class="form-control" id="inputPassword4">
        </div>
        <div class="col-12">
        <label for="inputAddress" class="form-label">Address</label>
        <input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
        </div>
        <div class="col-12">
        <label for="inputAddress2" class="form-label">Address 2</label>
        <input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
        </div>
        <div class="col-md-6">
        <label for="inputCity" class="form-label">City</label>
        <input type="text" class="form-control" id="inputCity">
        </div>
        <div class="col-md-4">
        <label for="inputState" class="form-label">State</label>
        <select id="inputState" class="form-select">
        <option selected>Choose...</option>
        <option>...</option>
        </select>
        </div>
        <div class="col-md-2">
        <label for="inputZip" class="form-label">Zip</label>
        <input type="text" class="form-control" id="inputZip">
        </div>
        <div class="col-12">
        <div class="form-check">
        <input class="form-check-input" type="checkbox" id="gridCheck">
        <label class="form-check-label" for="gridCheck">
          Check me out
        </label>
        </div>
        </div>
        <div class="col-12">
        <button type="submit" class="btn btn-primary">Sign in</button>
        </div>
        </form>

水平表单

通过将.row类添加到窗体组,并使用.col-*-* 类指定标签和控件的宽度,使用网格创建水平窗体。确保将.col表单标签也添加到label中,以便它们与相关表单控件垂直居中。

有时,您可能需要使用margin或padding实用程序来创建所需的完美对齐。例如,我们移除了堆叠的单选按钮标签上的填充顶部,以便更好地对齐文本基线。

Radios
<form>
        <div class="row mb-3">
        <label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
        <div class="col-sm-10">
        <input type="email" class="form-control" id="inputEmail3">
        </div>
        </div>
        <div class="row mb-3">
        <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
        <div class="col-sm-10">
        <input type="password" class="form-control" id="inputPassword3">
        </div>
        </div>
        <fieldset class="row mb-3">
        <legend class="col-form-label col-sm-2 pt-0">Radios</legend>
        <div class="col-sm-10">
        <div class="form-check">
          <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked>
          <label class="form-check-label" for="gridRadios1">
            First radio
          </label>
        </div>
        <div class="form-check">
          <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
          <label class="form-check-label" for="gridRadios2">
            Second radio
          </label>
        </div>
        <div class="form-check disabled">
          <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled>
          <label class="form-check-label" for="gridRadios3">
            Third disabled radio
          </label>
        </div>
        </div>
        </fieldset>
        <div class="row mb-3">
        <div class="col-sm-10 offset-sm-2">
        <div class="form-check">
          <input class="form-check-input" type="checkbox" id="gridCheck1">
          <label class="form-check-label" for="gridCheck1">
            Example checkbox
          </label>
        </div>
        </div>
        </div>
        <button type="submit" class="btn btn-primary">Sign in</button>
        </form>

水平表单标签大小

请确保对code><label>或<legend>使用 .col-form-label-sm<legend>,以正确遵循.form-control-lg.form-control-sm的大小。

<div class="row mb-3">
        <label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
        <div class="col-sm-10">
        <input type="email" class="form-control form-control-sm" id="colFormLabelSm" placeholder="col-form-label-sm">
        </div>
        </div>
        <div class="row mb-3">
        <label for="colFormLabel" class="col-sm-2 col-form-label">Email</label>
        <div class="col-sm-10">
        <input type="email" class="form-control" id="colFormLabel" placeholder="col-form-label">
        </div>
        </div>
        <div class="row">
        <label for="colFormLabelLg" class="col-sm-2 col-form-label col-form-label-lg">Email</label>
        <div class="col-sm-10">
        <input type="email" class="form-control form-control-lg" id="colFormLabelLg" placeholder="col-form-label-lg">
        </div>
        </div>

列大小

如前面的示例所示,我们的网格系统允许您在一行中放置任意数量的.col。他们将平均分配可用宽度。您还可以选择列的一个子集来占用更多或更少的空间,而其余的.col会使用特定的列类(如.col-sm-7)来平均分割其余的列。

<div class="row g-3">
        <div class="col-sm-7">
        <input type="text" class="form-control" placeholder="City" aria-label="City">
        </div>
        <div class="col-sm">
        <input type="text" class="form-control" placeholder="State" aria-label="State">
        </div>
        <div class="col-sm">
        <input type="text" class="form-control" placeholder="Zip" aria-label="Zip">
        </div>
        </div>

自动调整大小

下面的示例使用弹性盒子实用类将内容垂直居中,并将.col更改为.col-auto,以便列只占用所需的空间。换句话说,列的大小是基于内容的。

@
<form class="row gy-2 gx-3 align-items-center">
        <div class="col-auto">
        <label class="visually-hidden" for="autoSizingInput">Name</label>
        <input type="text" class="form-control" id="autoSizingInput" placeholder="Jane Doe">
        </div>
        <div class="col-auto">
        <label class="visually-hidden" for="autoSizingInputGroup">Username</label>
        <div class="input-group">
        <div class="input-group-text">@</div>
        <input type="text" class="form-control" id="autoSizingInputGroup" placeholder="Username">
        </div>
        </div>
        <div class="col-auto">
        <label class="visually-hidden" for="autoSizingSelect">Preference</label>
        <select class="form-select" id="autoSizingSelect">
        <option selected>Choose...</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        </select>
        </div>
        <div class="col-auto">
        <div class="form-check">
        <input class="form-check-input" type="checkbox" id="autoSizingCheck">
        <label class="form-check-label" for="autoSizingCheck">
          Remember me
        </label>
        </div>
        </div>
        <div class="col-auto">
        <button type="submit" class="btn btn-primary">Submit</button>
        </div>
        </form>

然后您可以再次将其与特定于大小的列类混合。

@
<form class="row gx-3 gy-2 align-items-center">
        <div class="col-sm-3">
        <label class="visually-hidden" for="specificSizeInputName">Name</label>
        <input type="text" class="form-control" id="specificSizeInputName" placeholder="Jane Doe">
        </div>
        <div class="col-sm-3">
        <label class="visually-hidden" for="specificSizeInputGroupUsername">Username</label>
        <div class="input-group">
        <div class="input-group-text">@</div>
        <input type="text" class="form-control" id="specificSizeInputGroupUsername" placeholder="Username">
        </div>
        </div>
        <div class="col-sm-3">
        <label class="visually-hidden" for="specificSizeSelect">Preference</label>
        <select class="form-select" id="specificSizeSelect">
        <option selected>Choose...</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        </select>
        </div>
        <div class="col-auto">
        <div class="form-check">
        <input class="form-check-input" type="checkbox" id="autoSizingCheck2">
        <label class="form-check-label" for="autoSizingCheck2">
          Remember me
        </label>
        </div>
        </div>
        <div class="col-auto">
        <button type="submit" class="btn btn-primary">Submit</button>
        </div>
        </form>

行内表单

使用.row-cols-*类创建响应的水平布局。通过添加间隙修饰符类,我们将在水平和垂直方向上创建间隙。在狭窄的移动视口中,.col-12有助于堆叠窗体控件等。.align-items-center将表单元素与中间对齐,使.form-checkbox正确对齐。

@
<form class="row row-cols-lg-auto g-3 align-items-center">
        <div class="col-12">
        <label class="visually-hidden" for="inlineFormInputGroupUsername">Username</label>
        <div class="input-group">
        <div class="input-group-text">@</div>
        <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Username">
        </div>
        </div>
        
        <div class="col-12">
        <label class="visually-hidden" for="inlineFormSelectPref">Preference</label>
        <select class="form-select" id="inlineFormSelectPref">
        <option selected>Choose...</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        </select>
        </div>
        
        <div class="col-12">
        <div class="form-check">
        <input class="form-check-input" type="checkbox" id="inlineFormCheck">
        <label class="form-check-label" for="inlineFormCheck">
          Remember me
        </label>
        </div>
        </div>
        
        <div class="col-12">
        <button type="submit" class="btn btn-primary">Submit</button>
        </div>
        </form>
返回顶部