当前位置:Gxlcms > css > 使用CSS自定义radio、checkbox样式的示例详解

使用CSS自定义radio、checkbox样式的示例详解

时间:2021-07-01 10:21:17 帮助过:8人阅读

以前做自定义样式的radio, checkbox 的时候,一直是如下结构

  1. <label>
  2. <span class="diyRadio">
  3. <input type="radio" name=" value="">
  4. </span>
  5. <span>文字</span>
  6. </label>

然后定义diyRadio 的样式作为新Radio, 再用js 做关联。

知道今天才知道可以用<label></label>标签的for 属性 + :checked 做,纯CSS
真是太不应该了,学东西还是要认真、细致点。

DIY 单选项示例如下:

  1. <!-- HTML -->
  2. <p class="radio-beauty-container">
  3. <label>
  4. <span class="radio-name">radio1</span>
  5. <input type="radio" name="radioName" id="radioName1" hidden/>
  6. <label for="radioName1" class="radio-beauty"></label>
  7. </label>
  8. <label>
  9. <span class="radio-name">radio2</span>
  10. <input type="radio" name="radioName" id="radioName2" hidden/>
  11. <label for="radioName2" class="radio-beauty"></label>
  12. </label>
  13. <label>
  14. <span class="radio-name">radio3</span>
  15. <input type="radio" name="radioName" id="radioName3" hidden/>
  16. <label for="radioName3" class="radio-beauty"></label>
  17. </label>
  18. </p>
  1. /* CSS */
  2. .radio-beauty-container {
  3. font-size: 0;
  4. }
  5. .radio-beauty-container .radio-beauty:hover, .radio-beauty-container input[type="radio"]:checked + .radio-beauty {
  6. padding: 2px;
  7. background-color: green;
  8. background-clip: content-box;
  9. }
  10. .radio-beauty-container .radio-name {
  11. vertical-align: middle;
  12. font-size: 16px;
  13. }
  14. .radio-beauty-container .radio-beauty {
  15. width: 18px;
  16. height: 18px;
  17. box-sizing: border-box;
  18. display: inline-block;
  19. border: 1px solid green;
  20. vertical-align: middle;
  21. margin: 0 15px 0 3px;
  22. border-radius: 50%;
  23. }
  24. .radio-beauty-container .radio-beauty:hover {
  25. box-shadow: 0 0 7px green;
  26. }

以上就是使用CSS自定义radio、checkbox样式的示例详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行