zoukankan      html  css  js  c++  java
  • [HTML/CSS]自定义input[type="radio"]的样式

    对单选按钮自定义样式,我们以前一直用的脚本来实现,不过现在可以使用新的伪类 :checkbox 来实现。

    思路:

    1. 可以为<label>元素添加生成性内容(伪元素),并基于单选按钮的状态来为其设置样式;

    2. 然后把真正的单选按钮隐藏起来;

    3. 最后把生成内容美化一下。

    解决办法:

    1. 一段简单的结构代码:

    <div class="female">
        <input type="radio" id="female" name="sex" />
        <label for="female"></label>
    </div>
    <div class="male">                
        <input type="radio" id="male" name="sex" />
        <label for="male"></label>
    </div>

    2. 生成一个伪元素,作为美化版的单选按钮,先给伪元素添加一些样式:

    input[type="radio"] + label::before {
        content: "a0"; /*不换行空格*/
        display: inline-block;
        vertical-align: middle;
        font-size: 18px;
        width: 1em;
        height: 1em;
        margin-right: .4em;
        border-radius: 50%;
        border: 1px solid #01cd78;
        text-indent: .15em;
        line-height: 1; 
    }

    现在的样子:

    3. 给单选按钮的勾选状态添加不同的样式: 

    input[type="radio"]:checked + label::before {
        background-color: #01cd78;
        background-clip: content-box;
        padding: .2em;
    }

    现在的样子:

    4. 现在把原来的单选按钮隐藏:

    input[type="radio"] {
        position: absolute;
        clip: rect(0, 0, 0, 0);
    }

    现在的样子:

    P.S:

    隐藏原来的单选按钮时,如果使用 display: none; 的话,那样会把它从键盘 tab 键切换焦点的队列中完全删除。

    于是可采用剪切的方式,让剪切后的尺寸为零,这样就隐藏了原来的单选按钮。

    文章引用于 https://www.cnblogs.com/xinjie-just/p/5911086.html

  • 相关阅读:
    hdoj 4006 The kth great number【优先队列】
    hdoj 1509 Windows Message Queue【优先队列】
    nyoj 55 懒省事的小明【优先队列】
    hdoj 1896 Stones【优先队列】
    nyoj 757 期末考试【优先队列+贪心】
    hdoj 2147 kiki's game【博弈】
    hdoj 1873 看病要排队【优先队列】
    hdoj 1789 Doing Homework again
    nyoj 1036 非洲小孩【贪心区间选点】
    转:栈和队列小知识【STL用法】
  • 原文地址:https://www.cnblogs.com/SoYang/p/14806590.html
Copyright © 2011-2022 走看看