code_junkie

How to align checkboxes and their labels consistently cross-browsers

Nov 12th, 2011
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. <form>
  2. <div>
  3. <label><input type="checkbox" /> Label text</label>
  4. </div>
  5. </form>
  6.  
  7. <form>
  8. <div>
  9. <label><input type="checkbox" /> Label text</label>
  10. </div>
  11. </form>
  12.  
  13. <style type="text/css">
  14. label {
  15. display: block;
  16. padding-left: 15px;
  17. text-indent: -15px;
  18. }
  19. input {
  20. width: 13px;
  21. height: 13px;
  22. padding: 0;
  23. margin:0;
  24. vertical-align: bottom;
  25. position: relative;
  26. top: -1px;
  27. *overflow: hidden;
  28. }
  29. </style>
  30.  
  31. <style type="text/css">
  32. .checkboxes label
  33. {
  34. display: block;
  35. float: left;
  36. padding-right: 10px;
  37. white-space: nowrap;
  38. }
  39.  
  40. .checkboxes input
  41. {
  42. vertical-align: middle;
  43. }
  44.  
  45. .checkboxes label span
  46. {
  47. vertical-align: middle;
  48. }
  49. </style>
  50.  
  51. <form>
  52. <div class="checkboxes">
  53. <label for="x"><input type="checkbox" id="x" /> <span>Label text x</span></label>
  54. <label for="y"><input type="checkbox" id="y" /> <span>Label text y</span></label>
  55. <label for="z"><input type="checkbox" id="z" /> <span>Label text z</span></label>
  56. </div>
  57. </form>
  58.  
  59. .checkboxes label
  60. {
  61. display: block;
  62. float: left;
  63. padding-right: 10px;
  64. padding-left: 22px;
  65. text-indent: -22px;
  66. }
  67.  
  68. <style type="text/css">
  69. *
  70. {
  71. padding: 0px;
  72. margin: 0px;
  73. }
  74. #wb
  75. {
  76. width: 15px;
  77. height: 15px;
  78. float: left;
  79. }
  80. #somelabel
  81. {
  82. float: left;
  83. padding-left: 3px;
  84. }
  85. </style>
  86.  
  87. <div>
  88. <input id="wb" type="checkbox" /><label for="wb" id="somelabel">Web Browser</label>
  89. </div>
  90.  
  91. input[type=checkbox], input[type=radio] {
  92. vertical-align: middle;
  93. position: relative;
  94. bottom: 1px;
  95. }
  96. input[type=radio] {
  97. bottom: 2px;
  98. }
  99.  
  100. <form>
  101. <div>
  102. <label><input type="checkbox" /> Label text</label>
  103. </div>
  104. </form>
  105.  
  106. <style type="text/css">
  107. label {
  108. line-height: 18px;
  109. }
  110. input {
  111. width: 13px;
  112. height: 18px;
  113. font-size: 12px;
  114. line-height: 12px;
  115. }
  116. </style>
  117.  
  118. <form>
  119. <div>
  120. <input type="checkbox" id="cb" /> <label for="cb">Label text</label>
  121. </div>
  122. </form>
  123.  
  124. <form>
  125. <div>
  126. <input id="blah" type="checkbox" /><label for="blah">Label text</label>
  127. </div>
  128. </form>
  129.  
  130. fieldset{text-align:left;border:none}
  131. fieldset ol,fieldset ul{padding:0;list-style:none}
  132. fieldset li{padding-bottom:1.5em;float:none;clear:left}
  133. label{float:left;width:7em;margin-right:1em}
  134. fieldset.checkboxes li{clear:both;padding:.75em}
  135. fieldset.checkboxes label{margin:0 0 0 1em;width:20em}
  136. fieldset.checkboxes input{float:left}
  137.  
  138. <form>
  139.  
  140. <fieldset class="checkboxes">
  141.  
  142. <ul>
  143. <li>
  144. <input type="checkbox" name="happy" value="yep" id="happy"/>
  145. <label for="happy">Happy?</label>
  146. </li>
  147. <li>
  148. <input type="checkbox" name="hungry" value="yep" id="hungry"/>
  149. <label for="hungry">Hungry?</label>
  150. </li>
  151. </ul>
  152.  
  153. </fieldset>
  154.  
  155. </form>
  156.  
  157. var label = ...
  158. var input = document.getElementById(label.htmlFor);
  159.  
  160. Without using DIV and other elements and fixed sizes.
  161. We can "align checkboxlist text by Setting
  162. "**float:left**" to the checkbox list input type
  163. in Css
  164.  
  165. Please check the following example code:
  166. ========================================
  167.  
  168. .CheckboxList
  169. {
  170.  
  171. font-size:14px;
  172. color:#333333;
  173.  
  174. }
  175. .CheckboxList input
  176. {
  177. float:left;
  178. clear:both;
  179. }
  180.  
  181. .Aspx Code:
  182. ===========
  183.  
  184. <asp:CheckBoxList runat="server" ID="chlCities" RepeatColumns="2" CssClass="CheckboxList"></asp:CheckBoxList>
  185.  
  186. input {
  187. width: 13px;
  188. height: 13px;
  189. padding: 0;
  190. margin:0;
  191. vertical-align: top;
  192. position: relative;
  193. *top: 1px;
  194. *overflow: hidden;
  195. }
  196. label {
  197. display: block;
  198. padding: 0;
  199. padding-left: 15px;
  200. text-indent: -15px;
  201. border: 0px solid;
  202. margin-left: 5px;
  203. vertical-align: top;
  204. }
  205.  
  206. <label for="id" class="checkbox">
  207. <input type="checkbox" id="id">
  208. <span>The Label</span>
  209. </label>
  210.  
  211. label.checkbox { display: block; }
  212. .checkbox input { float: left; height: 18px; vertical-align: middle; }
  213. .checkbox span { float: left; line-height: 18px; margin: 0 0 0 20px; }
  214.  
  215. input {
  216. margin:0;
  217. }
  218.  
  219. input { vertical-align: -2px }
  220.  
  221. input[type=checkbox], input[type=radio] {
  222. vertical-align: baseline;
  223. position: relative;
  224. top: 3px;
  225. margin: 0 3px 0 0;
  226. padding: 0px;
  227. }
  228. input.ie7[type=checkbox], input.ie7[type=radio] {
  229. vertical-align: middle;
  230. position: static;
  231. margin-bottom: -2px;
  232. height: 13px;
  233. width: 13px;
  234.  
  235. $(document).ready(function() {
  236. if ($.browser.msie && $.browser.version <= 7) {
  237. $('input[type=checkbox]').addClass('ie7');
  238. $('input[type=radio]').addClass('ie7');
  239. }
  240. });
  241.  
  242. <form>
  243. <div>
  244. <label style="display: inline-block">
  245. <input style="vertical-align: middle" type="checkbox" />
  246. <span style="vertical-align: middle">Label text</span>
  247. </label>
  248. </div>
  249. </form>
Add Comment
Please, Sign In to add comment