您好,登錄后才能下訂單哦!
一、表單布局
Bootstrap 提供了下列類型的表單布局:
--垂直表單(默認(rèn))
--內(nèi)聯(lián)表單
--水平表單
(1)垂直或基本表單
基本的表單結(jié)構(gòu)是 Bootstrap 自帶的,個(gè)別的表單控件自動(dòng)接收一些全局樣式。
下面列出了創(chuàng)建基本表單的步驟:
--向父 <form> 元素添加 role="form"。
--把標(biāo)簽和控件放在一個(gè)帶有 class .form-group 的 <div> 中。這是獲取最佳間距所必需的。
--向所有的文本元素 <input>、<textarea> 和 <select> 添加 class .form-control。
<form role="form"> <div class="form-group"> <label for="name">名稱</label> <input type="text" class="form-control" id="name" placeholder="請(qǐng)輸入名稱"> </div> <div class="form-group"> <label for="inputfile">文件輸入</label> <input type="file" id="inputfile"> <p class="help-block">這里是塊級(jí)幫助文本的實(shí)例。</p> </div> <div class="checkbox"> <label> <input type="checkbox">請(qǐng)打勾 </label> </div> <button type="submit" class="btn btn-default">提交</button> </form>
(2)內(nèi)聯(lián)表單
如果需要?jiǎng)?chuàng)建一個(gè)表單,它的所有元素是內(nèi)聯(lián)的,向左對(duì)齊的,標(biāo)簽是并排的,請(qǐng)向 <form> 標(biāo)簽添加 class .form-inline。
<form class="form-inline" role="form"> <div class="form-group"> <label class="sr-only" for="name">名稱</label> <input type="text" class="form-control" id="name" placeholder="請(qǐng)輸入名稱"> </div> <div class="form-group"> <label class="sr-only" for="inputfile">文件輸入</label> <input type="file" id="inputfile"> </div> <div class="checkbox"> <label> <input type="checkbox">請(qǐng)打勾 </label> </div> <button type="submit" class="btn btn-default">提交</button> </form>
默認(rèn)情況下,Bootstrap 中的 input、select 和 textarea 有 100% 寬度。在使用內(nèi)聯(lián)表單時(shí),您需要在表單控件上設(shè)置一個(gè)寬度。
注:使用 class .sr-only,您可以隱藏內(nèi)聯(lián)表單的標(biāo)簽。
(3)水平表單
水平表單與其他表單不僅標(biāo)記的數(shù)量上不同,而且表單的呈現(xiàn)形式也不同。如需創(chuàng)建一個(gè)水平布局的表單,請(qǐng)按下面的幾個(gè)步驟進(jìn)行:
--向父 <form> 元素添加 class .form-horizontal。
--把標(biāo)簽和控件放在一個(gè)帶有 class .form-group 的 <div> 中。
--向標(biāo)簽添加 class .control-label。
<form class="form-horizontal" role="form"> <div class="form-group"> <label for="firstname" class="col-sm-2 control-label">名字</label> <div class="col-sm-10"> <input type="text" class="form-control" id="firstname" placeholder="請(qǐng)輸入名字"> </div> </div> <div class="form-group"> <label for="lastname" class="col-sm-2 control-label">姓</label> <div class="col-sm-10"> <input type="text" class="form-control" id="lastname" placeholder="請(qǐng)輸入姓"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <div class="checkbox"> <label> <input type="checkbox">請(qǐng)記住我 </label> </div> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">登錄</button> </div> </div> </form>
二、支持的表單控件
Bootstrap 支持最常見的表單控件,主要是 input、textarea、checkbox、radio 和 select。
(1)輸入框
Bootstrap 提供了對(duì)所有原生的 HTML5 的 input 類型的支持,包括:text、password、datetime、datetime-local、date、month、time、week、number、email、url、search、tel 和 color。適當(dāng)?shù)?type 聲明是必需的,這樣才能讓 input 獲得完整的樣式。
(2)文本框
(3)復(fù)選框和單選框
對(duì)一系列復(fù)選框和單選框使用 .checkbox-inline 或 .radio-inline class,控制它們顯示在同一行上。
(4)選擇框
使用 multiple="multiple" 允許用戶選擇多個(gè)選項(xiàng)。
三、靜態(tài)控件
當(dāng)您需要在一個(gè)水平表單內(nèi)的表單標(biāo)簽后放置純文本時(shí),請(qǐng)?jiān)?<p> 上使用 class .form-control-static。
四、表單控件狀態(tài)
除了 :focus 狀態(tài)外,Bootstrap 還為禁用的輸入框定義了樣式,并提供了表單驗(yàn)證的 class。
(1)輸入框焦點(diǎn)
當(dāng)輸入框 input 接收到 :focus 時(shí),輸入框的輪廓會(huì)被移除,同時(shí)應(yīng)用 box-shadow。
(2)禁用的輸入框 input
如果您想要禁用一個(gè)輸入框 input,只需要簡(jiǎn)單地添加 disabled 屬性,這不僅會(huì)禁用輸入框,還會(huì)改變輸入框的樣式以及當(dāng)鼠標(biāo)的指針懸停在元素上時(shí)鼠標(biāo)指針的樣式。
(3)禁用的字段集 fieldset
對(duì) <fieldset> 添加 disabled 屬性來禁用 <fieldset> 內(nèi)的所有控件。
(4)驗(yàn)證狀態(tài)
Bootstrap 包含了錯(cuò)誤、警告和成功消息的驗(yàn)證樣式。只需要對(duì)父元素簡(jiǎn)單地添加適當(dāng)?shù)?class(.has-warning、 .has-error 或 .has-success)即可使用驗(yàn)證狀態(tài)。
<form class="form-horizontal" role="form"> <div class="form-group"> <label class="col-sm-2 control-label">聚焦</label> <div class="col-sm-10"> <input class="form-control" id="focusedInput" type="text" value="該輸入框獲得焦點(diǎn)..."> </div> </div> <div class="form-group"> <label for="inputPassword" class="col-sm-2 control-label">禁用</label> <div class="col-sm-10"> <input class="form-control" id="disabledInput" type="text" placeholder="該輸入框禁止輸入..." disabled> </div> </div> <fieldset disabled> <div class="form-group"> <label for="disabledTextInput" class="col-sm-2 control-label">禁用輸入(Fieldset disabled)</label> <div class="col-sm-10"> <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止輸入"> </div> </div> <div class="form-group"> <label for="disabledSelect" class="col-sm-2 control-label">禁用選擇菜單(Fieldset disabled)</label> <div class="col-sm-10"> <select id="disabledSelect" class="form-control"> <option>禁止選擇</option> </select> </div> </div> </fieldset> <div class="form-group has-success"> <label class="col-sm-2 control-label" for="inputSuccess">輸入成功</label> <div class="col-sm-10"> <input type="text" class="form-control" id="inputSuccess"> </div> </div> <div class="form-group has-warning"> <label class="col-sm-2 control-label" for="inputWarning">輸入警告</label> <div class="col-sm-10"> <input type="text" class="form-control" id="inputWarning"> </div> </div> <div class="form-group has-error"> <label class="col-sm-2 control-label" for="inputError">輸入錯(cuò)誤</label> <div class="col-sm-10"> <input type="text" class="form-control" id="inputError"> </div> </div> </form>
五、表單控件大小
您可以分別使用 class .input-lg 和 .col-lg-* 來設(shè)置表單的高度和寬度。
<form role="form"> <div class="form-group"> <input class="form-control input-lg" type="text" placeholder=".input-lg"> </div> <div class="form-group"> <input class="form-control" type="text" placeholder="默認(rèn)輸入"> </div> <div class="form-group"> <input class="form-control input-sm" type="text" placeholder=".input-sm"> </div> <div class="form-group"></div> <div class="form-group"> <select class="form-control input-lg"> <option value="">.input-lg</option> </select> </div> <div class="form-group"> <select class="form-control"> <option value="">默認(rèn)選擇</option> </select> </div> <div class="form-group"> <select class="form-control input-sm"> <option value="">.input-sm</option> </select> </div> <div class="row"> <div class="col-lg-2"> <input type="text" class="form-control" placeholder=".col-lg-2"> </div> <div class="col-lg-3"> <input type="text" class="form-control" placeholder=".col-lg-3"> </div> <div class="col-lg-4"> <input type="text" class="form-control" placeholder=".col-lg-4"> </div> </div> </form>
六、表單幫助文本
Bootstrap 表單控件可以在輸入框 input 上有一個(gè)塊級(jí)幫助文本。為了添加一個(gè)占用整個(gè)寬度的內(nèi)容塊,請(qǐng)?jiān)?<input> 后使用 .help-block。
<form role="form"> <span>幫助文本實(shí)例</span> <input class="form-control" type="text" placeholder=""> <span class="help-block">一個(gè)較長(zhǎng)的幫助文本塊,超過一行, 需要擴(kuò)展到下一行。本實(shí)例中的幫助文本總共有兩行。</span> </form>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。