zoukankan      html  css  js  c++  java
  • 用纯css改变默认的radio和checkbox的样式

    利用css的label的伪类(::before)代替checkbox和radio效果:

    1. 优点:需要图片来调整选中前和选中后的样式,纯css搞定

    2. 缺点:兼容性,IE8以下不支持

    在线例子:

    <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>css改变默认的radio和checkbox的样式</title> <style> input[type="radio"], input[type='checkbox'] { display: none; }
    input[type='radio']+label::before,
    input[type='checkbox']+label::before {
      content: '';
      display: inline-block;
       15px;
      height: 15px;
      margin-right: 6px;
      vertical-align: middle;
      border: 1px solid #E4E4E4;
      border-radius: 50%;
      background: #FFFFFF;
      background-image: url('https://i.loli.net/2018/07/20/5b517d0bf066a.png');
      background-position: 0px 0px;
    }
    
    input[type='radio']:hover+label::before,
    input[type='checkbox']:hover+label::before {
      background-position: -15px 0px;
    }
    
    input[type='radio']:checked+label::before,
    input[type='checkbox']:checked+label::before {
      background-position: -15px -15px;
    }
    
    </style> </head> <body> <p>单选框</p> <input type="radio" name="sex" value="man" id="man" checked> <label for="man">男</label> <input type="radio" name="sex" value="female" id="female"> <label for="female">女</label> <p>多选框</p> <input type="checkbox" name="fruits" value="apple" id="apple" checked> <label for="apple">苹果</label> <input type="checkbox" name="fruits" value="orange" id="orange"> <label for="orange">橙子</label> </body>

    代码:

    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>css改变默认的radio和checkbox的样式</title>
      <style>
        input[type="radio"],
        input[type='checkbox'] {
          display: none;
        }
    
        input[type='radio']+label::before,
        input[type='checkbox']+label::before {
          content: '';
          display: inline-block;
           15px;
          height: 15px;
          margin-right: 6px;
          border-radius: 100%;
          vertical-align: middle;
          border: 1px solid #E4E4E4;
          background: #FFFFFF;
          background-image: url('https://i.loli.net/2018/07/20/5b517d0bf066a.png');
          background-position: 0px 0px;
        }
    
        input[type='radio']:hover+label::before,
        input[type='checkbox']:hover+label::before {
          background-position: -15px 0px;
        }
    
        input[type='radio']:checked+label::before,
        input[type='checkbox']:checked+label::before {
          background-position: -15px -15px;
        }
      </style>
    </head>
    
    <body>
      <p>单选框</p>
      <input type="radio" name="sex" value="man" id="man" checked>
      <label for="man">男</label>
      <input type="radio" name="sex" value="female" id="female">
      <label for="female">女</label>
      <p>多选框</p>
      <input type="checkbox" name="fruits" value="apple" id="apple" checked>
      <label for="apple">苹果</label>
      <input type="checkbox" name="fruits" value="orange" id="orange">
      <label for="orange">橙子</label>
    </body>
    
    </html>
    
  • 相关阅读:
    对于js中原型的理解
    换行问题
    居中方法
    浮动清除
    js基础内容 原型与实例
    uniapp 吸顶 小demo
    uniapp 锚点滚动报错(h.push is not a function)
    uni-app 页面滚动到指定位置
    过滤后端返回的html文本标签
    uniapp 上拉加载
  • 原文地址:https://www.cnblogs.com/don-yang/p/9341194.html
Copyright © 2011-2022 走看看