Taking an example from the end of https://developer.mozilla.org/en-US/docs/Learn/Forms/How_to_build_custom_form_controls:
.select label {
display: none;
}
.select:focus-within label {
display: initial;
}
input:checked + label {
display: initial;
}
<fieldset class="select">
<legend>Pick a fruit</legend>
<input type="radio" name="fruit" value="Cherry" id="fruitCherry" checked>
<label for="fruitCherry">Cherry</label>
<input type="radio" name="fruit" value="Lemon" id="fruitLemon">
<label for="fruitLemon">Lemon</label>
<input type="radio" name="fruit" value="Banana" id="fruitBanana">
<label for="fruitBanana">Banana</label>
</fieldset>
(I have simplified the example to better illustrate the point). Note how you can click on one of the labels, and since focus is inside the select after that, all labels become visible. However, as soon as you try to click on label of another item, the labels disappear before selecting that item, since there is a brief period before the mouse button is released where no element is focused. I assume the the browser only handles the click event when the mouse button is released.
Is there any way to keep the focus while holding down the mouse button as well, without JavaScript? I experienced this on Firefox and Chrome on Linux.
I am also surprised by the fact that it acts differently depending on whether you click-and-hold on the label or the radio button.
By default <label> is not a focusable Element.
When you try to click a label it's as if you're clicking away to remove the focus from what you previously clicked.
In order to make an element focusable we use the tabindex attribute with a negative value so it doesn't interfere with the navigation because our sole purpose is to make the element focusable
.select label {
display: none;
}
.select:focus-within label {
display: initial;
}
input:checked+label {
display: initial;
}
<fieldset class="select">
<legend>Pick a fruit</legend>
<input type="radio" name="fruit" value="Cherry" id="fruitCherry" checked>
<label tabindex="-1" for="fruitCherry">Cherry</label>
<input type="radio" name="fruit" value="Lemon" id="fruitLemon">
<label tabindex="-1" for="fruitLemon">Lemon</label>
<input type="radio" name="fruit" value="Banana" id="fruitBanana">
<label tabindex="-1" for="fruitBanana">Banana</label>
</fieldset>
Related
I'm trying to do a pure css Show/Hide with radio button.
As seen in below snippet, it works like a charm.
.refusal,
.acceptance {
display: none;
}
input#refusal:checked~.refusal {
display: block;
}
input#acceptance:checked~.acceptance {
display: block;
}
This example works!</br>
<input type="radio" id="refusal" name="status" value="declined">
<label for="refusal">NO</label>
<input type="radio" id="acceptance" name="status" value="accepted">
<label for="acceptance">YES</label>
<form class="refusal">Something for REFUSAL</form>
<form class="acceptance">Something for ACCEPTANCE</form>
The problem is I want to modify my html input/label like this:
<label>
<input type="radio" id="refusal" name="status" value="declined">
NO</label>
However, if I do so, my snippet doesn't work any more (a css selector problem I guess).
But I don't know how to make it work. Thanks.
When you put the input inside a label element you change the level which it resides, so the tilde(~) selector does not work. If you really need the input to be inside a label element you need to use js.
I have this working for a checkbox. When I select the checkbox, my two radio buttons appear. But now, if I select one of the radio buttons, I want a block of address fields to appear. I'm not sure why the same thing that is working for the checkbox isn't working for the radio button.
<form>
<input class="no22XAddressRadioButtons_activator" type="checkbox">
Check if you do not have a physical address within the Oakland Beat 22X boundary.
<div class="no22XAddressRadioButtons">
<input class="outsideAddress_activator" type="radio" name="no22XAddress" id="outsideAddress_activator">
<label for="outsideAddress_activator">My physical address is outside of the 22X boundary.</label><br>
<input class="noAddress" type="radio" name="no22XAddress" id="no22XAddress">
<label for="no22XAddress">I don't have a physical address.</label><br>
</div>
<!--Begin conditional address-->
<div class="outsideAddressTextFields">
Enter your physical address including city, state, and zip code.
<input class="outsideAddress" type="text">
<label class="labelText" for="outsideAddress">Number and Street</label>
<input type="text" name="outsideAddress" id="outsideAddress" required>
</div>
</form>
/* Begin conditional radio buttons */
.no22XAddressRadioButtons {
display: none;
}
.no22XAddressRadioButtons_activator {
margin-left: 30px;
}
.no22XAddressRadioButtons_activator:checked + .no22XAddressRadioButtons {
display: block;
margin-left: 45px;
}
/* Begin conditional address */
.outsideAddressTextFields{
display: none;
}
.outsideAddress_activator:checked + .outsideAddressTextFields {
display: block;
}
Thats because you're using +, an Adjacent sibling combinator. Which means the block that you need to be made visible, while an input is checked has to be right after the said input. You can use a General sibling combinator which I think might be helpful in you case. I've attached a modified code snippet to demonstrate.
As a side note: use labels to associate an input to a caption.
You can click the associated label to focus/activate the input, as well as the input itself. This increased hit area provides an advantage to anyone trying to activate the input, including those using a touch-screen device.
/* Begin conditional radio buttons */
.no22XAddressRadioButtons {
display: none;
}
.no22XAddressRadioButtons_activator {
margin-left: 30px;
}
.no22XAddressRadioButtons_activator:checked ~ .no22XAddressRadioButtons {
display: block;
margin-left: 45px;
}
/* Begin conditional address */
.outsideAddressTextFields{
display: none;
}
.outsideAddress_activator:checked ~ .outsideAddressTextFields {
display: block;
}
<form>
<input id="addressCheck" class="no22XAddressRadioButtons_activator" type="checkbox">
<label for="addressCheck">Check if you do not have a physical address within the Oakland Beat 22X boundary.</label>
<div class="no22XAddressRadioButtons">
<div>
<input class="outsideAddress_activator" type="radio" name="no22XAddress" id="outsideAddress_activator">
<label for="outsideAddress_activator">My physical address is outside of the 22X boundary.</label>
<!--Begin conditional address-->
<div class="outsideAddressTextFields">
<div>
<label for="insideAddress">Enter your physical address including city, state, and zip code.</label>
<input class="outsideAddress" type="text" id="insideAddress">
</div>
<div>
<label class="labelText" for="outsideAddress">Number and Street</label>
<input type="text" name="outsideAddress" id="outsideAddress" required>
</div>
</div>
</div>
<div>
<input class="noAddress" type="radio" name="no22XAddress" id="no22XAddress">
<label for="no22XAddress">I don't have a physical address.</label><br>
</div>
</div>
</form>
I want to have radio input choice done with pushing buttons. (it's where you have several options and you can only choose one, usually done with radio dots) I used
<input id="test"><label for="test">Click Me</label>
To achieve the same effect but with text (when clicked on text radio button sets to checked="true", but no matter how I try to approach it while using HTML buttons I never can get radio input checked with just button and not text clicking. So this and variation of this where you set button as parent with for tag etc. didn't work for me.
<input id="test"><label for="test"><button>Click Me</button></label>
I can use something like addEventListeners to buttons and set the choice in javascript, but thought maybe there is an obvious pure HTML way I just could not figure out. Heres a small pen just for fiddling around - Cheers
Just use your first example and style the label element to look like a button.
Here is a quick example:
label {
-webkit-appearance: button;
-moz-appearance: button;
appearance: button;
padding: 5px;
}
input {
display: none;
}
input:checked + label {
background: red;
}
<input id="radio1" name="radio" type="radio" />
<label for="radio1">Click me</label>
<input id="radio2" name="radio" type="radio" />
<label for="radio2">Click me</label>
<input id="radio3" name="radio" type="radio" />
<label for="radio3">Click me</label>
I'm using Twitter Bootstrap and I need two radio buttons, inline, with text on the left. So far afer a couple of pieces of different code I managed to get them inline, the text is on the right though. That's not the main problem anyway - take a look at how the radio buttons look:
On the left there seem to be two radio buttons, one on top of the other. Another thing is that when I choose the second one, the first one still appears chosen.
My questions (the most important on top):
1) How to deal with two radio buttons being chosen at the same time?
2) How to style the radio buttons? I tried background color, border - nothing changes
3) How to put the text to the left from the radio button? Changing its position before and after input doesn't change a thing.
Here's the code:
<form name="searchform">
<input type="text" name="searchterms">
<input type="submit" name="SearchSubmit" value="Search">
<label class="search-radio-text"><input type="radio" name="sex" value="male">Nazwisko</label>
<label class="search-radio-text"><input type="radio" name="sex" value="female">Tytuł</label>
</form>
One solution is to style the labels, after hiding the radio boxes themselves and binding the labels to their radio boxes.
HTML
<form name="searchform">
<input type="text" name="searchterms">
<input type="submit" name="SearchSubmit" value="Search">
<input type="radio" id="radio1" name="sex" value="male">
<label class="search-radio-text" for="radio1">Nazwisko</label>
<input type="radio" id="radio2" name="sex" value="female">
<label class="search-radio-text" for="radio2">Tytuł</label>
</form>
CSS
input[type="radio"] {
display:none;
}
input[type=radio] + label {
display:inline-block;
margin:-2px;
padding: 4px 12px;
background-color: #e7e7e7;
border-color: #ddd;
}
input[type=radio]:checked + label {
background-image: none;
background-color:#99cc33;
}
Demo: http://jsfiddle.net/user2314737/X5gBm/
You can also simulate checkboxes using images like this: http://jsfiddle.net/user2314737/X5gBm/1/
I am trying to show and hide a text field based on whether or not the user clicks a radio button. Is this possible? According to the docs, collapsible content needs a header.
Here is my code with no collapsible content:
<fieldset data-role="controlgroup" data-mini="true">
<input type="radio" name="radio-mini" id="radio-mini-1" value="choice-1" />
<label for="radio-mini-1">No</label>
<input type="radio" name="radio-mini" id="radio-mini-2" value="choice-2" />
<label for="radio-mini-2">Yes</label>
</fieldset>
<label for="textarea-a">Textarea:</label>
<textarea name="textarea" id="textarea-a">
</textarea>
I would like the textarea to be shown if the radio button labeled "Yes" is clicked. Any ideas?
Well, if the radio and the textarea are siblings (and you're using, and happy to be compatible with up-to-date browsers) you could use CSS:
label[for=textarea-a],
#textarea-a {
display: none;
}
#radio-mini-2:checked ~ label[for=textarea-a],
#radio-mini-2:checked ~ #textarea-a {
display: block;
}
JS Fiddle demo.
In the above I removed your radio elements from the fieldset (as the textarea, and its label have to be siblings for this approach to work).