表单radio单选框美化

话不多说,直接上图:


13130960-58435569e90ee5b0.png
image.png

核心思想是隐藏input元素,利用css伪元素before和after来模拟外部圆圈和内部实心圆。
巧妙运用label元素的for属性与input联动特性,再通过input的checked属性来控制内部实心圆的显示/透明

HTML DOM结构:

  <label for="onlySelf">
    <input type="radio" name="power" id="onlySelf">
    <span>仅自己可见</span>
  </label>
  <label for="onlyDepa">
    <input type="radio" name="power" id="onlyDepa">
    <span>仅部门可见</span>
  </label>

css:

<style>
    label {
      line-height: 30px;
      display: flex;
      align-items: center;
      margin-right: 25px;
      font-size: 14px;
      color: #666;
    }

    label span {
      display: inline-block;
      height: 30px;
      line-height: 30px;
      padding-left: 24px;
      position: relative;
      cursor: pointer;
    }

    label span:before {
      content: '';
      position: absolute;
      width: 18px;
      height: 18px;
      border: 1px solid #999;
      box-sizing: border-box;
      border-radius: 50%;
      left: 0;
      top: 6px;
    }

    label span:after {
      content: '';
      position: absolute;
      width: 10px;
      height: 10px;
      background-color: #09f;
      box-sizing: border-box;
      border-radius: 50%;
      left: 4px;
      top: 10px;
      opacity: 0;
    }

    label input {
      display: none;
    }

    label input:checked~span:before {
      border-color: #09f;
      transition: border-color 0.5s ease-in;
    }

    label input:checked~span:after {
      opacity: 1;
      transition: opacity 0.3s ease-in;
    }
  </style>

猜你喜欢

转载自blog.csdn.net/weixin_34258078/article/details/90999812
今日推荐