用于多个提示的单选按钮自定义样式

时间:2019-07-08 22:36:27

标签: html css radio-button radio

基本上,我正在尝试创建自己的单选按钮组件以进行反应并再次使用,但是我正在努力使按钮与样式一起正常使用。如果您在第二组按钮中选择一个按钮,则即使每个按钮组都有不同的name属性,该按钮也无法正常反应。如果我摆脱了自定义样式,则可以正常工作。

我认为这与此有关,但尚未找到解决方案:

.radio-custom {
    opacity: 0;
    position: absolute; 
}

这是我的codepen:

https://codepen.io/Sbphillips19/pen/XLyzzN

HTML:

  <form>
        <h2>Radio Button Prompt 1</h2>
        <div>
            <input id="radio-1" class="radio-custom" name="radio-group" type="radio">
            <label for="radio-1" class="radio-custom-label">First Choice</label>
        </div>
        <div>
            <input id="radio-2" class="radio-custom"name="radio-group" type="radio">
            <label for="radio-2" class="radio-custom-label">Second Choice</label>
        </div>
        <div>
            <input id="radio-3" class="radio-custom" name="radio-group" type="radio">
            <label for="radio-3" class="radio-custom-label">Third Choice</label>
        </div>
      </div>



 <h2>Radio Button Prompt 2</h2>
        <div>
            <input id="radio-1" class="radio-custom" name="radio-group-2" type="radio">
            <label for="radio-1" class="radio-custom-label">First Choice</label>
        </div>
        <div>
            <input id="radio-2" class="radio-custom"name="radio-group-2" type="radio">
            <label for="radio-2" class="radio-custom-label">Second Choice</label>
        </div>
        <div>
            <input id="radio-3" class="radio-custom" name="radio-group-2" type="radio">
            <label for="radio-3" class="radio-custom-label">Third Choice</label>
        </div>
      </div>
    </form>

和CSS:

.radio-custom {
    opacity: 0;
    position: absolute; 
}

.radio-custom, .radio-custom-label {
    display: inline-block;
    vertical-align: middle;
    margin: 5px;
    cursor: pointer;
}

.checkbox-custom-label, .radio-custom-label {
    position: relative;
}

.radio-custom + .radio-custom-label:before {
    content: '';
    background: white;
    border: 2px solid #888888;
    display: inline-block;
    vertical-align: middle;
    width: 44px;
    height: 44px;
    padding: 2px;
    margin-right: 10px;
    text-align: center;
  border-radius: 50%;
}

.radio-custom:checked + .radio-custom-label:before {
    background: #444444;
    box-shadow: inset 0px 0px 0px 6px #fff;
}

1 个答案:

答案 0 :(得分:1)

问题在于attr id应该是唯一的!

html的以下第二部分更改为此,它将起作用:

工作示例:https://codepen.io/jo-o-teixeira-the-sasster/pen/agQErM

单选按钮提示2

<div>
        <input id="radio-4" class="radio-custom" name="radio-group-2" type="radio">
        <label for="radio-4" class="radio-custom-label">First Choice</label>
    </div>
    <div>
        <input id="radio-5" class="radio-custom"name="radio-group-2" type="radio">
        <label for="radio-5" class="radio-custom-label">Second Choice</label>
    </div>
    <div>
        <input id="radio-6" class="radio-custom" name="radio-group-2" type="radio">
        <label for="radio-6" class="radio-custom-label">Third Choice</label>
    </div>
  </div>