3

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 expected look (in Chrome): enter image description here

This is how it looks in Firefox: enter image description here

And IE...

enter image description here

I've got a JSFiddle for you guys to play around with, but I just can't seem to figure out how to get this working cross-browser. Any ideas?

JSFiddle: http://jsfiddle.net/s5rpd97b/

Obligatory paste of code despite the fact it's on JSFiddle anyway:

HTML:

   <input style="background: url(http://socialavatarnetworkscript.com/media/img/male_avatar.png);margin-right: 240px;" class="gender" name="gender" type="radio" value="male">
   <input style="background: url(http://socialavatarnetworkscript.com/media/img/female_avatar.png)" class="gender" name="gender" type="radio" value="female">

CSS:

input[type="radio"].gender::after{
    background: rgba(0, 0, 0, 0.01);
    position: absolute;
    width: 170px;
    height: 300px;
    display: block;
    content: '';
    color: white;
    font-size: 1px;
    -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;
}
input[type="radio"].gender:checked::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;
}
input[type="radio"]{
    display: inline-block;
    -webkit-appearance: none;
    -moz-appearance:    none;
    -ms-appearance:     none;
    appearance:         none;
    border-radius: 4px  !important;
    outline: none       !important;
    border: none        !important;
}
input[type="radio"].gender{
    height: 300px;
    width: 170px;
    border-radius: 4px !important;
    outline: none !important;
    border: none !important;
}
Tom Wilson
  • 253
  • 1
  • 7
  • 15
  • Possibly related: http://stackoverflow.com/q/12831620/1127114 – Michael Liu Aug 17 '14 at 02:48
  • Simply don't use *form elements* like `input` for `:after` and `:before`. – Roko C. Buljan Aug 17 '14 at 02:48
  • `::after` is supposed to be rendered as if a new _child_ element was inserted into the element that it is applied upon – but `input` elements can’t have children. And formatting radio buttons cross-browser is tricky in any case. For more reliable results I’d suggest you use an extra element after each input, and use the adjacent sibling combinator `+` to format those depending on the input’s state. – CBroe Aug 17 '14 at 02:49
  • 1
    I suppose doing this with a label is the most viable alternative? – Tom Wilson Aug 17 '14 at 02:54
  • It's a really bad practice to mix inline and external styles.... Because it'll be a pain for somebody else going through your code... – T J Aug 17 '14 at 03:53
  • You get my special star of the day! * – Tom Wilson Aug 17 '14 at 16:58

1 Answers1

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;
}
misterManSam
  • 24,303
  • 11
  • 69
  • 89