纯CSS实现radio和checkbox实现效果

radio-and-checkbox

纯CSS实现radio和checkbox实现效果

reset-radio

  • 在开发PC端的项目时,经常会用到radio和checkbox组件,可是因为原生的样式相对来说不符合设计师的设计风格,所以我们可能会经常引用第三方的模块去实现,或者通过JS等其他方式去hack。这样相对来说增加了代码量不说,还特别复杂,所以才有了这个纯CSS依赖原生input[radio]和input[checkbox]的实现方式,主要代码如下:
  • html主要代码
<div class="reset-radio">
    <input checked type="radio" id="age1" name="age">
    <span class="real-target"></span>
</div>
  • CSS代码,这里主要是通过一个子节点span去配合input:checked兄弟选择器,来修改样式
.reset-radio {
    display: inline-block;
    position: relative;
    width: 16px;
    height: 16px;
}

.reset-radio .real-target {
    z-index: 1;
    width: 100%;
    height: 100%;
    position: absolute;
    display: inline-block;
    background: #ffffff;
    border: 1px solid #dadde0;
    border-radius: 100%;
    top: 0;
    left: 0;
    bottom: 0;
}

.reset-radio input[type=radio] {
    cursor: pointer;
    z-index: 2;
    width: 16px;
    height: 16px;
    opacity: 0;
    position: absolute;
    left: 0;
    top: 0;
    margin: 0;
    right: 0;
    bottom: 0;
}

.reset-radio input:checked+span {
    border-color: #48b4ec;
}

.reset-radio input:checked+span::before {
    content: '';
    position: absolute;
    background: #48b4ec;
    width: 6px;
    height: 6px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 100%;
}

reset-checkbox

  • reset-checkbox原理是一样的就不在赘述了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以使用 CSS 美化多选框(checkbox)和单选框(radio)。以下是一个简单的示例: HTML 代码: ```html <label class="checkbox"> <input type="checkbox"> <span class="checkmark"></span> Checkbox 1 </label> <label class="radio"> <input type="radio" name="radio"> <span class="checkmark"></span> Radio 1 </label> <label class="radio"> <input type="radio" name="radio"> <span class="checkmark"></span> Radio 2 </label> ``` CSS 代码: ```css /* 隐藏默认的 checkboxradio */ input[type="checkbox"], input[type="radio"] { display: none; } /* 定义 checkboxradio 的样式 */ .checkmark { display: inline-block; width: 20px; height: 20px; border: 1px solid #999; border-radius: 5px; cursor: pointer; position: relative; } /* 定义被选中的 checkboxradio 的样式 */ .checkmark::after { content: ""; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 10px; height: 10px; background-color: #999; border-radius: 50%; display: none; } input[type="checkbox"]:checked + .checkmark::after { display: block; } input[type="radio"]:checked + .checkmark::after { display: block; } /* 定义 label 的样式 */ label { display: block; margin-bottom: 10px; } /* 定义 checkboxradio 的父元素的样式 */ .checkbox, .radio { display: inline-block; vertical-align: top; margin-right: 20px; } /* 定义 checkboxradio 的文本样式 */ .checkbox span, .radio span { margin-left: 10px; font-size: 14px; } ``` 你可以根据需要调整样式,达到想要的美化效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值