修改 input[type="radio"] 和 input[type="checkbox"] 的默认样式

怼烎@ 2021-12-21 14:29 444阅读 0赞

表单中,经常会使用到单选按钮和复选框,但是,input[type=”radio”] 和 input[type=”checkbox”] 的默认样式在不同的浏览器或者手机上,显示的效果总是不统一,而且难以修改器样式。

input[type=”radio”] 样式定制

代码:

  1. <form>
  2. <p>
  3. <input type="radio" name="gender" id="male" value="male">
  4. <label for="male">男士</label>
  5. </p>
  6. <p>
  7. <input type="radio" name="gender" id="female" value="female">
  8. <label for="female">女士</label>
  9. </p>
  10. </form>

css 样式

  1. input[type="radio"] {
  2. height: 22px;
  3. width: 22px;
  4. margin-right: 10px;
  5. display: none;
  6. }
  7. input[type="radio"] + label::before {
  8. content: "\a0"; /*不换行空格*/
  9. display: inline-block;
  10. vertical-align: middle;
  11. font-size: 18px;
  12. width: 18px;
  13. height: 18px;
  14. margin-right: 10px;
  15. border-radius: 50%;
  16. border: 1px solid #003c66;
  17. background: #fff;
  18. line-height: 22px;
  19. box-sizing: border-box;
  20. }
  21. input[type="radio"]:checked + label::before {
  22. background-color: #003c66;
  23. background-clip: content-box;
  24. padding: 3px;
  25. }

效果如图:

单选框

input[type=”checkbox”] 样式定制

代码:

  1. <form>
  2. <input id="select_all" name="select_all" type="checkbox">
  3. <label for="select_all"> <i></i>选择</label>
  4. </form>

css 样式

  1. input[type="checkbox"] {
  2. display: none;
  3. }
  4. input[type="checkbox"]+label>i {
  5. display: inline-block;
  6. width: 20px;
  7. height: 20px;
  8. border: 1px solid #bbb;
  9. background: #bbb;
  10. margin-right: 10px;
  11. vertical-align: middle;
  12. }
  13. input[type="checkbox"]:checked+label>i {
  14. position: relative;
  15. }
  16. input[type="checkbox"]:checked+label>i::before {
  17. content: '';
  18. position: absolute;
  19. width: 12px;
  20. height: 18px;
  21. color: black;
  22. border-bottom: 1px solid green;
  23. border-right: 1px solid green;
  24. left: 50%;
  25. top: 20%;
  26. transform-origin: center;
  27. transform: translate(-50%, -30%) rotate(40deg);
  28. -webkit-transform: translate(-50%, -30%) rotate(40deg);
  29. }

效果如图:

复选框

转载于:https://www.cnblogs.com/cckui/p/9411498.html

发表评论

表情:
评论列表 (有 0 条评论,444人围观)

还没有评论,来说两句吧...

相关阅读