If I have a radio input that is wrapped within a label, how can I target the label when the input is checked?
<div>
<p>Payment Plan:</p>
<label><input name="os0" type="radio" value="monthly">TEST</label>
</div>
I tried:
input:checked + label { color: red }
and
input:checked + label
But none worked, what I am doing wrong? I also tried the > selector.
The reason I have label wrapping the input, is because I NEED the label to be clickable
There's no parent or backward selector in CSS (yet?). Thus, we can't select the wrapper label by the wrapped input.
There are two options:
1) Wrapping the content by an inline wrapper like <span> element, as follows:
<label>
<input name="os0" type="radio" value="monthly">
<span>TEST</span>
</label>
Then select the <span> by using adjacent sibling selector +:
input:checked + span {
color: red
}
WORKING DEMO
2) Using for attribute for the label to target the input by its id attribute as follows:
<input name="os0" type="radio" id="myinput" value="monthly">
<label for="myinput">TEST</label>
And Then select the label by:
input:checked + label {
color: red
}
WORKING DEMO.
Your css will work if you modify your html to this:
<div>
<p>Payment Plan:</p>
<input id="os0" name="os0" type="radio" value="monthly">
<label for="os0">TEST</label>
</div>
Using the for attribute with an id on the input will let you click on the label to affect the button, as it does when wrapped in the element.
http://jsfiddle.net/PMmrk/
Related
I have four radio buttons to toggle the visibility of:
MINES
MINES INCLUDING SILICIUM
MINES INCLUDING PHOSPHATE
MINES INCLUDING GOLD
<label>
<input type="radio" name="mineRadio"
id="allmines" checked> All
</label>
<label>
<input type="radio" name="mineRadio"
id="sili"> Silicium
</label>
<label>
<input type="radio" name="mineRadio"
id="phos"> Phosphate
</label>
<label>
<input type="radio" name="mineRadio"
id="gold"> Gold
</label>
The mines are circle-shaped elements on top of a map:
When for instance the 'Silicium Mine' button is checked, I want the mines with Silicium to be visible and the ones without this attribute to hide.
How can I set the mines' displays to visible/hidden according to which radio button is checked?
You need a structure where sibbling selector can be used , parent selector is not avalaible in pure CSS. So you need to take inputs out of labels and use the for attribute to link each labels with its related input (can work without it, but click on the label won't be usable).
Example of a structure that can used with the :checked pseud-class and sibbling selector ~
/* Demo : toggle display from an id attribute to later sibblings of a specific class */a {
border: solid;
display: none;
}
a:before {/* show them by their class names */
display: inline-block;
content: attr(class);
padding:0.15em 0.5em;
}
#map {margin:1em;padding:1em;border:solid;}
#sili:checked ~ #map .sili,
#phos:checked ~ #map .phosp,
#gold:checked ~ #map .gold,
#allmines:checked ~ #map a {
display: inline;
}
<input type="radio" name="mineRadio" id="allmines" checked> <label for='allmines'> All
</label>
<input type="radio" name="mineRadio" id="sili"><label for='sili'> Silicium
</label>
<input type="radio" name="mineRadio" id="phos"> <label for='phos'> Phosphate
</label>
<input type="radio" name="mineRadio" id="gold"> <label for='gold'> Gold
</label> The mines are circle-shaped elements on top of a map:
<div id="map">
</div>
infos about duos input/label : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label
The HTML <label> element represents a caption for an item in a user interface.
the for attribute
for
The id of a labelable form-related element in the same document as the <label> element. The first element in the document with an id matching the value of the for attribute is the labeled control for this label element if it is a labelable element. If it is not labelable then the for attribute has no effect. If there are other elements that also match the id value, later in the document, they are not considered.
Note: A <label> element can have both a for attribute and a contained control element, as long as the for attribute points to the contained control element.
Infos about the sibbling selectors : https://css-tricks.com/child-and-sibling-selectors/
I am trying to directly style <label> but based on value "black"
The html is output by a Wordpress plugin and the <label> has a #before selector which I would like to target for changing and icon color. However I wish to change the icon color multiple times based on the radio value .. In the case of value="black" I wish to change the icon to black.
Can anybody point me in the right direction?
<div class="mspc-text-wrapper">
<strong class="mspc-attribute-title">Black</strong>
<div class="mspc-radio ui radio checkbox">
<input type="radio" name="pa_colour" value="black">
<label>
::before
::after
</label>
</div>
</div>
You select it using the adjacent sibling selector + like this:
input[value=black] + label::before
label::before { content: '::before'; } /* just so the result is visible */
label::after { content: '::after'; } /* just so the result is visible */
input[value=black] + label::before { color: red; }
input[value=black] + label::after { color: green; }
<div class="mspc-text-wrapper">
<strong class="mspc-attribute-title">Black</strong>
<div class="mspc-radio ui radio checkbox">
<input type="radio" name="pa_colour" value="black">
<label>
</label>
</div>
</div>
I have this code:
<fieldset id="login">
<input type="text">
<input type="password">
<input type="checkbox" id="inline_invisible">
<label for="invisible">Login invisible</label>
<input type="checkbox" id="remember">
<label for="remember">Remember me</label>
</fieldset>
Labels are not supposed to show until one of the input boxes are on focus so I have this CSS:
#login input:focus + input + label {
display:inline-block;
}
but it only selects the first label and not the second one when the password box is on focus and does nothing with the first input box. What should I do? CSS only please, I cant change the html.
Thank you.
Try
#login input:focus ~ label {display:inline-block;}
plus(+) selects the adjacent siblings while ~ looks for the next sibling
Reference for general sibling selector: https://developer.mozilla.org/en/docs/Web/CSS/General_sibling_selectors
Reference for adjacent sibling selector:
https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors
Use the general sibling selector(~) which will select all the labels following the focused input.
(Note that + selector selects only one sibling element that follows)
label{
display: none;
}
#login input:focus ~ label {
display: inline-block;
}
<fieldset id="login">
<input type="text">
<input type="password">
<input type="checkbox" id="inline_invisible">
<label for="invisible">Login invisible</label>
<input type="checkbox" id="remember">
<label for="remember">Remember me</label>
</fieldset>
I'm trying to run a hover state for a specific label (not all) that forms a custom radio button by nesting an input field and a span element, like so -
<label>
<input id="sample" name="sample" type="radio" value="sample">
<span></span>
Sample
</label>
The hover state should be that of a cursor pointer change. How would I go about to achieve this? Right now only the span (which is a custom radio button design) gets a pointer cursor. If the user hovers the label text nothing happens.
Why not wrap the text in em, i, etc tags and style with cursor: pointer?
label em {
cursor: pointer;
font-style: normal;
}
<label>
<input id="sample" name="sample" type="radio" value="sample">
<span></span>
<em>Sample</em>
</label>
EDIT - further to comments below, please see the following:
label, label *:not(input) {
cursor: pointer;
font-style: normal;
}
<label>
<input id="sample" name="sample" type="radio" value="sample">
<span></span>
Sample
</label>
Since you can't select actual text nodes with CSS (i.e. - "Sample" in your example), we can use CSS to style the label as a whole and its descendants, but with the exception of the input (:not(input))
Is it possible, with only CSS, to style an HTML label dependent on its input's state?
In my case, I want to style an <input type="checkbox"> based on whether it's checked.
I tried putting the label inside the input, but Firefox and Chrome (at least) seems to parse them as siblings, even though they're clearly nested in the input source. And I don't know how to write a CSS rule that can indirect through a for= attribute.
Do I need to whip out the Javascript on this one?
They don't need to be nested, that's what the "for" attribute is for in the <label> element.
In modern browsers (those supporting CSS 2.1), you can use a sibling selector, such as
input + label {
/* rules */
}
You would have to have your markup in a strict sibling relationship, such as:
<input name="cb" id="cb" type="checkbox"><label for="cb">Checkbox</label>
Using the adjacent/sibling selector plus the attribute selector would make it work:
<form>
<style>
INPUT[checked=checked] + LABEL {
color: #f00;
}
</style>
<div>
<input type="checkbox" id="chk1" />
<label for="chk1">Label #1</label>
</div>
<div>
<input type="checkbox" id="chk2" checked="checked" />
<label for="chk2">Label #2</label>
</div>
</form>
To make this thing work you need to put the label after the input, this goes for text type inputs, so for checkbox you can skip this, unless you want the label before checkbox.
To keep the order for label being shown before the input you need to use Flexbox and reverse order of items, for example like this.
.form-group {
display: flex;
flex-direction: column-reverse;
}
The display: flex; with flex-direction: column-reverse; reorders the divs content.
Now all you need to do is use this to affect your label style.
input:checked + label {
color: #000;
}
And HTML for completeness.
<div class="form-group">
<input type="checkbox" name="rememberPwd" id="rememberPwd" class="form-input" required/>
<label for="rememberPwd">Remember?</label>
</div>