Skip to main content Skip to docs navigation

浮动标签

创建精美简单的表单标签,浮动在你的输入字段上。

示例

🌐 Example

将一对 <input class="form-control"><label> 元素封装在 .form-floating 中,以在 Bootstrap 的文本表单字段中启用浮动标签。

🌐 Wrap a pair of <input class="form-control"> and <label> elements in .form-floating to enable floating labels with Bootstrap’s textual form fields.

每个 <input> 上都需要一个非空的 placeholder 属性,因为我们的纯 CSS 浮动标签实现依赖 :placeholder-shown 伪元素来检测输入是否为空。占位符文本本身不可见;用户只会看到 <label>

🌐 A non-empty placeholder attribute is required on each <input> as our CSS-only floating label implementation relies on the :placeholder-shown pseudo-element to detect when the input is empty. The placeholder text itself is not visible; only the <label> is shown to users.

还要注意,<input> 必须先出现,这样我们才能使用兄弟选择器(即 ~)。

🌐 Also note that the <input> must come first so we can utilize a sibling selector (i.e., ~).

html
<div class="form-floating mb-3">
  <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
  <label for="floatingInput">Email address</label>
</div>
<div class="form-floating">
  <input type="password" class="form-control" id="floatingPassword" placeholder="Password">
  <label for="floatingPassword">Password</label>
</div>

当已经定义了 value 时,<label> 会自动调整到它们的浮动位置。

🌐 When there’s a value already defined, <label>s will automatically adjust to their floated position.

html
<form class="form-floating">
  <input type="email" class="form-control" id="floatingInputValue" placeholder="name@example.com" value="test@example.com">
  <label for="floatingInputValue">Input with value</label>
</form>

表单验证样式也按预期工作。

🌐 Form validation styles also work as expected.

html
<form class="form-floating">
  <input type="email" class="form-control is-invalid" id="floatingInputInvalid" placeholder="name@example.com" value="test@example.com">
  <label for="floatingInputInvalid">Invalid input</label>
</form>

文本区域

🌐 Textareas

默认情况下,带有 .form-control<textarea> 将与 <input> 高度相同。

🌐 By default, <textarea>s with .form-control will be the same height as <input>s.

html
<div class="form-floating">
  <textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea"></textarea>
  <label for="floatingTextarea">Comments</label>
</div>

要在你的 <textarea> 上设置自定义高度,请不要使用 rows 属性。相反,应设置明确的 height(可以是内联或通过自定义 CSS)。

🌐 To set a custom height on your <textarea>, do not use the rows attribute. Instead, set an explicit height (either inline or via custom CSS).

html
<div class="form-floating">
  <textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea2" style="height: 100px"></textarea>
  <label for="floatingTextarea2">Comments</label>
</div>

选择

🌐 Selects

除了 .form-control,浮动标签仅适用于 .form-select。它们的工作方式相同,但与 <input> 不同,它们将始终以浮动状态显示 <label>不支持带 sizemultiple 的选择控件。

🌐 Other than .form-control, floating labels are only available on .form-selects. They work in the same way, but unlike <input>s, they’ll always show the <label> in its floated state. Selects with size and multiple are not supported.

html
<div class="form-floating">
  <select class="form-select" id="floatingSelect" aria-label="Floating label select example">
    <option selected>Open this select menu</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
  <label for="floatingSelect">Works with selects</label>
</div>

禁用

🌐 Disabled

在 input、textarea 或 select 上添加 disabled 布尔属性,以使其显示灰色外观,移除指针事件,并防止获得焦点。

🌐 Add the disabled boolean attribute on an input, a textarea or a select to give it a grayed out appearance, remove pointer events, and prevent focusing.

html
<div class="form-floating mb-3">
  <input type="email" class="form-control" id="floatingInputDisabled" placeholder="name@example.com" disabled>
  <label for="floatingInputDisabled">Email address</label>
</div>
<div class="form-floating mb-3">
  <textarea class="form-control" placeholder="Leave a comment here" id="floatingTextareaDisabled" disabled></textarea>
  <label for="floatingTextareaDisabled">Comments</label>
</div>
<div class="form-floating mb-3">
  <textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea2Disabled" style="height: 100px" disabled>Disabled textarea with some text inside</textarea>
  <label for="floatingTextarea2Disabled">Comments</label>
</div>
<div class="form-floating">
  <select class="form-select" id="floatingSelectDisabled" aria-label="Floating label disabled select example" disabled>
    <option selected>Open this select menu</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
  <label for="floatingSelectDisabled">Works with selects</label>
</div>

只读明文

🌐 Readonly plaintext

浮动标签还支持 .form-control-plaintext,这对于将可编辑的 <input> 切换为纯文本值而不影响页面布局可能很有帮助。

🌐 Floating labels also support .form-control-plaintext, which can be helpful for toggling from an editable <input> to a plaintext value without affecting the page layout.

html
<div class="form-floating mb-3">
  <input type="email" readonly class="form-control-plaintext" id="floatingEmptyPlaintextInput" placeholder="name@example.com">
  <label for="floatingEmptyPlaintextInput">Empty input</label>
</div>
<div class="form-floating mb-3">
  <input type="email" readonly class="form-control-plaintext" id="floatingPlaintextInput" placeholder="name@example.com" value="name@example.com">
  <label for="floatingPlaintextInput">Input with value</label>
</div>

输入组

🌐 Input groups

浮动标签也支持 .input-group

🌐 Floating labels also support .input-group.

@
html
<div class="input-group mb-3">
  <span class="input-group-text">@</span>
  <div class="form-floating">
    <input type="text" class="form-control" id="floatingInputGroup1" placeholder="Username">
    <label for="floatingInputGroup1">Username</label>
  </div>
</div>

在将 .input-group.form-floating 与表单验证一起使用时,-feedback 应放在 .form-floating 外部,但在 .input-group 内部。这意味着反馈需要通过 JavaScript 显示。

🌐 When using .input-group and .form-floating along with form validation, the -feedback should be placed outside of the .form-floating, but inside of the .input-group. This means that the feedback will need to be shown using JavaScript.

@
Please choose a username.
html
<div class="input-group has-validation">
  <span class="input-group-text">@</span>
  <div class="form-floating is-invalid">
    <input type="text" class="form-control is-invalid" id="floatingInputGroup2" placeholder="Username" required>
    <label for="floatingInputGroup2">Username</label>
  </div>
  <div class="invalid-feedback">
    Please choose a username.
  </div>
</div>

布局

🌐 Layout

使用 Bootstrap 网格系统时,请确保将表单元素放置在列类中。

🌐 When working with the Bootstrap grid system, be sure to place form elements within column classes.

html
<div class="row g-2">
  <div class="col-md">
    <div class="form-floating">
      <input type="email" class="form-control" id="floatingInputGrid" placeholder="name@example.com" value="mdo@example.com">
      <label for="floatingInputGrid">Email address</label>
    </div>
  </div>
  <div class="col-md">
    <div class="form-floating">
      <select class="form-select" id="floatingSelectGrid">
        <option selected>Open this select menu</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
      <label for="floatingSelectGrid">Works with selects</label>
    </div>
  </div>
</div>

CSS

Sass 变量

🌐 Sass variables

$form-floating-height:                  add(3.5rem, $input-height-border);
$form-floating-line-height:             1.25;
$form-floating-padding-x:               $input-padding-x;
$form-floating-padding-y:               1rem;
$form-floating-input-padding-t:         1.625rem;
$form-floating-input-padding-b:         .625rem;
$form-floating-label-height:            1.5em;
$form-floating-label-opacity:           .65;
$form-floating-label-transform:         scale(.85) translateY(-.5rem) translateX(.15rem);
$form-floating-label-disabled-color:    $gray-600;
$form-floating-transition:              opacity .1s ease-in-out, transform .1s ease-in-out;