跳至主要內容 跳至文件導覽

浮動標籤(Floating labels)

建立簡潔美觀的表單標籤,懸浮於輸入欄位上方。

範例(Example)

將一對 <input class="form-control"><label> 元素包裝在 .form-floating 中,即可在 Bootstrap 的文字表單欄位上啟用浮動標籤。

每個 <input> 都需要一個非空的 placeholder 屬性,因為我們純 CSS 的浮動標籤實作依賴 :placeholder-shown 偽元素來偵測輸入欄位何時為空。placeholder 文字本身不會顯示;使用者只會看到 <label>

另請注意,<input> 必須放在前面,這樣我們才能使用同層選擇器(即 ~)。

html
<div class="form-floating mb-3">
  <input type="email" class="form-control" id="floatingInput" placeholder="[email protected]">
  <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> 將自動調整到浮動位置。

html
<form class="form-floating">
  <input type="email" class="form-control" id="floatingInputValue" placeholder="[email protected]" value="[email protected]">
  <label for="floatingInputValue">Input with value</label>
</form>

表單驗證樣式也能如預期般運作。

html
<form class="form-floating">
  <input type="email" class="form-control is-invalid" id="floatingInputInvalid" placeholder="[email protected]" value="[email protected]">
  <label for="floatingInputInvalid">Invalid input</label>
</form>

文字區域(Textareas)

預設情況下,帶有 .form-control<textarea> 將與 <input> 具有相同的高度。

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)。

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 的選擇器。

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)

在輸入欄位、文字區域或選擇器上加入 disabled 布林屬性,使其呈現灰色外觀、移除指標事件並防止聚焦。

html
<div class="form-floating mb-3">
  <input type="email" class="form-control" id="floatingInputDisabled" placeholder="[email protected]" 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> 和純文字值之間切換而不影響頁面版面配置很有幫助。

html
<div class="form-floating mb-3">
  <input type="email" readonly class="form-control-plaintext" id="floatingEmptyPlaintextInput" placeholder="[email protected]">
  <label for="floatingEmptyPlaintextInput">Empty input</label>
</div>
<div class="form-floating mb-3">
  <input type="email" readonly class="form-control-plaintext" id="floatingPlaintextInput" placeholder="[email protected]" value="[email protected]">
  <label for="floatingPlaintextInput">Input with value</label>
</div>

輸入群組(Input groups)

浮動標籤也支援 .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 來顯示。

@
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 格線系統時,請務必將表單元素放置在欄類別中。

html
<div class="row g-2">
  <div class="col-md">
    <div class="form-floating">
      <input type="email" class="form-control" id="floatingInputGrid" placeholder="[email protected]" value="[email protected]">
      <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;