Embedding radio inputs
So I was having some serious problems with styling buttons and as such I managed to get it to work fine in Chrome, a tiny bit in Firefox and not at all in IE.
This is the expected view (in Chrome):
This is how it looks in Firefox:
And IE ...
I have a JSFiddle for you guys to play with, but I just can't figure out how to get this cross browser working. Any ideas?
JSFiddle: http://jsfiddle.net/s5rpd97b/
A must-have code paste, even though it's on a 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;
}
source to share
As noted in the comments, pseudo :after
- :before
elements are and are inserted inside the element they are applied to. :after
and :before
shouldn't work with inputs, since an element <input>
cannot have children :(
The solution is to apply images and a :after
pseudo element to the radio input label. Semantically, it's also nice to assign a value to a label.
-
The radio inputs are hidden with
display: none
, and they are selected through the appropriate label with the appropriate attributesfor
andid
. -
input + label
specifies only the label element immediately after each input. -
input + label:after
andinput:checked + label:after
provides a selected opaque overlay and transition animation
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;
}
source to share