跳到主要内容
输入关键词开始搜索,↑ ↓ 选择,Enter 打开
没有找到匹配的文档

浮动标签

创建美观简洁的表单标签,悬浮在输入字段上方。

层级:forms

示例

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

非空 placeholder 每个都需要属性 <input> 由于我们的仅 CSS 悬浮标签实现依赖于 :placeholder-shown 伪元素用于检测输入是否为空。当控件未获得焦点时,占位符保持透明,然后使用正常的占位符颜色。只有 <label> 当字段为空且未获得焦点时显示。

还要注意,<label> 必须首先出现,以便屏幕阅读器在字段值之前进行朗读;我们的 CSS 使用 :has() 选择器响应控件的状态。

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

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

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

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

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

多行文本框

默认情况下, <textarea>与 .form-control 将和……一样高 <input>s.

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

设置自定义高度到你的 <textarea>,不要使用该 rows 属性。相反,重写该 --bs-form-floating-height CSS 变量在 .form-floating 包装器(可以是内联或通过自定义 CSS)以便标签在任何高度下都能正确浮动。

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

选择

选择元素的工作方式相同,但与<input>不同,它们总是会显示处于浮动状态的<label>。不支持带有size和multiple的选择元素。

HTML
<div class="form-floating">
  <label for="floatingSelect">Works with selects</label>
  <select class="form-control" 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>
</div>

日期和时间

日期、时间、日期时间-本地、月份和周输入在其浮动状态下也总是显示 <label>。这些控件不像文本输入那样支持占位符,因此不需要 placeholder 属性(大多数浏览器对这些类型会忽略该属性)。

HTML
<div class="form-floating">
  <label for="floatingDate">Date</label>
  <input type="date" class="form-control" id="floatingDate">
</div>
<div class="form-floating">
  <label for="floatingTime">Time</label>
  <input type="time" class="form-control" id="floatingTime">
</div>

残疾

在输入框、文本区或选择框上添加disabled布尔属性,以使其呈灰显外观,移除指针事件,并防止获得焦点。

HTML
<div class="form-floating">
  <label for="floatingInputDisabled">Email address</label>
  <input type="email" class="form-control" id="floatingInputDisabled" placeholder="name@example.com" disabled>
</div>
<div class="form-floating">
  <label for="floatingTextareaDisabled">Comments</label>
  <textarea class="form-control" placeholder="Leave a comment here" id="floatingTextareaDisabled" disabled></textarea>
</div>
<div class="form-floating" style="--bs-form-floating-height: 100px">
  <label for="floatingTextarea2Disabled">Comments</label>
  <textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea2Disabled" disabled>Disabled textarea with some text inside</textarea>
</div>
<div class="form-floating">
  <label for="floatingSelectDisabled">Works with selects</label>
  <select class="form-control" 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>
</div>

只读纯文本

浮动标签也支持.form-control-plaintext,这对于在不影响页面布局的情况下从可编辑的<input>切换到纯文本值非常有用。

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

输入组

浮动标签也支持.input-group。

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

使用时 .input-group 和 .form-floating 以及表单验证,放置 -feedback 在……外面 .input-group 在……里面 .form-field 包装器。

@
请选择一个用户名。
HTML
<div class="form-field">
  <div class="input-group">
    <span class="input-group-text">@</span>
    <div class="form-floating is-invalid">
      <label for="floatingInputGroup2">Username</label>
      <input type="text" class="form-control is-invalid" id="floatingInputGroup2" placeholder="Username" required>
    </div>
  </div>
  <div class="invalid-feedback">
    Please choose a username.
  </div>
</div>

始终浮动

添加 .form-floating-always 保持标签浮动并显示占位符。在...上使用它 .form-floating 它本身或作为其中一个的包装器。

HTML
<div class="form-floating form-floating-always">
  <label for="alwaysFloatingInput">Email address</label>
  <input type="email" class="form-control" id="alwaysFloatingInput" placeholder="name@example.com">
</div>
<div class="form-floating form-floating-always">
  <label for="alwaysFloatingPassword">Password</label>
  <input type="password" class="form-control" id="alwaysFloatingPassword" placeholder="••••••••">
</div>

默认情况下,占位符在控件未聚焦时保持透明。使用 .form-floating-always,占位符在两种状态下都会保持可见。

布局

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

HTML
<div class="row g-2">
  <div class="md:col">
    <div class="form-floating">
      <label for="floatingInputGrid">Email address</label>
      <input type="email" class="form-control" id="floatingInputGrid" placeholder="name@example.com" value="mdo@example.com">
    </div>
  </div>
  <div class="md:col">
    <div class="form-floating">
      <label for="floatingSelectGrid">Works with selects</label>
      <select class="form-control" 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>
    </div>
  </div>
</div>

CSS

变量

浮动标签在.form-floating上使用本地 CSS 变量进行实时自定义。CSS 变量的值是从每个组件独特的 Sass map生成的,并应用到前述类上。

// stylelint-disable-next-line scss/dollar-variable-default
$form-floating-tokens: defaults(
  (
    --form-floating-height: calc(3.75rem + (var(--border-width) * 2)),
    --form-floating-line-height: 1.25,
    --form-floating-padding-x: calc(var(--btn-input-padding-x) * 1.25),
    --form-floating-padding-y: 1rem,
    --form-floating-input-padding-t: 1.625rem,
    --form-floating-input-padding-b: .625rem,
    --form-floating-label-height: 1.5em,
    // Backgrounds for the textarea label's masking pseudo-element. Mirrors
    // `.form-control` here because the label is a sibling of the control, so it
    // can't inherit the control's own `--control-bg`/`--control-disabled-bg`.
    --form-floating-label-bg: var(--btn-input-bg),
    --form-floating-label-disabled-bg: var(--bg-2),
    --form-floating-label-opacity: .65,
    --form-floating-label-transform: scale(.85) translateY(-.5rem) translateX(.15rem),
    --form-floating-label-disabled-color: var(--fg-3),
    --form-floating-transition-property: "opacity, transform",
    --form-floating-transition-duration: .1s,
    --form-floating-transition-timing: ease-in-out,
  ),
  $form-floating-tokens
);