DIV CSS 佈局教程網

深入學習 Bootstrap 表單:布局、控件與狀態
編輯:關於JavaScript     

本文知識點借鑒自 https://www.runoob.com/bootstrap/bootstrap-forms.html ,在原文基礎上歸納重點,方便記憶與複習。

一、表單布局

1. 垂直表單(默認)

向父 <form> 元素添加 role="form";把標簽和控件放在帶有 .form-group 類的 <div> 中(這是獲取最佳間距所必需的);向所有文本元素 <input>、<textarea> 和 <select> 添加 .form-control 類。

 <form action="#" role="form"> <div class="form-group"> <lable>姓名:<input class="form-control" type="text"/></lable> <lable>性別:<input class="form-control" type="text"/></lable> </div> </form> 

2. 內聯表單

向 <form> 標簽添加 .form-inline。默認情況下 Bootstrap 中的 input、select、textarea 寬度為 100%,使用內聯表單時需手動設置控件寬度。使用 .sr-only 類可以隱藏內聯表單的標簽。

3. 水平表單

向父 <form> 元素添加 .form-horizontal;把標簽和控件放在帶有 .form-group 的 <div> 中;並向標簽添加 .control-label 類。

 <form action="#" role="form" class="form-horizontal"> <div class="form-group"> <lable class="control-label col-lg-2">姓名:</lable> <div class=" col-lg-10"><input class="form-control" type="text"/></div> </div> </form> 

二、支持的表單控件

1. 輸入框(Input)

<lable>姓名:<input class="form-control" type="text"/></lable>

2. 文本框(Textarea)

可透過 rows 屬性改變行數。

 <div class="form-group"> <textarea rows="8" class="form-control"> </textarea> </div> 

3. 複選框(Checkbox)和單選框(Radio)

需要用戶從列表中選擇若干個選項時用 checkbox,限制只能選一個時用 radio。對一系列複選框或單選框使用 .checkbox-inline 或 .radio-inline 類,可控制它們顯示在同一行上。

 <div class="checkbox-inline"> <lable class="control-label"><input type="checkbox"/>籃球</lable> </div> <div class="radio"> <lable><input type="radio"/>男</lable> </div> 

4. 選擇框(Select)

使用 <select> 展示列表選項,加上 multiple="multiple" 可允許複選。

 <div class="form-group"> <select name="" id="" class="form-control" multiple> <option value="">1</option> </select> </div> 

5. 靜態控件

在水平表單內的標簽後放置純文本時,在 <p> 上使用 .form-control-static 類。

 <div class="form-group"> <label class="col-sm-2 control-label">Email</label> <div class="col-sm-10"> <p class="form-control-static">email@example.com</p> </div> </div> 

三、表單控件狀態

1. 輸入框焦點

當 input 接收到 :focus 時,輪廓會被移除,同時套用 box-shadow。

2. 禁用的輸入框

為 input 添加 disabled 屬性即可禁用,這不僅禁用輸入框,還會改變其樣式及滑鼠指針樣式。

 <div> <lable>姓名:<input type="text" disabled class="form-control"/></lable> </div> 

3. 禁用的字段集

對 <fieldset> 添加 disabled 屬性,可禁用其內的所有控件。

4. 驗證狀態

Bootstrap 內含錯誤、警告和成功消息的驗證樣式,只需對父元素添加 .has-warning、.has-error 或 .has-success 類即可。

 <div class="has-error"> <lable class="control-label"> 姓名:<input type="text" class="form-control" placeholder="has-error"/> </lable> </div> <div class="has-warning"> <lable class="control-label"> 姓名:<input type="text" class="form-control" placeholder="has-warning"/> </lable> </div> 

5. 表單控件大小

分別使用 .input-lg(或 .input-sm)和 .col-lg-* 來設置表單的高度與寬度。

6. 表單幫助文本

在 <input> 後使用 .help-block,可添加一個佔用整個寬度的塊級幫助文本。

 <div class="form-grounp"> <input type="text"/> <span class="help-block">這是一段幫助文本</span> </div> 

以上就是本文的全部內容,希望對大家的學習有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved