1. 表單概述
表單是 HTML 網頁互動的核心,也是 Bootstrap 用力最深的部分之一。它提供了基礎、內聯、橫向三種版面配置,加上完整的狀態樣式。
表單相關元素
input、textarea、select、checkbox、radio(後兩者其實是 input 的特殊 type)
其他標籤
form、fieldset、legend
<!--基礎表單的三個步驟: 1. 在父 <form> 上加上 role="form" 2. 把 label 與控制項放在帶 .form-group 的 <div> 裡,才能取得最佳間距 3. 所有 <input>、<textarea>、<select> 都加上 .form-control--> <form role="form"> <fieldset> <legend>用戶登錄</legend> <div class="form-group"> <label for="name">姓名</label> <input type="text" class="form-control" id="name" placeholder="請輸入名稱"> </div> <div class="form-group"> <label for="psd">密碼</label> <input type="text" class="form-control" id="psd" placeholder="請輸入密碼"> </div> <div class="checkbox"> <label><input type="checkbox">記住密碼</label> </div> <button type="submit" class="btn btn-default">登錄</button> </fieldset> </form>
需要所有元素並排、往左對齊時,在 <form> 上加 .form-inline 即可:
<form role="form" class="form-inline"> <fieldset> <legend>用戶登錄</legend> <div class="form-group"> <label for="name">姓名</label> <input type="text" class="form-control" id="name" placeholder="請輸入名稱"> </div> <div class="form-group"> <label for="psd">密碼</label> <input type="text" class="form-control" id="psd" placeholder="請輸入密碼"> </div> <div class="checkbox"> <label><input type="checkbox">記住密碼</label> </div> <button type="submit" class="btn btn-default">登錄</button> </fieldset> </form>
橫向表單需要三件事:form 上加 .form-horizontal、label 上加 .control-label、再用柵格類別(col-lg-*)來配置寬度比例。
<form role="form" class="form-horizontal"> <fieldset> <legend>用戶登錄</legend> <div class="form-group"> <label class="control-label col-lg-1" for="name">姓名</label> <div class="col-lg-10"> <input type="text" class="form-control" id="name" placeholder="請輸入名稱"> </div> </div> <div class="form-group"> <label class="control-label col-lg-1" for="psd">密碼</label> <div class="col-lg-10"> <input type="text" class="form-control" id="psd" placeholder="請輸入密碼"> </div> </div> </fieldset> </form>
input 元素:使用時一定要宣告 type,否則可能出現預期外的行為。
select 元素:多選加上 multiple="multiple"。
textarea 元素:rows 決定高度、cols 決定寬度;但一旦套上 .form-control,cols 就會失效,寬度改由容器決定。
checkbox 與 radio:注意每個 input 外層都要用 label 包住,最外層再用容器包好並套用 .checkbox 或 .radio 樣式。
//使用 <div class="checkbox"> <label><input type="checkbox">學習前端</label> </div> <div class="radio"> <label><input type="radio" name="optionsRadios" value="male">男生</label> </div> <div class="radio"> <label><input type="radio" name="optionsRadios" value="female">女生</label> </div>
//源碼 //讓單選框與複選框能左右上下置中 .radio, .checkbox { position: relative; display: block; margin-top: 10px; margin-bottom: 10px; } //內部有 label 時改為內距對齊 .radio label, .checkbox label { min-height: 20px; padding-left: 20px; margin-bottom: 0; font-weight: normal; cursor: pointer; }
若想讓選項並排顯示,改在 label 上加上 checkbox-inline 或 radio-inline 即可。
包含焦點狀態、禁用狀態與驗證提示狀態三類。
焦點狀態:input 取得 :focus 時,Bootstrap 會移除外框並改用 box-shadow 呈現光暈。
禁用狀態:在 <fieldset> 上加 disabled 屬性,即可一次停用裡面所有控制項。
驗證提示狀態:在父元素加上 .has-warning、.has-error 或 .has-success,文字、邊框與陰影的顏色就會一起變化。
<div class="form-group has-warning"> <label for="inputWarning" class="control-label">輸入長度不夠!</label> <input type="text" class="form-control"> </div> <div class="form-group has-error"> <label for="inputError" class="control-label">輸入不符合要求!</label> <input type="text" class="form-control"> </div> <div class="form-group has-success has-feedback"> <label for="inputSuccess" class="control-label">輸入文本符合要求!</label> <input type="text" class="form-control" id="inputSuccess"> <span class="glyphicon glyphicon-ok form-control-feedback"></span> </div>
//相對定位,作為 feedback 圖示的定位基準 .has-feedback { position: relative; } //右內距加大,為小圖示留出空間 .has-feedback .form-control { padding-right: 42.5px; } //圖示定位:絕對定位貼右,pointer-events:none 避免擋住點擊 .form-control-feedback { position: absolute; top: 0; right: 0; z-index: 2; display: block; width: 34px; height: 34px; line-height: 34px; text-align: center; pointer-events: none; } /* 大尺寸版本 */ .input-lg + .form-control-feedback, .input-group-lg + .form-control-feedback, .form-group-lg .form-control + .form-control-feedback { width: 46px; height: 46px; line-height: 46px; } /* 小尺寸版本 */ .input-sm + .form-control-feedback, .input-group-sm + .form-control-feedback, .form-group-sm .form-control + .form-control-feedback { width: 30px; height: 30px; line-height: 30px; } /* success 狀態配色 */ .has-success .help-block, .has-success .control-label, .has-success .radio, .has-success .checkbox, .has-success .radio-inline, .has-success .checkbox-inline, .has-success.radio label, .has-success.checkbox label, .has-success.radio-inline label, .has-success.checkbox-inline label { color: #3c763d; } .has-success .form-control { border-color: #3c763d; -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); } .has-success .form-control:focus { border-color: #2b542c; -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #67b168; box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #67b168; } .has-success .input-group-addon { color: #3c763d; background-color: #dff0d8; border-color: #3c763d; } .has-success .form-control-feedback { color: #3c763d; }
原始碼裡 pointer-events: none 這個細節很關鍵:feedback 圖示是疊在輸入框上的,若不關閉滑鼠事件,使用者點圖示位置就無法聚焦輸入框。
用 input-lg 或 input-sm 即可調整控制項高度與字級,feedback 圖示的尺寸也會跟著對應縮放。