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

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

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

代码:

<form>
    <p>
        <input type="radio" name="gender" id="male" value="male">
        <label for="male">男士</label>
    </p>
    <p>
        <input type="radio" name="gender" id="female" value="female">
        <label for="female">女士</label>
    </p>
</form>

css 样式

input[type="radio"] {
    height: 22px;
    width: 22px;
    margin-right: 10px;
    display: none;
}
input[type="radio"] + label::before {
    content: "\a0"; /*不换行空格*/
    display: inline-block;
    vertical-align: middle;
    font-size: 18px;
    width: 18px;
    height: 18px;
    margin-right: 10px;
    border-radius: 50%;
    border: 1px solid #003c66;
    background: #fff;
    line-height: 22px;
    box-sizing: border-box;
}
input[type="radio"]:checked + label::before {
    background-color: #003c66;
    background-clip: content-box;
    padding: 3px;
}

效果如图:

单选框

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

代码:

<form>
    <input id="select_all" name="select_all" type="checkbox">
    <label for="select_all"> <i></i>选择</label>
</form>

css 样式

input[type="checkbox"] {
    display: none;
}

input[type="checkbox"]+label>i {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 1px solid #bbb;
    background: #bbb;
    margin-right: 10px;
    vertical-align: middle;
}

input[type="checkbox"]:checked+label>i {
    position: relative;
}

input[type="checkbox"]:checked+label>i::before {
    content: '';
    position: absolute;
    width: 12px;
    height: 18px;
    color: black;
    border-bottom: 1px solid green;
    border-right: 1px solid green;
    left: 50%;
    top: 20%;
    transform-origin: center;
    transform: translate(-50%, -30%) rotate(40deg);
    -webkit-transform: translate(-50%, -30%) rotate(40deg);
}

效果如图:

复选框

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值