【问题标题】:How to display radio buttons as <div>如何将单选按钮显示为 <div>
【发布时间】:2020-06-01 01:13:16
【问题描述】:

我想显示我的单选按钮,以便我可以提供样式表并使div 可点击。

这是我的单选按钮。

我想像这样向他们展示。

div 作为收音机

这是我尝试过但没有奏效的方法。

HTML

<div class="radio-toolbar">
    <table>
        <tr>
            <td><input type="radio" id="radioone" name="product" value="first">
                <label for="radioone">50</label></td>

            <td><input type="radio" id="radiotwo" name="product" value="second">
                <label for="radiotwo">100</label></td>

            <td><input type="radio" id="radiothree" name="product" value="third">
                <label for="radiothree">500</label></td>
</div>

CSS

.radio-toolbar input[type='radio']:focus + label {
  border: 2px thin blue;
}
.radio-toolbar input[type='radio']:checked + label {
  background-color: #86b1f7;
  border-color: #4c4;
  color: white;
}

那么我怎样才能做到这一点 小的帮助将不胜感激。 在此先感谢:)

【问题讨论】:

    标签: html css radio-button


    【解决方案1】:

    试试这样。让您的无线电工具栏输入width: 0;

    也让你的收音机工具栏标签像这样

    .radio-toolbar label {
    
        background-color: #A9A9A9;
        padding: 10px 20px;
        font-size: 16px;
        border-radius: 4px;
    }
    

    【讨论】:

      【解决方案2】:

      你需要隐藏单选按钮,并且左边的标签仍然是“可点击的”

      .radio-toolbar input[type="radio"] {
        display: none;
      }
      

      我把所有的工作代码放在 codepen 上:https://codepen.io/stormingorman-the-vuer/pen/ExjKRvz

      【讨论】:

        【解决方案3】:

        这是自定义单选按钮的一种方法。

        .radio-toolbar .radio-item {
          position: relative;
          display: inline-block;
          vertical-align: top;
          margin: 15px 5px;
        }
        
        .radio-item input[type="radio"] {
          position: absolute;
          width: 0;
          height: 0;
          top: 0;
          left: 0;
        }
        .radio-item label {
          padding: 10px;
          color: #fff;
          border-radius: 10px;
          background-color: grey;
          border: 1px solid #000;
        }
        
        .radio-item input[type="radio"]:checked ~ label {
          border-color: #f00;
        }
        <div class="radio-toolbar">
          <div class="radio-item">
            <input type="radio" id="radioone" name="product" value="first">
            <label for="radioone">50</label>
          </div>
          <div class="radio-item">
            <input type="radio" id="radiotwo" name="product" value="second">
            <label for="radiotwo">100</label>
          </div>
          <div class="radio-item">
            <input type="radio" id="radiothree" name="product" value="third">
            <label for="radiothree">500</label>
          </div>
        </div>

        如果这有帮助,请告诉我。

        【讨论】:

          【解决方案4】:

          我以前做过这个。我有代码准备。我希望它对你有用。

          .switch-field {
              display: flex;
              overflow: hidden;
              direction: ltr;
              float: right;
              text-align:right;
          }
          
          .switch-field input {
              position: absolute !important;
              clip: rect(0, 0, 0, 0);
              height: 1px;
              width: 1px;
              border: 0;
              overflow: hidden;
          }
          
          .switch-field label {
              /*width: 33.33%;*/
              background-color: transparent;
              color: #666666;
              font-size: 14px;
              line-height: 1;
              text-align: center;
              padding: 10px 12px;
              margin-left: 0px;
              /*margin-right: -1px;*/
              border: 1px solid #ddd;
              transition: all 0.1s ease-in-out;
          }
          
          .switch-field label:hover {
              cursor: pointer;
          }
          
          .switch-field input:checked + label {
              background-color: #F4F5F9;
              box-shadow: none;
              border-color: #3E7DE7;
              color: #3E7DE7;
          }
          
          .switch-field label:first-of-type {
              border-radius: 2px 0px 0px 2px;
          
          }
          
          .switch-field label:last-of-type {
              border-radius: 0px 2px 2px 0px;
          
          }
          <div class="switch-field">
            <input class="uk-radio" id="radio-six" type="radio" value="admin" name="user_type" checked>
            <label for="radio-six">admin</label>
             <input class="uk-radio" id="radio-seven" type="radio" value="user" name="user_type" >
             <label for="radio-seven">user</label>
          </div>

          【讨论】:

            猜你喜欢
            • 2012-09-05
            • 1970-01-01
            • 2015-08-24
            • 2014-02-06
            • 2018-08-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-07-14
            相关资源
            最近更新 更多