Bootstrap-CSS-表单

来源:互联网 发布:中文编程安卓版 编辑:程序博客网 时间:2024/06/14 14:27

基本实例

单独的表单控件会被自动赋予一些全局样式。所有设置了 .form-control 类的 <input>、<textarea> 和 <select> 元素都将被默认设置宽度属性为 width: 100%;。 将 label 元素和前面提到的控件包裹在 .form-group 中可以获得最好的排列。

<form>    <div class="form-group">        <label for="name">姓名:</label>        <input type="text" id="name" class="form-control" placeholder="请输入姓名 ">    </div>    <div class="form-group">        <label for="file">File:</label>        <input type="file" id="file">        <p class="help-block">提示:请选择文件</p>    </div>    <div class="checkbox">        <label>            <input type="checkbox"> m        </label>    </div>    <button type="submit" class="btn btn-primary">提交</button></form>
                                                                                   

内联表单

为 <form> 元素添加 .form-inline 类可使其内容左对齐并且表现为 inline-block 级别的控件。

只适用于视口(viewport)至少在 768px 宽度时(视口宽度再小的话就会使表单折叠)。
在 Bootstrap 中,输入框和单选/多选框控件默认被设置为 width: 100%; 宽度。在内联表单,我们将这些元素的宽度设置为 width: auto;,因此,多个控件可以排列在同一行。根据你的布局需求,可能需要一些额外的定制化组件。

一定要添加 label 标签

如果你没有为每个输入控件设置 label 标签,屏幕阅读器将无法正确识别。对于这些内联表单,你可以通过为 label 设置 .sr-only 类将其隐藏。 

水平排列的表单

通过为表单添加 .form-horizontal 类,并联合使用 Bootstrap 预置的栅格类,可以将 label 标签和控件组水平并排布局。这样做将改变 .form-group 的行为,使其表现为栅格系统中的行(row),因此就无需再额外添加 .row 了。

文本域

支持多行文本的表单控件。可根据需要改变 rows 属性。

多选和单选框

多选框(checkbox)用于选择列表中的一个或多个选项,而单选框(radio)用于从多个选项中只选择一个。

设置了 disabled 属性的单选或多选框都能被赋予合适的样式。对于和多选或单选框联合使用的 <label> 标签,如果也希望将悬停于上方的鼠标设置为“禁止点击”的样式,请将 .disabled 类赋予 .radio、.radio-inline、.checkbox、.checkbox-inline 或 <fieldset>。

内联单选和多选框
通过将 .checkbox-inline 或 .radio-inline 类应用到一系列的多选框(checkbox)或单选框(radio)控件上,可以使这些控件排列在一行。

下拉列表(select)

注意,很多原生选择菜单单 - 即在 Safari 和 Chrome 中 - 的圆角是无法通过修改 border-radius 属性来改变的。

对于标记了 multiple 属性的 <select> 控件来说,默认显示多选项。

<form class="form-horizontal">    <div class="form-group">        <label for="name2" class="col-sm-2 control-label">name</label>        <div class="col-sm-10">            <input type="text" placeholder="name" class="form-control" id="name2">        </div>    </div>    <textarea class="form-control" rows="3" placeholder="textarea"></textarea>    <div class="checkbox">        <label>            <input type="checkbox" value="">1        </label>        <label>            <input type="checkbox" value="" disabled>不能选了        </label>    </div>    <div class="checkbox disabled">        <label>            <input type="checkbox" value="">鼠标移到文字上显示禁止        </label>        <label>            <input type="checkbox" value="">没有效果        </label>    </div>    <div class="radio">        <label>            <input type="radio" name="radio1">radio        </label>        <label>            <input type="radio" name="radio1" checked>默认        </label>        <label>            <input type="radio" name="radio1" disabled>无法选择        </label>    </div>    <div class="checkbox">        <label class="checkbox-inline">            <input type="checkbox" value="">1        </label>        <label class="checkbox-inline">            <input type="checkbox" value="">2        </label>    </div>    <div class="radio">        <label class="radio-inline">            <input type="radio" name="radio2">radio-inline        </label>        <label class="radio-inline">            <input type="radio" name="radio2" checked>默认        </label>        <label class="radio-inline">            <input type="radio" name="radio2" disabled>无法选择        </label>    </div>    <select class="form-control">        <option>1</option>        <option>2</option>        <option selected>3</option>        <option>4</option>        <option>5</option>    </select>    <select class="form-control" multiple>        <option>1</option>        <option>2</option>        <option>3</option>        <option selected>4</option>        <option>5</option>    </select><br></form>
                                    

焦点状态

我们将某些表单控件的默认 outline 样式移除,然后对 :focus 状态赋予 box-shadow 属性。

禁用状态

为输入框设置 disabled 属性可以防止用户输入,并能对外观做一些修改,使其更直观。

被禁用的 fieldset

为<fieldset> 设置 disabled 属性,可以禁用 <fieldset> 中包含的所有控件。

<a> 标签的链接功能不受影响

只读状态

为输入框设置 readonly 属性可以禁止用户输入,并且输入框的样式也是禁用状态。

校验状态

Bootstrap 对表单控件的校验状态,如 error、warning 和 success 状态,都定义了样式。使用时,添加 .has-warning、.has-error 或 .has-success 类到这些控件的父元素即可。任何包含在此元素之内的 .control-label、.form-control 和 .help-block 元素都将接受这些校验状态的样式。

添加额外的图标

你还可以针对校验状态为输入框添加额外的图标。只需设置相应的 .has-feedback 类并添加正确的图标即可。(fonts文件夹要放在bootstrap.min.css的同一层)

控件尺寸

通过 .input-lg 类似的类可以为控件设置高度,通过 .col-lg-* 类似的类可以为控件设置宽度。

高度尺寸

创建大一些或小一些的表单控件以匹配按钮尺寸。

<form>    <div class="form-group">        <label class="sr-only"></label>        <input type="text" class="focus form-control" placeholder="focus">    </div>    <div class="form-group has-success">        <label class="sr-only">has-success</label>        <input type="text" class="form-control" placeholder="has-success">    </div>    <div class="form-group has-warning">        <label class="sr-only">has-success</label>        <input type="text" class="form-control" placeholder="has-warning">    </div>    <div class="form-group has-error">        <label class="sr-only">has-success</label>        <input type="text" class="form-control" placeholder="has-error">    </div>    <div class="form-group has-success has-feedback">        <label class="control-label" for="inputSuccess2">Input with success</label>        <input type="text" class="form-control" id="inputSuccess2" aria-describedby="inputSuccess2Status">        <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>        <span id="inputSuccess2Status" class="sr-only">(success)</span>    </div>    <div class="form-group has-warning has-feedback">        <label class="control-label" for="inputWarning2">Input with warning</label>        <input type="text" class="form-control" id="inputWarning2" aria-describedby="inputWarning2Status">        <span class="glyphicon glyphicon-warning-sign form-control-feedback" aria-hidden="true"></span>        <span id="inputWarning2Status" class="sr-only">(warning)</span>    </div>    <div class="form-group has-error has-feedback">        <label class="control-label" for="inputError2">Input with error</label>        <input type="text" class="form-control" id="inputError2" aria-describedby="inputError2Status">        <span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>        <span id="inputError2Status" class="sr-only">(error)</span>    </div>    <div class="form-group has-success has-feedback">        <label class="control-label" for="inputGroupSuccess1">Input group with success</label>        <div class="input-group">            <span class="input-group-addon">@</span>            <input type="text" class="form-control" id="inputGroupSuccess1" aria-describedby="inputGroupSuccess1Status">        </div>        <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>        <span id="inputGroupSuccess1Status" class="sr-only">(success)</span>    </div>    <input class="form-control input-lg" type="text" placeholder="input-lg"><br>    <input class="form-control" type="text" placeholder="Default input"><br>    <input class="form-control input-sm" type="text" placeholder="input-sm"><br>    <select class="form-control input-lg">        <option>input-lg</option>    </select><br>    <select class="form-control">        <option>Default input</option>    </select><br>    <select class="form-control input-sm">        <option>input-sm</option>    </select><br></form>
                                                                                                      

水平排列的表单组的尺寸

通过添加 .form-group-lg 或 .form-group-sm 类,为 .form-horizontal 包裹的 label 元素和表单控件快速设置尺寸。

调整列(column)尺寸

用栅格系统中的列(column)包裹输入框或其任何父元素,都可很容易的为其设置宽度。

<form class="form-horizontal">    <div class="form-group form-group-lg">        <label class="col-sm-2 control-label" for="formGroupInputLarge">Large label</label>        <div class="col-sm-10">            <input class="form-control" type="text" id="formGroupInputLarge" placeholder="Large input">        </div>    </div>    <div class="row">        <div class="col-xs-2">            <input type="text" class="form-control" placeholder=".col-xs-2">        </div>        <div class="col-xs-3">            <input type="text" class="form-control" placeholder=".col-xs-3">        </div>        <div class="col-xs-4">            <input type="text" class="form-control" placeholder=".col-xs-4">        </div>    </div></form>
          

0 0
原创粉丝点击