表单
bootstrap为表单提供三种样式:默认表单,水平表单,内联表单。
<form class="form-horizontal" role="form"> <!--默认表单--> </form> <!--分割线--> <form class="form-horizontal" role="form"> <!--水平表单--> </form> <!--分割线--> <form class="form-inline" role="form"> <!--内联表单--> </form>
水平表单效果图(ps:bootstrap默认表单是label标签和表单元素上下排列)
内联表单效果图(ps:内联表单就是把垂直排列的表单元素水平排列)
<form role="form"> <div class="form-group"> <label for="exampleInputEmail1">邮箱:</label> <input type="email" class="form-control" id="exampleInputEmail1" placeholder="请输入您的邮箱地址"> </div> <div class="form-group"> <label for="exampleInputPassword1">密码</label> <input type="password" class="form-control" id="exampleInputPassword1" placeholder="请输入您的邮箱密码"> </div> <div class="checkbox"> <label> <input type="checkbox"> 记住密码 </label> </div> <button type="submit" class="btn btn-default">进入邮箱</button> </form>
表单元素
下拉框<select></select>
bootstrap除了传统的单选下拉框外,还新增了多选下拉框。
<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> <option>6</option> <option>7</option> </select> </div> </form>
单选下拉框效果图
多选下拉框效果图
单选,复选框
bootstrap对checkbox和radio做了一些优化。用<div></div>包裹一组checkbox或者radio元素。用class="checkbox"或者class="radio"赋值div样式。
<form role="form"> <h2>选择框<small>案例</small></h2> <!--复选框--> <div class="checkbox"> <label> <input type="checkbox" value=""> 复选 </label> </div> <!--单选框--> <div class="radio"> <label> <input type="radio" name="optionsRadios" id="optionsRadios1" value="love" checked> 单选1 </label> </div> <div class="radio"> <label> <input type="radio" name="optionsRadios" id="optionsRadios2" value="hate"> 单选2 </label> </div> </form>
优化后的checkbox选择框和文本对齐更完美。
水平选择框
一组checkbox或radio呈横向水平排列
class="checkbox-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>
按钮
bootstrap的按钮更漂亮直接看看效果吧
class="btn",基本按钮样式。
class="btn btn-info(primary,success,warning,danger)",带颜色的按钮样式。
<button class="btn btn-primary">按钮</button> <a class="btn btn-info">超链接</a> <input type="submit" class="btn btn-success"/> <input type="text" value="输入框" class="btn btn-danger"/>
效果图
调整表单元素尺寸
元素尺寸调节:
class="input input-lg" (大号)
class="input" (普通)
class="input input-sm" (小号)
(ps:input也可以替换其他元素,如:btn,text)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>表单控件大小</title> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> </head> <body> <h1>案例1</h1> <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> <button class="btn btn-lg">btn-lg</button> <button class="btn">默认</button> <button class="btn btn-sm">btn-sm</button> </form> </body> </html>
效果图
表单控件禁用
bootstrap禁用表单控件仅仅需要一个属性:disabled(任何表单元素都有disabled属性)
<h3>示例1</h3> <form role="form" class="form-horizontal"> <div class="form-group"> <div class="col-xs-6"> <input class="form-control input-lg" type="text" placeholder="不是焦点状态下效果"> </div> <div class="col-xs-6"> <input class="form-control input-lg" id="disabledInput" type="text" placeholder="表单已被禁用,不可输入" disabled> </div> </div> </form> <br> <h3>示例2</h3> <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"> 无法选择 </label> </div> <button type="submit" class="btn btn-primary">提交</button> </fieldset> </form> <br> <form role="form"> <fieldset disabled> <legend><input type="text" class="form-control" placeholder="显然我颜色变灰了,但是我没被禁用,不信?单击试一下" /></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"> 无法选择 </label> </div> <button type="submit" class="btn btn-primary">提交</button> </fieldset> </form>
表单提示消息
配合表单元素显示提示消息:class="help-block"
<h3>示例1</h3> <form role="form"> <div class="form-group has-success has-feedback"> <label class="control-label" for="inputSuccess1">成功状态</label> <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" > <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">警告状态</label> <input type="text" class="form-control" id="inputWarning1" placeholder="警告状态"> <span class="help-block">请输入正确信息</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">错误状态</label> <input type="text" class="form-control" id="inputError1" placeholder="错误状态"> <span class="help-block">你输入的信息是错误的</span> <span class="glyphicon glyphicon-remove form-control-feedback"></span> </div> </form> <br> <form role="form"> <div class="form-group"> <label class="control-label" for="inputSuccess1">成功状态</label> <div class="row"> <div class="col-xs-6"> <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" > </div> <span class="col-xs-6 help-block">你输入的信息是正确的</span> </div> </div> </form>
效果图