【问题标题】:How to change style of radio and checkbox input [duplicate]如何更改单选和复选框输入的样式[重复]
【发布时间】:2014-09-11 17:33:29
【问题描述】:

我有一个网站,我正在尝试更改单选按钮圆点的背景颜色。现在它似乎是透明的,所以它可以得到任何背景的颜色。例如,我尝试使用 CSS 并设置 "background: white;",但这在浏览器中没有任何效果。有什么很酷的技巧可以用来实现这一点吗?

同样的问题也代表复选框。

【问题讨论】:

    标签: html css


    【解决方案1】:

    jsBin demo

    此技术使用绑定到隐藏input 元素的label 元素,接收:checked 状态将改变:before 伪元素的外观:

    /* COMMON RADIO AND CHECKBOX STYLES  */
    input[type=radio],
    input[type=checkbox]{
      /* Hide original inputs */
      visibility: hidden;
      position: absolute;
    }
    input[type=radio] + label:before,
    input[type=checkbox] + label:before{
      height:12px;
      width:12px;
      margin-right: 2px;
      content: " ";
      display:inline-block;
      vertical-align: baseline;
      border:1px solid #777;
    }
    input[type=radio]:checked + label:before,
    input[type=checkbox]:checked + label:before{
      background:gold;
    }
    
    /* CUSTOM RADIO AND CHECKBOX STYLES */
    input[type=radio] + label:before{
      border-radius:50%;
    }
    input[type=checkbox] + label:before{
      border-radius:2px;
    }
    <input type="radio" name="r" id="r1"><label for="r1">Radio 1</label>
    <input type="radio" name="r" id="r2"><label for="r2">Radio 2</label>
    
    <input type="checkbox" name="c1" id="c1"><label for="c1">Check 1</label>
    <input type="checkbox" name="c2" id="c2"><label for="c2">check 2</label>  

    【讨论】:

    • 很好的提示,谢谢!
    • 这是一个绝妙的技巧。做得很好。
    【解决方案2】:

    您无法更改浏览器生成的控件的每个细节,这一点已得到充分证实。例如选择下拉菜单上箭头的颜色,或收音机的圆点等...

    您可以创建自定义控件,使用 JQuery UI 之类的库,或者....也许可以玩一下 css。

    这是一个使用:before伪元素在收音机上伪造彩色圆点的实验:

    http://jsfiddle.net/bvtngh57/

    input[type="radio"]:checked:before {
        content: "";
        display: block;
        position: relative;
        top: 3px;
        left: 3px;
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background: red;
    }
    

    结果:

    【讨论】:

    • 看起来很酷,可惜没有在 FF 上工作
    • 意外伪元素适用于基于 Webkit 的网络浏览器上的 &lt;input&gt; 元素。
    • @HashemQolami 谢天谢地,这个问题最近得到了修复。伪元素不再适用于 webkit 上的输入。
    • 当给定的 PSD 有相同的收音机 btn 时有多酷:D
    【解决方案3】:

    使用 CSS 设置复选框和单选按钮的非标签元素样式的首选方法是用代表当前状态(未选中、选中等)的图像替换它们。

    请参阅 Ryan Seddon 的这篇文章:http://www.thecssninja.com/css/custom-inputs-using-css

    【讨论】:

      【解决方案4】:

      浏览器本身处理单选按钮和复选框的外观,以及下拉/选择。但是,您可以隐藏单选按钮,将它们替换为图像,然后使用 jQuery 修改单选/检查值。 Font Awesome (http://fortawesome.github.io/Font-Awesome/icons/) 有一些很酷的图标可供您使用。

      Here is a demo

      <div>
          Radio 1 - 
          <input type="radio" name="radio" class="radio" value="1" />
          <span class="red fa fa-circle-o"></span>
      </div>
      <div>
          Radio 2 - 
          <input type="radio" name="radio" class="radio" value="2" />
          <span class="blue fa fa-circle-o"></span>
      </div>
      
      $('span.fa').on('click', function() {
          $('span.fa').removeClass('fa fa-dot-circle-o').addClass('fa fa-circle-o');
          $(this).removeClass('fa-circle-o').addClass('fa-dot-circle-o');
      
          //Check corresponding hidden radio
          $(this).prev('input.radio').prop('checked', true);
      });
      

      【讨论】:

        【解决方案5】:

        有很多方法可以做到这一点,所有这些都涉及摆脱 DOM 样式,因为没有这些“技巧”是不可能做到的。这是Chris Coyier 的示例(只需阅读页面,跳过第 1 步,只需准备图片和 CSS)

        /*
            Hide the original radios and checkboxes
            (but still accessible)
        
            :not(#foo) > is a rule filter to block browsers
                         that don't support that selector from
                         applying rules they shouldn't
        
        */
        li:not(#foo) > fieldset > div > span > input[type='radio'],
        li:not(#foo) > fieldset > div > span > input[type='checkbox'] {
        
            /* Hide the input, but have it still be clickable */
            opacity: 0;
        
            float: left;
            width: 18px;
        }
        
        li:not(#foo) > fieldset > div > span > input[type='radio'] + label,
        li:not(#foo) > fieldset > div > span > input[type='checkbox'] + label {
            margin: 0;
            clear: none;
        
            /* Left padding makes room for image */
            padding: 5px 0 4px 24px;
        
            /* Make look clickable because they are */
            cursor: pointer;
        
            background: url(off.png) left center no-repeat;
        }
        
        /*
            Change from unchecked to checked graphic
        */
        li:not(#foo) > fieldset > div > span > input[type='radio']:checked + label {
            background-image: url(radio.png);
        }
        li:not(#foo) > fieldset > div > span > input[type='checkbox']:checked + label {
            background-image: url(check.png);
        }
        

        【讨论】:

          【解决方案6】:

          您可以单独使用 css 设置离子单选按钮的样式。检查小提琴:

          https://jsfiddle.net/sreekanthjayan/0d9vj86k/

               <div>
                    <ion-radio class="radio radio-inline radio-gray" ng-model="choice" ng-value="'A'">iOS</ion-radio>
                    <ion-radio class="radio radio-inline radio-teal" ng-model="choice" ng-value="'B'">Android</ion-radio>
                    <ion-radio class="radio radio-inline radio-blue" ng-model="choice" ng-value="'C'">Windows Phone</ion-radio>
               </div>
          
          
          .radio .radio-icon {
            visibility: visible !important;
          }
          
          .radio .radio-icon:before {
            content: "" !important;
            border: 2px solid black !important;
            width: 24px !important;
            height: 24px !important;
            border-radius: 50% !important;
            overflow: hidden !important;
          }
          
          .radio .radio-icon:after {
            content: "" !important;
            position: absolute !important;
            right: 20px !important;
            top: 22px !important;
            background: black !important;
            width: 12px !important;
            height: 12px !important;
            border-radius: 50% !important;
            overflow: hidden !important;
            transition: -webkit-transform .28s cubic-bezier(0.420, 0.000, 0.000, 1.650);
            transition: transform .28s cubic-bezier(0.420, 0.000, 0.000, 1.650);
            -webkit-transform: scale(0);
            transform: scale(0);
          }
          
          .radio.item-radio > input[type=radio]:checked ~ .radio-icon:after {
            -webkit-transform: scale(1);
            transform: scale(1);
          }
          
          .radio .item-content {
            background-color: #fff;
            margin: 0;
            padding-right: 50px;
            padding-left: 0px;
          }
          
          .radio.item-radio > input[type=radio]:checked ~ .item-content {
            background-color: #fff;
          }
          
          .radio-inline.item {
            display: inline-block;
            border: none;
            margin: 0;
            height: 50px;
          }
          
          .radio-blue .radio-icon:after {
            background: #2196F3 !important;
          }
          
          .radio-blue .radio-icon:before {
            border-color: #2196F3 !important;
          }
          
          .radio-teal .radio-icon:after {
            background: #009688 !important;
          }
          
          .radio-teal .radio-icon:before {
            border-color: #009688 !important;
          }
          
          .radio-gray .radio-icon:after {
            background: #B6B6B6 !important;
          }
          
          .radio-gray .radio-icon:before {
            border-color: #B6B6B6 !important;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-02-13
            • 2014-03-04
            • 2023-02-22
            • 1970-01-01
            • 2010-11-10
            • 2012-11-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多