Given HTML similar to the following:
<label>
<input type="radio" name="group1" /><span class="radio1"></span>label 1 text</label>
<label>
<!-- and so on... -->
The following CSS allows you to add a class to resize the 'fake' radio
element:
/* Hiding the radio inputs: */
input[type=radio] {
display: none;
}
/* styling the spans adjacent to the radio inputs: */
input[type=radio] + span {
display: inline-block;
border: 1px solid #000;
border-radius: 50%;
margin: 0 0.5em;
}
/* styling the span following the checked radio: */
input[type=radio]:checked + span {
background-color: #ffa;
}
/* defining the height/width for the span-radio: */
.radio1 {
width: 0.5em;
height: 0.5em;
}
/* and so on... */
JS Fiddle demo.
The above version does require an added element, which feels unnecessary, but allows for the omission of the for
and id
attributes from the label
and input
elements (respectively). If you're able to use those attributes, however, then the span
elements aren't necessary, which allows HTML such as the following:
<input id="r1" type="radio" name="group1" class="radio1" />
<label for="r1">label 1 text</label>
With the CSS:
input[type=radio] {
display: none;
}
/* Creating the psuedo-element to replace the radio inputs: */
input[type=radio] + label::before {
content: '';
display: inline-block;
border: 1px solid #000;
border-radius: 50%;
margin: 0 0.5em;
}
/* styling the checked 'radio': */
input[type=radio]:checked + label::before {
background-color: #ffa;
}
/* defining the height/width of the 'radio' according to the class of
the radio input element: */
.radio1 + label::before {
width: 0.5em;
height: 0.5em;
}
/* and so on... */
JS Fiddle demo.
It goes without saying, of course, that these approaches require browsers that understand the :checked
pseudo-class and, in the event of the latter approach, a browser that implements psuedo-elements. With that in mind, sadly, neither of these approaches can be used in Internet Explorer before version nine (when it began to implement the :checked
selector).
References:
- 'CSS Selectors,' at Quirksmode.