3-7 表单控件(复选框checkbox和单选择按钮radio)
本节编程练习不计算学习进度,请电脑登录imooc.com操作

表单控件(复选框checkbox和单选择按钮radio)

Bootstrap框架中checkbox和radio有点特殊,Bootstrap针对他们做了一些特殊化处理,主要是checkbox和radio与label标签配合使用会出现一些小问题(最头痛的是对齐问题)。使用Bootstrap框架,开发人员无需考虑太多,只需要按照下面的方法使用即可。

<form role="form">
<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>

运行效果如下或查看右侧结果窗口(案例1):

从上面的示例,我们可以得知:
1、不管是checkbox还是radio都使用label包起来了
2、checkbox连同label标签放置在一个名为“.checkbox”的容器内
3、radio连同label标签放置在一个名为“.radio”的容器内
在Bootstrap框架中,主要借助“.checkbox”和“.radio”样式,来处理复选框、单选按钮与标签的对齐方式。源码请查看bootstrap.css文件第1742行~第1762行:

.radio,
.checkbox {
display: block;
min-height: 20px;
padding-left: 20px;
margin-top: 10px;
margin-bottom: 10px;
}
.radio label,
.checkbox label {
display: inline;
font-weight: normal;
cursor: pointer;
}
.radio input[type="radio"],
.radio-inline input[type="radio"],
.checkbox input[type="checkbox"],
.checkbox-inline input[type="checkbox"] {
float: left;
margin-left: -20px;
}
.radio + .radio,
.checkbox + .checkbox {
margin-top: -5px;
}

任务

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>表单控件——表单控件大小</title>
  6. <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <form role="form">
  10. <h3>案例1</h3>
  11. <div class="checkbox">
  12. <label>
  13. <input type="checkbox" value="">
  14. 记住密码
  15. </label>
  16. </div>
  17. <div class="radio">
  18. <label>
  19. <input type="radio" name="optionsRadios" id="optionsRadios1" value="love" checked>
  20. 喜欢
  21. </label>
  22. </div>
  23. <div class="radio">
  24. <label>
  25. <input type="radio" name="optionsRadios" id="optionsRadios2" value="hate">
  26. 不喜欢
  27. </label>
  28. </div>
  29.  
  30.  
  31. </form>
  32. </body>
  33. </html>
  1. body {
  2. padding: 100px;
  3. }
下一节