Skip to content Skip to sidebar Skip to footer

Styling Radio Inputs

So I'm having major trouble with styling radio buttons and as such I've managed to get it working perfectly in Chrome, a tiny bit in Firefox and not at all in IE. This is the expec

Solution 1:

As noted in the comments, the :after and :before pseudo elements are inserted inside of the element that they are applied on. :after and :before shouldn't work with inputs as the <input> element cannot have children :(

The solution is to apply the images and :after pseudo element to the radio inputs label. From a semantics point of view it's also nice to give the label a value.

  • The radio inputs are hidden with display: none and they are selected via their corresponding label attached with the matching for and id attributes.

  • input + label targets only the label element directly after each input.

  • input + label:after and input:checked + label:after provide the selected opaque overlay and transition animation

Have an example!

HTML

<div id="welcome-gender"> 
    <input class="male" name="gender"type="radio" value="male"id="male-one">
    <label for="male-one">Male One</label>          

    <input class="female" name="gender"type="radio" value="female"id="female-one">
    <label for="female-one">Female One</label>
</div>

CSS

input[type="radio"]{
    display: none;
}
label {
    position: relative;
    width: 170px;
    height: 300px;
    display: inline-block;
    color: white;
    font-size: 0;
    border-radius: 4px;
}
.male + label {
    background: url(http://socialavatarnetworkscript.com/media/img/male_avatar.png);    
}
.female + label {
    background: url(http://socialavatarnetworkscript.com/media/img/female_avatar.png)
}

input + label:after {
    background: #FFF;
    content: '';    
}
input:checked + label:after {
    background: rgba(0, 0, 0, 0.8);
    position: absolute;
    width: 170px;
    height: 300px;
    display: block;
    content: 'SELECTED';
    border-radius: 4px;
    color: white;
    font-family: titillium, sans-serif;
    font-weight: 500;
    font-size: 22px;
    text-align: center;
    line-height: 300px;
    -webkit-transition: all 0.2s ease;
    -moz-transition: all 0.2s ease;
    -ms-transition: all 0.2s ease;
    -o-transition: all 0.2s ease;
    transition: all 0.2s ease;
}

Post a Comment for "Styling Radio Inputs"