自定义单选框样式

这篇文章展示了如何使用CSS样式来定制HTML中的radio类型input,通过移除原生样式并利用伪元素创建圆形的单选按钮。主要涉及到border-radius、appearance属性以及CSS布局技巧。
摘要由CSDN通过智能技术生成
  1. 原理:就是把原生的input的radio类型的样式去掉,然后利用伪元素实现

border-radius:设置50%就成圆形

  1. 代码

<template>
  <div class="changeOver">
    <label>
      <input type="radio" name="original" checked /><span>第一个</span>
    </label>
    <label>
      <input type="radio" name="number" class="radio" checked /><span
        >第一个</span
      >
    </label>
    <label>
      <input type="radio" name="number" class="radio" /><span>第二个</span>
    </label>
  </div>
</template>

<script>
export default {};
</script>

<style>
.changeOver {
  display: flex;
  flex-direction: column;
  position: absolute;
  background: #fff;
  top: 8px;
  left: 16px;
}
label {
  width: 130px;
  height: 26px;
  display: flex;
  line-height: 26px;
}
span {
  display: inline-block;
}
.radio {
  width: 16px;
  height: 16px;
  appearance: none; /*隐藏原生单选框的样式*/
  -webkit-appearance: none; /*隐藏原生单选框的样式*/
  outline: none;
  border-radius: 50%;
  position: relative;
}
.radio:before,
.radio:after {
  content: "";
  display: block;
  position: absolute;
  border-radius: 50%;
}
.radio:before {
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 50%;
  border: 1px solid green;
}
.radio:after {
  top: 6px;
  left: 6px;
  width: 5px;
  height: 5px;
  background-color: #fff;
  border-radius: 50%;
}
.radio:checked:after {
  top: 5.2px;
  left: 5.5px;
  width: 9px;
  height: 9px;
  background-color: green;
}
.radio:checked:before {
  border-color: green;
}
</style>
  1. 效果图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值