【问题标题】:CSS - How to Style a Selected Radio Buttons Label?CSS - 如何为选定的单选按钮标签设置样式?
【发布时间】:2011-06-06 05:15:43
【问题描述】:

我想为单选按钮的选定标签添加样式:

HTML:

<div class="radio-toolbar">
 <label><input type="radio" value="all" checked>All</label>
 <label><input type="radio" value="false">Open</label>
 <label><input type="radio" value="true">Archived</label>
</div>

CSS

.radio-toolbar input[type="radio"] {display:none;}
.radio-toolbar label {
    background:Red;
    border:1px solid green;
    padding:2px 10px;
}
.radio-toolbar label + input[type="radio"]:checked { 
    background:pink !important;
}

任何想法我做错了什么?

【问题讨论】:

    标签: html css


    【解决方案1】:

    当元素不是同级时,您正在使用相邻同级选择器 (+)。标签是输入的 parent,而不是兄弟。

    CSS 无法根据它的后代(也没有任何跟随它的元素)来选择元素。

    您需要使用 JavaScript 来解决这个问题。

    或者,重新排列您的标记:

    <input id="foo"><label for="foo">…</label>
    

    【讨论】:

      【解决方案2】:

      .radio-toolbar input[type="radio"] {
        display: none;
      }
      
      .radio-toolbar label {
        display: inline-block;
        background-color: #ddd;
        padding: 4px 11px;
        font-family: Arial;
        font-size: 16px;
        cursor: pointer;
      }
      
      .radio-toolbar input[type="radio"]:checked+label {
        background-color: #bbb;
      }
      <div class="radio-toolbar">
        <input type="radio" id="radio1" name="radios" value="all" checked>
        <label for="radio1">All</label>
      
        <input type="radio" id="radio2" name="radios" value="false">
        <label for="radio2">Open</label>
      
        <input type="radio" id="radio3" name="radios" value="true">
        <label for="radio3">Archived</label>
      </div>

      首先,您可能希望在单选按钮上添加name 属性。否则,它们不属于同一组,并且可以选中多个单选按钮。

      此外,由于我将标签作为(单选按钮的)同级放置,我必须使用 idfor 属性将它们关联在一起。

      【讨论】:

      • @Johncl 是的。 IE8 没有实现:checked 选择器。
      • 如果我不想选择 input[type="radio"] 或 input[type="checkbox"]。仅在一个规则中执行此操作并避免执行此操作的正确语法是: input[type="radio"], input[type="checkbox"]
      • @fabregas88 你可以为这些元素添加一个 CSS 类,然后 .foo { ... }
      • 你刚刚杀死了键盘访问!希望您的所有用户都身体健康。 ...现在,如果您只保留单选按钮,而不使用 display:none;,那么浏览器将使其与键盘一起正常工作。
      • @gcb 问题是如何隐藏标准单选按钮。我已经制作了这个原型jsbin.com/miwati/edit?html,css,output。单选按钮绝对位于标签后面。不过可能不是最好的方法。
      【解决方案3】:

      如果您真的想将复选框放在标签内,请尝试添加额外的 span 标签,例如。

      HTML

      <div class="radio-toolbar">
       <label><input type="radio" value="all" checked><span>All</span></label>
       <label><input type="radio" value="false"><span>Open</span></label>
       <label><input type="radio" value="true"><span>Archived</span></label>
      </div>
      

      CSS

      .radio-toolbar input[type="radio"]:checked ~ * { 
          background:pink !important;
      }
      

      这将为所选单选按钮的所有兄弟设置背景。

      【讨论】:

      • 我更喜欢这种方法,尽管我使用了不同的选择器(下面提到的'+'和in this question,因为我只想设置所选项目的样式。这让我不必担心@987654324 @ 属性。
      【解决方案4】:

      您可以在 html 和 css 中添加跨度。

      这是我的代码中的一个示例......

      HTML(JSX):

      <input type="radio" name="AMPM" id="radiostyle1" value="AM" checked={this.state.AMPM==="AM"} onChange={this.handleChange}/>  
      <label for="radiostyle1"><span></span> am  </label>
      
      <input type="radio" name="AMPM" id="radiostyle2" value="PM" checked={this.state.AMPM==="PM"} onChange={this.handleChange}/>
      <label for="radiostyle2"><span></span> pm  </label>
      

      CSS 使标准单选按钮在屏幕上消失并叠加自定义按钮图像:

      input[type="radio"] {  
          opacity:0;                                      
      }
      
      input[type="radio"] + label {
          font-size:1em;
          text-transform: uppercase;
          color: white ;  
          cursor: pointer;
          margin:auto 15px auto auto;                    
      }
      
      input[type="radio"] + label span {
          display:inline-block;
          width:30px;
          height:10px;
          margin:1px 0px 0 -30px;                       
          cursor:pointer;
          border-radius: 20%;
      }
      
      
      input[type="radio"] + label span {
          background-color: #FFFFFF 
      }
      
      
      input[type="radio"]:checked + label span{
           background-color: #660006;  
      }
      

      【讨论】:

        【解决方案5】:

        由于目前没有 CSS 解决方案来设置父级样式,因此我在这里使用一个简单的 jQuery 将一个类添加到标签中,其中包含已检查的输入。

        $(document).on("change","input", function(){
         $("label").removeClass("checkedlabel");
         if($(this).is(":checked")) $(this).closest("label").addClass("checkedlabel");
        });
        

        不要忘记给预检查输入的标签也提供checkedlabel

        【讨论】:

          【解决方案6】:

          这是一个可访问的解决方案

          label {
            position: relative;
          }
          
          label input {
            position: absolute;
            opacity: 0;
          }
          
          label:focus-within {
            outline: 1px solid orange;
          }
          <div class="radio-toolbar">
            <label><input type="radio" value="all" checked>All</label>
            <label><input type="radio" value="false">Open</label>
            <label><input type="radio" value="true">Archived</label>
          </div>

          【讨论】:

            【解决方案7】:

            只需使用label:focus-within {} 为带有选中单选或复选框的标签设置样式。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-09-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-01-13
              • 1970-01-01
              • 2019-01-25
              • 1970-01-01
              相关资源
              最近更新 更多