CSS - Custom styling on radio button

谁都会走 提交于 2019-12-24 08:39:00

问题


I have a jsfiddle here - https://jsfiddle.net/5qmnptuk/4/

I'm trying to craete a custom radio button.

Ive hidden the radio button and then created the new one with :after on the label.

I'm trying to use :checked to style the clicked but its not working

Can anyone see why this doesn't work.

    .form-group{
        margin-top: 30px;
    }

    label{
        cursor: pointer;
        display: inline-block;
        position: relative;
        padding-right: 25px;
        margin-right: 15px;
    }

    label:after{
        bottom: -1px;
        border: 1px solid #aaa;
        border-radius: 25px;
        content: "";
        display: inline-block;
        height: 25px;
        margin-right: 25px;
        position: absolute;
        right: -31px;
        width: 25px;
    }

    input[type=radio]{
        display: none;
    }

    input[type=radio]:checked + label{
        content: \2022;
        color: red;
        font-size: 30px;
        text-align: center;
        line-height: 18px; 
    }

回答1:


There are several problems with your code:

1 - Your input needs to be before the label

The + and ~ CSS selectors only select siblings after an element in the DOM, so you need to have the input before the label.

2 - Your label isn't assigned to the input

To assign the label, use the for attribute, and give the input an ID:

  <div class="form-group">
    <input type="radio" id="custom" />
    <label for="custom">Label</label>
  </div>

3 - The content for the pseudo element is missing quotes.

4 - You aren't applying the styles to the after element of the label, you were applying them to the label directly:

 input[type=radio]:checked + label{
    content: \2022;
    color: red;
    font-size: 30px;
    text-align: center;
    line-height: 18px; 
}

Selects the label, while:

input[type=radio]:checked + label:after{
    content: "\2022";
    color: red;
    font-size: 30px;
    text-align: center;
    line-height: 18px; 
}

Selects the after element

Updated, working JSFiddle




回答2:


Updated fiddle where I've added id for the radio and label needs

for="radioId" 

to be able to work like that

also the label needs to be after the radio button



来源:https://stackoverflow.com/questions/38384538/css-custom-styling-on-radio-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!