Is it possible to apply a css(3) style to a label of a checked radio button?
I have the following markup:
<input type="radio" id="rad" name="radio"/>
<label for="rad">A Label</label>
What I was hoping is that
label:checked { font-weight: bold; }
would do something, but alas it does not (as I expected).
Is there a selector that can achieve this sort of functionality? You may surround with divs etc if that helps, but the best solution would be one that uses the label ''for'' attribute.
It should be noted that I am able to specify browsers for my application, so best of class css3 etc please.
You could use a bit of jQuery:
You'd need to make sure your checked radio buttons have the correct class on page load as well.
I know this is an old question, but if you would like to have the
<input>
be a child of<label>
instead of having them separate, here is a pure CSS way that you could accomplish it:Then just wrap the text with a
<span>
:See it on JSFiddle.
I forget where I first saw it mentioned but you can actually embed your labels in a container elsewhere as long as you have the
for=
attribute set. So, let's check out a sample on SO:Whew. That was a lot for a "sample" but I feel it really drives home the effect and point: we can certainly select a label for a checked input control without it being a sibling. The secret lies in keeping the
input
tags a child to only what they need to be (in this case - only thebody
element).Since the
label
element doesn't actually utilize the:checked
pseudo selector, it doesn't matter that the labels are stored in theheader
. It does have the added benefit that since theheader
is a sibling element we can use the~
generic sibling selector to move from theinput[type=radio]:checked
DOM element to theheader
container and then use descendant/child selectors to access thelabel
s themselves, allowing the ability to style them when their respective radio boxes/checkboxes are selected.Not only can we style the labels, but also style other content that may be descendants of a sibling container relative to all of the
input
s. And now for the moment you've all been waiting for, the JSFIDDLE! Go there, play with it, make it work for you, find out why it works, break it, do what you do!Hopefully that all makes sense and fully answers the question and possibly any follow ups that may crop up.
try the
+
symbol: It is Adjacent sibling combinator. It combines two sequences of simple selectors having the same parent and the second one must come IMMEDIATELY after the first.As such:
works very nicely for the following markup:
... and it will work for any structure, with or without divs etc as long as the label follows the radio input.
Example:
If your input is a child element of the
label
and you have more than one labels, you can combine @Mike's trick withFlexbox
+order
.html css
See it on JSFiddle.
note: Sibling selector only works within the same parent. To work around this, you can make the input hidden at top-level using @Nathan Blair hack.
UPDATE:
This only worked for me because our existing generated html was wacky, generating
label
s along withradio
s and giving them bothchecked
attribute.Never mind, and big ups for Brilliand for bringing it up!
If your label is a sibling of a checkbox (which is usually the case), you can use the
~
sibling selector, and alabel[for=your_checkbox_id]
to address it... or give the label an id if you have multiple labels (like in this example where I use labels for buttons)Came here looking for the same - but ended up finding my answer in the docs.
a
label
element withchecked
attribute can be selected like so:I know it's an old question, but maybe it helps someone out there :)