浮動標簽(Floating labels)
創(chuàng)建能夠漂浮在輸入字段上的,漂亮簡單的表單標簽。
On this page
例子
在.form-floating
中包裝一對<input class="form-control">
和<label>
元素,以啟用帶有Bootstrap文本表單字段的浮動標簽。每個input上都需要一個占位符,因為我們的CSS-only浮動標簽方法使用placeholder
偽元素。還要注意,<input>
必須放在第一位,這樣我們就可以使用同級選擇器(例如,~)。
<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>
如果已經(jīng)定義了值,label將自動調整到浮動位置。
<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 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>
文本域
默認情況下,帶.form-control
的textarea控件的高度將與input相同。
<div class="form-floating">
<textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea"></textarea>
<label for="floatingTextarea">Comments</label>
</div>
要在textarea上設置自定義高度,請不要使用rows屬性。相反,設置一個顯式高度(內(nèi)聯(lián)或通過自定義CSS)。
<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>
選擇
除.form控件外,浮動標簽僅在.form selects上可用。它們的工作方式相同,但與input不同,它們總是以浮動狀態(tài)顯示label。不支持帶大小和多選屬性的選擇。
<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>
布局
使用Bootstrap網(wǎng)格系統(tǒng)時,請確保將窗體元素放置在列中。
<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" 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="floatingSelectGrid">Works with selects</label>
</div>
</div>
</div>
Sass
Variables
$form-floating-height: add(3.5rem, $input-height-border);
$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-opacity: .65;
$form-floating-label-transform: scale(.85) translateY(-.5rem) translateX(.15rem);
$form-floating-transition: opacity .1s ease-in-out, transform .1s ease-in-out;