全面解析Bootstrap表單樣式的使用
本文主要給大家介紹了bootstrap表單樣式的使用知識(shí),非常不錯(cuò),一起看看吧!
表單
<form role="form"> <div class="form-group"> <label for="exampleInputEmail1">郵箱:</label> <input type="email" class="form-control" id="exampleInputEmail1" placeholder="請(qǐng)輸入您的郵箱地址"> </div> <div class="form-group"> <label for="exampleInputPassword1">密碼</label> <input type="password" class="form-control" id="exampleInputPassword1" placeholder="請(qǐng)輸入您的郵箱密碼"> </div> <div class="checkbox"> <label> <input type="checkbox"> 記住密碼 </label> </div> <button type="submit" class="btn btn-default">進(jìn)入郵箱</button> </form>
效果圖:
class: form-control
1、寬度變成了100%,2、設(shè)置了一個(gè)淺灰色(#ccc)的邊框,3、具有4px的圓角,4、設(shè)置陰影效果,并且元素得到焦點(diǎn)之時(shí),陰影和邊框效果會(huì)有所變化,5、設(shè)置了placeholder的顏色為#999
2、水平表單:
Bootstrap框架默認(rèn)的表單是垂直顯示風(fēng)格,但很多時(shí)候我們需要的水平表單風(fēng)格(標(biāo)簽居左,表單控件居右)
<form class="form-horizontal" role="form"> <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">郵箱</label> <div class="col-sm-10"> <input type="email" class="form-control" id="inputEmail3" placeholder="請(qǐng)輸入您的郵箱地址"> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">密碼</label> <div class="col-sm-10"> <input type="password" class="form-control" id="inputPassword3" 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"> 記住密碼 </label> </div> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">進(jìn)入郵箱</button> </div> </div> </form>
效果圖:
在Bootstrap框架中要實(shí)現(xiàn)水平表單效果,必須滿足以下兩個(gè)條件:
1、在元素是使用類名“form-horizontal”。
2、配合Bootstrap框架的網(wǎng)格系統(tǒng)。
在元素上使用類名“form-horizontal”主要有以下幾個(gè)作用:
- 設(shè)置表單控件padding和margin值。
- 改變“form-group”的表現(xiàn)形式,類似于網(wǎng)格系統(tǒng)的“row”。
3、水平表單:
在Bootstrap框架中實(shí)現(xiàn)這樣的表單效果是輕而易舉的,你只需要在元素中添加類名“form-inline”即可。
內(nèi)聯(lián)表單實(shí)現(xiàn)原理非常簡(jiǎn)單,欲將表單控件在一行顯示,就需要將表單控件設(shè)置成內(nèi)聯(lián)塊元素(display:inline-block)。
<form class="form-inline" role="form"> <div class="form-group"> <label class="sr-only" for="exampleInputEmail2">郵箱</label> <input type="email" class="form-control" id="exampleInputEmail2" placeholder="請(qǐng)輸入你的郵箱地址"> </div> <div class="form-group"> <label class="sr-only" for="exampleInputPassword2">密碼</label> <input type="password" class="form-control" id="exampleInputPassword2" placeholder="請(qǐng)輸入你的郵箱密碼"> </div> <div class="checkbox"> <label> <input type="checkbox"> 記住密碼 </label> </div> <button type="submit" class="btn btn-default">進(jìn)入郵箱</button> </form>
效果圖:
4、輸入框input
單行輸入框,常見(jiàn)的文本輸入框,也就是input的type屬性值為text。在Bootstrap中使用input時(shí)也必須添加type類型,如果沒(méi)有指定type類型,將無(wú)法得到正確的樣式.
<form role="form"> <div class="form-group"> <input type="email" class="form-control" placeholder="Enter email"> </div> <div class="form-group"> <input type="text" class="form-control" placeholder="請(qǐng)輸入用戶名"/> </div> </form>
效果圖:
5、下拉選擇框select
Bootstrap框架中的下拉選擇框使用和原始的一致,多行選擇設(shè)置multiple屬性的值為multiple。Bootstrap框架會(huì)為這些元素提供統(tǒng)一的樣式風(fēng)格。
<form role="form"> <div class="form-group"> <select class="form-control"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </div> <div class="form-group"> <select multiple class="form-control"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </div> </form>
效果圖:
6、文本域textarea
文本域和原始使用方法一樣,設(shè)置rows可定義其高度,設(shè)置cols可以設(shè)置其寬度。但如果textarea元素中添加了類名“form-control”類名,則無(wú)需設(shè)置cols屬性。因?yàn)锽ootstrap框架中的“form-control”樣式的表單控件寬度為100%或auto。
<form role="form"> <div class="form-group"> <textarea class="form-control" rows="3"></textarea> </div> </form>
效果圖:
7.復(fù)選框checkbox和單選擇按鈕radio
Bootstrap框架中checkbox和radio有點(diǎn)特殊,Bootstrap針對(duì)他們做了一些特殊化處理,主要是checkbox和radio與label標(biāo)簽配合使用會(huì)出現(xiàn)一些小問(wèn)題(最頭痛的是對(duì)齊問(wèn)題)。使用Bootstrap框架,開(kāi)發(fā)人員無(wú)需考慮太多,只需要按照下面的方法使用即可。
<form role="form"> <h3>案例1</h3> <div class="checkbox"> <label> <input type="checkbox" value=""> 記住密碼 </label> </div> <div class="radio"> <label> <input type="radio" name="optionsRadios" id="optionsRadios1" value="love" checked> 喜歡 </label> </div> <div class="radio"> <label> <input type="radio" name="optionsRadios" id="optionsRadios2" value="hate"> 不喜歡 </label> </div> </form>
效果圖:
8.復(fù)選框和單選按鈕水平排列
有時(shí)候,為了布局的需要,將復(fù)選框和單選按鈕需要水平排列。Bootstrap框架也做了這方面的考慮:
1、如果checkbox需要水平排列,只需要在label標(biāo)簽上添加類名“checkbox-inline”
2、如果radio需要水平排列,只需要在label標(biāo)簽上添加類名“radio-inline”
<form role="form"> <div class="form-group"> <label class="checkbox-inline"> <input type="checkbox" value="option1">游戲 </label> <label class="checkbox-inline"> <input type="checkbox" value="option2">攝影 </label> <label class="checkbox-inline"> <input type="checkbox" value="option3">旅游 </label> </div> <div class="form-group"> <label class="radio-inline"> <input type="radio" value="option1" name="sex">男性 </label> <label class="radio-inline"> <input type="radio" value="option2" name="sex">女性 </label> <label class="radio-inline"> <input type="radio" value="option3" name="sex">中性 </label> </div> </form>
效果圖:
9.按鈕
在Bootstrap框架中的按鈕都是采用來(lái)實(shí)現(xiàn)
<table class="table table-bordered table-striped"> <thead> <tr> <th>Button</th> <th>class=""</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><button class="btn" href="#">Default</button></td> <td><code>btn</code></td> <td>Standard gray button with gradient</td> </tr> <tr> <td><button class="btn btn-primary" href="#">Primary</button></td> <td><code>btn btn-primary</code></td> <td>Provides extra visual weight and identifies the primary action in a set of buttons</td> </tr> <tr> <td><button class="btn btn-info" href="#">Info</button></td> <td><code>btn btn-info</code></td> <td>Used as an alternative to the default styles</td> </tr> <tr> <td><button class="btn btn-success" href="#">Success</button></td> <td><code>btn btn-success</code></td> <td>Indicates a successful or positive action</td> </tr> <tr> <td><button class="btn btn-warning" href="#">Warning</button></td> <td><code>btn btn-warning</code></td> <td>Indicates caution should be taken with this action</td> </tr> <tr> <td><button class="btn btn-danger" href="#">Danger</button></td> <td><code>btn btn-danger</code></td> <td>Indicates a dangerous or potentially negative action</td> </tr> <tr> <td><button class="btn btn-inverse" href="#">Inverse</button></td> <td><code>btn btn-inverse</code></td> <td>Alternate dark gray button, not tied to a semantic action or use</td> </tr> </tbody> </table>
效果圖:
10.表單控件大小
前面看到的表單控件都正常的大小??梢酝ㄟ^(guò)設(shè)置控件的height,line-height,padding和font-size等屬性來(lái)實(shí)現(xiàn)控件的高度設(shè)置。不過(guò)Bootstrap框架還提供了兩個(gè)不同的類名,用來(lái)控制表單控件的高度。這兩個(gè)類名是:
1、input-sm:讓控件比正常大小更小
2、input-lg:讓控件比正常大小更大
這兩個(gè)類適用于表單中的input,textarea和select控件.
<form role="form"> <div class="form-group"> <label class="control-label">控件變大</label> <input class="form-control input-lg" type="text" placeholder="添加.input-lg,控件變大"> </div> <div class="form-group"> <label class="control-label">正常大小</label> <input class="form-control" type="text" placeholder="正常大小"> </div> <div class="form-group"> <label class="control-label">控件變小</label> <input class="form-control input-sm" type="text" placeholder="添加.input-sm,控件變小"> </div> </form>
效果圖:
11.表單控件狀態(tài)(禁用狀態(tài))
Bootstrap框架的表單控件的禁用狀態(tài)和普通的表單禁用狀態(tài)實(shí)現(xiàn)方法是一樣的,在相應(yīng)的表單控件上添加屬性“disabled”。具體的看下面的代碼
禁用單個(gè)表單標(biāo)簽,只需要在標(biāo)簽的屬性中加入disabled
<form role="form" class="form-horizontal"> <div class="form-group"> <div class="col-xs-6"> <input class="form-control input-lg" id="disabledInput" type="text" placeholder="表單已被禁用,不可輸入" disabled> </div> </div> </form>
效果圖:
在Bootstrap框架中,如果fieldset設(shè)置了disabled屬性,整個(gè)域都將處于被禁用狀態(tài)。
<form role="form"> <fieldset disabled> <div class="form-group"> <label for="disabledTextInput">禁用的輸入框</label> <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止輸入"> </div> <div class="form-group"> <label for="disabledSelect">禁用的下拉框</label> <select id="disabledSelect" class="form-control"> <option>不可選擇</option> </select> </div> <div class="checkbox"> <label> <input type="checkbox"> 無(wú)法選擇 </label> </div> <button type="submit" class="btn btn-primary">提交</button> </fieldset> </form>
效果圖:
據(jù)說(shuō)對(duì)于整個(gè)禁用的域中,如果legend中有輸入框的話,這個(gè)輸入框是無(wú)法被禁用的。
form role="form"> <fieldset disabled> <legend><input type="text" class="form-control" placeholder="顯然我顏色變灰了,但是我沒(méi)被禁用,不信?單擊試一下" /></legend> <div class="form-group"> <label for="disabledTextInput">禁用的輸入框</label> <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止輸入"> </div> <div class="form-group"> <label for="disabledSelect">禁用的下拉框</label> <select id="disabledSelect" class="form-control"> <option>不可選擇</option> </select> </div> <div class="checkbox"> <label> <input type="checkbox"> 無(wú)法選擇 </label> </div> <button type="submit" class="btn btn-primary">提交</button> </fieldset> </form>
效果圖:
表單驗(yàn)證狀態(tài)
在制作表單時(shí),不免要做表單驗(yàn)證。同樣也需要提供驗(yàn)證狀態(tài)樣式,在Bootstrap框架中同樣提供這幾種效果。
1、.has-warning:警告狀態(tài)(黃色)
2、.has-error:錯(cuò)誤狀態(tài)(紅色)
3、.has-success:成功狀態(tài)(綠色)
很多時(shí)候,在表單驗(yàn)證的時(shí)候,不同的狀態(tài)會(huì)提供不同的 icon,比如成功是一個(gè)對(duì)號(hào)(√),錯(cuò)誤是一個(gè)叉號(hào)(×)等。在Bootstrap框中也提供了這樣的效果。如果你想讓表單在對(duì)應(yīng)的狀態(tài)下顯示 icon 出來(lái),只需要在對(duì)應(yīng)的狀態(tài)下添加類名“has-feedback”。請(qǐng)注意,此類名要與“has-error”、“has-warning”和“has-success”在一起:
<form role="form"> <div class="form-group has-success"> <label class="control-label" for="inputSuccess1">成功狀態(tài)</label> <input type="text" class="form-control" id="inputSuccess1" placeholder="成功狀態(tài)"> </div> <div class="form-group has-warning"> <label class="control-label" for="inputWarning1">警告狀態(tài)</label> <input type="text" class="form-control" id="inputWarning1" placeholder="警告狀態(tài)"> </div> <div class="form-group has-error"> <label class="control-label" for="inputError1">錯(cuò)誤狀態(tài)</label> <input type="text" class="form-control" id="inputError1" placeholder="錯(cuò)誤狀態(tài)"> </div> </form> <br> <br> <br> <form role="form"> <div class="form-group has-success has-feedback"> <label class="control-label" for="inputSuccess1">成功狀態(tài)</label> <input type="text" class="form-control" id="inputSuccess1" placeholder="成功狀態(tài)"> <span class="glyphicon glyphicon-ok form-control-feedback"></span> </div> <div class="form-group has-warning has-feedback"> <label class="control-label" for="inputWarning1">警告狀態(tài)</label> <input type="text" class="form-control" id="inputWarning1" placeholder="警告狀態(tài)"> <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span> </div> <div class="form-group has-error has-feedback"> <label class="control-label" for="inputError1">錯(cuò)誤狀態(tài)</label> <input type="text" class="form-control" id="inputError1" placeholder="錯(cuò)誤狀態(tài)"> <span class="glyphicon glyphicon-remove form-control-feedback"></span> </div> </form>
效果圖:
表單提示信息
平常在制作表單驗(yàn)證時(shí),要提供不同的提示信息。在Bootstrap框架中也提供了這樣的效果。使用了一個(gè)”help-block”樣式,將提示信息以塊狀顯示,并且顯示在控件底部。
<form role="form"> <div class="form-group has-success has-feedback"> <label class="control-label" for="inputSuccess1">成功狀態(tài)</label> <input type="text" class="form-control" id="inputSuccess1" placeholder="成功狀態(tài)"> <span class="help-block">你輸入的信息是正確的</span> <span class="glyphicon glyphicon-ok form-control-feedback"></span> </div> <div class="form-group has-warning has-feedback"> <label class="control-label" for="inputWarning1">警告狀態(tài)</label> <input type="text" class="form-control" id="inputWarning1" placeholder="警告狀態(tài)"> <span class="help-block">請(qǐng)輸入正確信息</span> <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span> </div> <div class="form-group has-error has-feedback"> <label class="control-label" for="inputError1">錯(cuò)誤狀態(tài)</label> <input type="text" class="form-control" id="inputError1" placeholder="錯(cuò)誤狀態(tài)"> <span class="help-block">你輸入的信息是錯(cuò)誤的</span> <span class="glyphicon glyphicon-remove form-control-feedback"></span> </div> </form>
效果圖:
以上所述是小編給大家介紹的Bootstrap表單樣式的使用,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 基于jQuery.validate及Bootstrap的tooltip開(kāi)發(fā)氣泡樣式的表單校驗(yàn)組件思路詳解
- Bootstrap實(shí)現(xiàn)水平排列的表單
- Bootstrap實(shí)現(xiàn)登錄校驗(yàn)表單(帶驗(yàn)證碼)
- Bootstrap三種表單布局的使用方法
- Bootstrap中的表單驗(yàn)證插件bootstrapValidator使用方法整理(推薦)
- BootStrap智能表單實(shí)戰(zhàn)系列(十一)級(jí)聯(lián)下拉的支持
- BootStrap 智能表單實(shí)戰(zhàn)系列(十)自動(dòng)完成組件的支持
- BootStrap智能表單實(shí)戰(zhàn)系列(九)表單圖片上傳的支持
相關(guān)文章
JavaScript基于對(duì)象方法實(shí)現(xiàn)數(shù)組去重及排序操作示例
這篇文章主要介紹了JavaScript基于對(duì)象方法實(shí)現(xiàn)數(shù)組去重及排序操作,涉及javascript基于對(duì)象方法的數(shù)組遍歷、比較、去重、排序等相關(guān)操作技巧,需要的朋友可以參考下2018-07-07盤點(diǎn)javascript 正則表達(dá)式中 中括號(hào)的【坑】
下面小編就為大家?guī)?lái)一篇盤點(diǎn)javascript 正則表達(dá)式中 中括號(hào)的【坑】。小編覺(jué)得總結(jié)的不錯(cuò)。現(xiàn)在分享給大家,希望能給大家一個(gè)參考2016-03-03Typescript 函數(shù)重載的實(shí)現(xiàn)
本文主要介紹了Typescript 函數(shù)重載的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04JavaScript簡(jiǎn)單實(shí)現(xiàn)的仿微博留言功能示例
這篇文章主要介紹了JavaScript簡(jiǎn)單實(shí)現(xiàn)的仿微博留言功能,涉及javascript頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01js實(shí)現(xiàn)獲取當(dāng)前時(shí)間是本月第幾周的方法
這篇文章主要介紹了js實(shí)現(xiàn)獲取當(dāng)前時(shí)間是本月第幾周的方法,涉及javascript針對(duì)日期及時(shí)間的相關(guān)操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-08-08js鼠標(biāo)移動(dòng)時(shí)禁止選中文字
本文主要介紹了js鼠標(biāo)移動(dòng)時(shí)禁止選中文字的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02Bootstrap+jfinal實(shí)現(xiàn)省市級(jí)聯(lián)下拉菜單
這篇文章主要為大家詳細(xì)介紹了Bootstrap+jfinal實(shí)現(xiàn)省市級(jí)聯(lián)下拉菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05js實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)表格行變色的方法
這篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)表格行變色的方法,涉及javascript表格節(jié)點(diǎn)樣式及鼠標(biāo)事件的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05js實(shí)現(xiàn)圖片放大并跟隨鼠標(biāo)移動(dòng)特效
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)圖片放大并跟隨鼠標(biāo)移動(dòng)特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01