设为首页 加入收藏

TOP

纯 CSS 解决自定义 CheckBox 背景颜色问题
2019-09-03 01:34:53 】 浏览:17
Tags:CSS 解决 定义 CheckBox 背景 颜色 问题

CodePen

需要使用色 #ec6337(当然可以是任意颜色),解决问题:记住密码定制 CheckBox,解释全在注释里

未选中

选中

主要使用到 ::before 或 ::after 伪类处理,伪装成内部的那个勾

  • html
<label>
  <input type="checkbox" /> // 注意嵌在 label 里面
  记住密码
  <div class="show-box" /> // 注意嵌在 label 里面
</label>
  • CSS(LESS)
label {
  position: relative;
  cursor: pointer;

  input {
    cursor: pointer;
  }

  input:checked + .show-box {
    background: #ec6337;
  }

  .show-box {
    position: absolute;
    top: 0;
    left: 0;
    width: 16px;
    height: 16px;
    border-radius: 2px;
    border: 1px solid #d8d8d8;
    background: white; // 这里取个巧,与下面颜色一样而已

    &:before { // 使用了 absolute 所以无所谓是 before 还是 after
      content: ''; // 空白内容占位,当做盒模型处理,见下面
      position: absolute;
      top: 2px;
      left: 6px;
      width: 3px; // 勾的短边
      height: 8px; // 勾的长边
      border: solid white; // 勾的颜色
      border-width: 0 2px 2px 0; // 勾的宽度
      transform: rotate(45deg); // 定制宽高加上旋转可以伪装内部的白色勾
    }
  }

CodePen

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇github个人博客绑定单独阿里域名.. 下一篇【工具】sublime使用技巧

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目