使用CSS3美化复选框checkbox

通过css3伪元素实现以下样式效果checkbox复选框

image.png

html部分

<div id="header">
   <div id="logo"><h1><a href="http://www.helloweba.com" title="返回helloweba首页">helloweba</a></h1></div>
</div>

<div id="main">
   <h2 class="top_title"><a href="http://www.helloweba.com/view-blog-295.html">使用CSS3美化复选框checkbox</a></h2>
    <div class="demo">
        <div class="wrap">
            <p>1. 勾选</p>
            <input type="checkbox" id="checkbox_a1" class="chk_1" checked /><label for="checkbox_a1"></label>
            <input type="checkbox" id="checkbox_a2" class="chk_1" /><label for="checkbox_a2"></label>
        </div>
        <div class="wrap" style="width:198px">
            <p>2. 单按钮</p>
            <input type="checkbox" id="checkbox_b1" class="chk_2" /><label for="checkbox_b1">我同意</label>
        </div>
        
        <div class="wrap" style="width: 180px;">
            <p>3. 移动端开关</p>
            <input type="checkbox" id="checkbox_c1" class="chk_3" /><label for="checkbox_c1"></label>
            <input type="checkbox" id="checkbox_c2" class="chk_3" checked /><label for="checkbox_c2"></label>
        </div>
        
        <div class="wrap" style="width: 231px;">
            <p>4. 开启和关闭</p>
            <input type="checkbox" id="checkbox_d1" class="chk_4" /><label for="checkbox_d1"></label>
            <input type="checkbox" id="checkbox_d2" class="chk_4" checked /><label for="checkbox_d2"></label>
        </div>
    </div>
</div>

css部分

.chk_1,.chk_2,.chk_3,.chk_4 {
    display: none;
}
 
/*******STYLE 1*******/
.chk_1 + label {
    background-color: #FFF;
    border: 1px solid #C1CACA;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
    padding: 9px;
    border-radius: 5px;
    display: inline-block;
    position: relative;
    margin-right: 30px;
}
.chk_1 + label:active {
    box-shadow: 0 1px 2px rgba(0,0,0,0.05), inset 0px 1px 3px rgba(0,0,0,0.1);
}

.chk_1:checked + label {
    background-color: #ECF2F7;
    border: 1px solid #92A1AC;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 15px 10px -12px rgba(255, 255, 255, 0.1);
    color: #243441;
}

.chk_1:checked + label:after {
    content: '\2714';
    position: absolute;
    top: 0px;
    left: 0px;
    color: #758794;
    width: 100%;
    text-align: center;
    font-size: 1.4em;
    padding: 1px 0 0 0;
    vertical-align: text-top;
}


/*******STYLE 2*******/
.chk_2 + label {
    background-color: #F37900;
    padding: 18px 20px 18px 23px;
    box-shadow: inset 0 50px 37px -30px rgba(255, 222, 197, 0.3), 0 0 13px rgba(0, 0, 0, 0.6);
    border-radius: 1000px;
    display: inline-block;
    position: relative;
    border-top: 1px solid #ECA14F;
    margin-right: 30px;
    color: #FFF;
    font-size: 1.7em;
    width: 113px;
    text-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
    border-bottom: 1px solid #552B09;
}

.chk_2 + label:hover  {
    border-top: 1px solid #FC8C1E;
    background: #FC8C1E;
    box-shadow: inset 0 -50px 37px -30px rgba(255, 222, 197, 0.07), 0 0 13px rgba(0, 0, 0, 0.6);
}

.chk_2 + label:active  {
    border-top: none;
    background: #FC8C1E;
    padding: 19px 20px 18px 23px;
    box-shadow: inset 0 3px 8px rgba(129, 69, 13, 0.3), inset 0 -50px 37px -30px rgba(255, 222, 197, 0.07), 0 0 13px rgba(0, 0, 0, 0.6);    
}

.chk_2 + label:after {
    content: ' ';
    border-radius: 100px;
    width: 32px;
    position: absolute;
    top: 12px;
    right: 12px;
    box-shadow: inset 0px 16px 40px rgba(0, 0, 0, 0.4);
    height: 32px;
}

.chk_2 + label:before {
    content: ' ';
    border-radius: 100px;
    width: 20px;
    position: absolute;
    top: 18px;
    right: 18px;
    z-index: 999;
    box-shadow: inset 0px 16px 40px #FFF;
    height: 20px;
    display: none;
}

.chk_2:checked + label:before {
    display: block;
}

/*******STYLE 3*******/

.chk_3 + label {
    background-color: #fafbfa;
    padding: 9px;
    border-radius: 50px;
    display: inline-block;
    position: relative;
    margin-right: 30px;
    -webkit-transition: all 0.1s ease-in;
    transition: all 0.1s ease-in;
    width: 40px;
    height: 15px;
}

.chk_3  + label:after {
    content: ' ';
    position: absolute;
    top: 0;
    -webkit-transition: box-shadow 0.1s ease-in;
    transition: box-shadow 0.1s ease-in;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 100px;
    box-shadow: inset 0 0 0 0 #eee, 0 0 1px rgba(0,0,0,0.4);
}

.chk_3  + label:before {
    content: ' ';
    position: absolute;
    background: white;
    top: 1px;
    left: 1px;
    z-index: 999999;
    width: 31px;
    -webkit-transition: all 0.1s ease-in;
    transition: all 0.1s ease-in;
    height: 31px;
    border-radius: 100px;
    box-shadow: 0 3px 1px rgba(0,0,0,0.05), 0 0px 1px rgba(0,0,0,0.3);
}

.chk_3:active + label:after {
    box-shadow: inset 0 0 0 20px #eee, 0 0 1px #eee;
}

.chk_3:active + label:before {
    width: 37px;
}

.chk_3:checked:active + label:before {
    width: 37px;
    left: 20px;
}

.chk_3  + label:active {
    box-shadow: 0 1px 2px rgba(0,0,0,0.05), inset 0px 1px 3px rgba(0,0,0,0.1);
}

.chk_3:checked + label:before {
    content: ' ';
    position: absolute;
    left: 26px;
    border-radius: 100px;
}

.chk_3:checked + label:after {
    content: ' ';
    font-size: 1.5em;
    position: absolute;
    background: #4cda60;
    box-shadow: 0 0 1px #4cda60;
}


/*******STYLE 4*******/
.chk_4 + label {
    background-color: #FFF;
    padding: 11px 9px;
    border-radius: 7px;
    display: inline-block;
    position: relative;
    margin-right: 30px;
    background: #F7836D;
    width: 58px;
    height: 10px;
    box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.1), 0 0 10px rgba(245, 146, 146, 0.4);
}

.chk_4 + label:before {
    content: ' ';
    position: absolute;
    background: #FFF;
    top: 0px;
    z-index: 99999;
    left: 0px;
    width: 24px;
    color: #FFF;
    height: 32px;
    border-radius: 7px;
    box-shadow: 0 0 1px rgba(0,0,0,0.6);
}

.chk_4 + label:after {
    content: '关闭';  
    position: absolute;
    top: 7px;
    left: 37px;
    font-size: 1.2em;
    color: white;
    font-weight: bold;
    left: 8px;
    padding: 5px;
    top: 4px;
    border-radius: 100px;
}

.chk_4:checked + label {
    background: #67A5DF;
    box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.1), 0 0 10px rgba(146, 196, 245, 0.4);
}

.chk_4:checked + label:after {
    content: '开启';
    left: 10px;
}

.chk_4:checked + label:before {
    content: ' ';
    position: absolute;
    z-index: 99999;
    left: 52px;
}


.chk_4 + label:after {
    left: 35px; 
}   

#checkbox_d2 + label:after, #checkbox_d2 + label:before, #checkbox label {  
    -webkit-transition: all 0.1s ease-in;
    transition: all 0.1s ease-in;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,192评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,186评论 1 303
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,844评论 0 252
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,471评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,876评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,891评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,068评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,791评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,539评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,772评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,250评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,577评论 3 260
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,244评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,146评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,949评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,995评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,812评论 2 276

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,634评论 1 92
  • 一:在制作一个Web应用或Web站点的过程中,你是如何考虑他的UI、安全性、高性能、SEO、可维护性以及技术因素的...
    Arno_z阅读 1,095评论 0 1
  • Bootstrap是什么? 一套易用、优雅、灵活、可扩展的前端工具集--BootStrap。GitHub上介绍 的...
    凛0_0阅读 10,694评论 3 184
  • Compilation failed; see the compiler error output for det...
    敲代码的小新阅读 1,512评论 0 0
  • 这周早上起的愈加晚了,又出现了打卡晚的情况,没坚持到最后,阵亡了。虽然存在客观原因,但是打卡群已经给了自己两次免死...
    狐狸王阅读 412评论 0 50