美文网首页
只用CSS定义checkbox的样式以及mac下设置input[

只用CSS定义checkbox的样式以及mac下设置input[

作者: 丶灰太狼他叔 | 来源:发表于2018-01-16 01:18 被阅读143次

1.1、纯css通过伪元素来定义input输入框激活样式

HTML代码:

<body>
    <input type="checkbox" id="myCheck">
    <label for="myCheck"></label>
</body>

CSS伪元素实现代码:

#myCheck + label{
    background-color: white;
    border-radius: 5px;
    border:1px solid #d3d3d3;
    width:20px;
    height:20px;
    display: inline-block;
    text-align: center;
    vertical-align: middle;
    line-height: 20px;
}
#myCheck:checked + label{
    background-color: #eee;
}
#myCheck:checked + label:after{
    content:"\2713";
}

注:
1、加号为相邻兄弟选择器,即选择相邻的下一个兄弟元素
2、content:"\2713"为特殊符号对号的预留字符。值得一提的是html中的为10003和css,js中是不一样的。css和js中是一样的。更多特殊符号见:http://www.haorooms.com/post/html_tsfh
1、2开关样式的checkbox
不多说直接上代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>纯css编写开关按钮(二)</title>
    <style type="text/css">
        #toggle-button{ display: none; }
        .button-label{
            position: relative;
            display: inline-block;
            width: 80px;
            height: 30px;
            background-color: #ccc;
            box-shadow: #ccc 0px 0px 0px 2px;
            border-radius: 30px;
            overflow: hidden;
        }
        .circle{
            position: absolute;
            top: 0;
            left: 0;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background-color: #fff;
        }
        .button-label .text {
            line-height: 30px;
            font-size: 18px;
            text-shadow: 0 0 2px #ddd;
        }

        .on { color: #fff; display: none; text-indent: 10px;}
        .off { color: #fff; display: inline-block; text-indent: 34px;}
        .button-label .circle{
            left: 0;
            transition: all 0.3s;
        }
        .btn:checked + label.button-label .circle{
            left: 50px;
        }
        .btn:checked + label.button-label .on{ display: inline-block; }
        .btn:checked + label.button-label .off{ display: none; }
        .btn:checked + label.button-label{
            background-color: #5cb85c;
        }

    </style>
</head>
<body>

<div class="toggle-button-wrapper">
    <input type="checkbox" id="toggle-button1" name="switch" class="btn">
    <label for="toggle-button1" class="button-label">
        <span class="circle"></span>
        <span class="text on">开启</span>
        <span class="text off">关闭</span>
    </label>
</div>
<div class="toggle-button-wrapper">
    <input type="checkbox" id="toggle-button2" name="switch" class="btn">
    <label for="toggle-button2" class="button-label">
        <span class="circle"></span>
        <span class="text on">开启</span>
        <span class="text off">关闭</span>
    </label>
</div>
</body>
</html>

2、input type=submit设置高度无效的问题

经过测试input submit高度无效是由于在Mac上的Chrome造成的。就是说Mac上的其他浏览器是可以的。Windows上的Chrome也是可以的;解决办法就是给input submit添加背景颜色就可以解决了

相关文章

网友评论

      本文标题:只用CSS定义checkbox的样式以及mac下设置input[

      本文链接:https://www.haomeiwen.com/subject/bdpjoxtx.html