css 模拟radio的样式

1、input 默认的 type 为 radio的样式,在具体场合中的改造

默认的样式这样:

但是我要这样的:

这样看来是不是比原来的好看多了。

2、优化radio的样式

<span class="answer-item-wrapper" :class="{ active: chooseNum === index }" @click="selectItem(index)">
   <span class="select-wrapper">
   </span>
   <span class="select-content">
     {{val}}
   </span>
</span>

 这个是vue的一个例子,点击一个元素给它追加一个class。当然这个不是重点,重点是 红色部分,我们需要对红色部分进行css描述。

  .select-wrapper {
    display: inline-block;
    height: 16px;
    width: 16px;
    background-color: #fff;
    border: 1px solid #d4a668;
    border-radius: 100%;
    margin-right: 10px;
    margin-top: -1px;
    vertical-align: middle;
    line-height: 1;
  }

  然后对这个添加一个伪类

.answer-item-wrapper.active .select-wrapper::after {
    content: "";
    display: inline-block;
    height: 12px;
    margin: 2px;
    width: 12px;
    background-color: #cd9a51;
    border-radius: 100%;
 }

 OK 这样的 话, 就可以实现了radio这样按钮的格式。

3、直接对input的一种改造

具体见完整demo,参考另外一个同学的写法

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>

  <style>
    .demo--label{margin:20px 20px 0 0;display:inline-block}
    .demo--radio{display:none}
    .demo--radioInput{background-color:#fff;border:1px solid #cd9a51;border-radius:100%;display:inline-block;height:16px;margin-right:10px;margin-top:-1px;vertical-align:middle;width:16px;line-height:1}
    .demo--radio:checked + .demo--radioInput:after{background-color:#cd9a51;border-radius:100%;content:"";display:inline-block;height:12px;margin:2px;width:12px}
    .demo--checkbox.demo--radioInput,.demo--radio:checked + .demo--checkbox.demo--radioInput:after{border-radius:0}
  </style>
</head>
<body>
  <div>
    <input  type="radio" name="demo-radio"> 我是radio
    <div></div>
    <label class="demo--label">
        <input class="demo--radio" type="radio" name="demo-radio">
        <span class="demo--radioInput"></span>我是radio
    </label>
    <div></div>
    <label class="demo--label">
        <input class="demo--radio" type="radio" name="demo-radio">
        <span class="demo--radioInput"></span>我是另一个radio
    </label>
</div>
</body>
</html>

  截图如下:

 

 附录: 第一个例子是用vue的一个方法,添加active ,然后,对这个有active的 元素下的子元素进行css描述。这个给当前元素添加class的方法解释,请挪步至另一篇笔记:

https://www.cnblogs.com/adouwt/p/7911639.html

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值