CSS for checked radio button with multiple labels - html

I'm having css issues with a second label on a radio button, when this radio button is checked. I already found out that a second (and a third) label is possible using 'for' in the label-tag. It's not possible to group everything in a single label-tag.
How can I change the background for the second label when a radio button is checked?
My (simplified) code is below, for the first label it works, second label it doesn't.
.radioclass:checked + label {
background-color: cyan;
}
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>

The + selector in css means "immediate sibling", i.e. the very next element after the input must be a label. This will only select the immediate sibling.
An alternative is ~ which means "any sibling" and targets any label after the input.
In both of these cases, the elements (input and label) are on the same dom level. There is no way to traverse up the dom, grab the sibling and then the label - which is what you are trying to do with the html supplied.
If you are able to change the html, then either place the input outside the two divs or place both labels inside the same div (after the input).
.radioclass:checked ~ div label {
background-color: cyan;
}
<div class="row">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<div class="col-sm-4">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
.radioclass:checked ~ label {
background-color: cyan;
}
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
<label for="id_radiobtn1">second label</label>
</div>
</div>
If you cannot alter the html, then JS is the only other approach (I've changed the input to a checkbox for demonstration purposes):
$('input').on('change', function(){
var func = 'removeClass';
if($(this).is(':checked')) {
func = 'addClass';
}
$('label[for="'+$(this).attr('id')+'"]')[func]('checked');
});
label.checked {
background-color: cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="checkbox" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>

Using just CSS this is possible, although it does require a rearrangement of your HTML, effectively moving the radio <input> ahead of the elements in which the <label> elements are contained; this removes the (impossible in CSS) requirement of traversing to the parent of the <input> in order to style the non-sibling <label> elements.
/* selects the element with the id of 'id_radiobtn1' when it is
checked, uses the general sibling combinator (~) to select
all sibling <div> elements with the class of 'col-sm-4'
and then finds the descendant <label> elements within
that/those <div> elements whose 'for' attribute-value is
equal to 'id_radiobtn1': */
#id_radiobtn1:checked ~ div.col-sm-4 label[for=id_radiobtn1] {
background-color: cyan;
}
<div class="row">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1" />
<div class="col-sm-4">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
Now, if you must have a visible radio <input> besides the first of the <label> elements, you can use a pseudo-element to effectively pretend, while also hiding the real radio <input>:
*,
::before,
::after {
/* to include border and padding in the calculated
dimensions (width/height) of the elements: */
box-sizing: border-box;
}
.row>input[type=radio] {
/* hiding the radio <input> elements: */
display: none;
}
/* selecting the <input> elements whose type is
equal to 'radio', which is checked and then
finding all (subsequent) sibling elements
(using the '~' combinator) which <div>
elements with a class of 'col-sm-4', and
traversing to the <label> elements within: */
input[type=radio]:checked~div.col-sm-4 label {
background-color: cyan;
}
label {
/* in order to ensure that the descendant
pseudo-elements are positioned relative
to their parent element: */
position: relative;
/* making room for the pseudo-elements: */
margin-left: 2em;
}
input[type=radio]+.col-sm-4 label::before {
/* the content property is required, even
if only an empty string, to have the
pseudo-element show up: */
content: '';
display: inline-block;
/* positioning absolutely, in relation to
the closest ancestor with a non 'static'
position, in this case the parent
<label> element: */
position: absolute;
/* moving it outside of the <label> element's
left border, to avoid the background-color: */
left: -2em;
/* purely aesthetic: */
width: 0.8em;
height: 0.8em;
/* arbitrary positioning, adjust to taste: */
top: 50%;
transform: translateY(-40%);
/* making the pseudo-element circular in shape: */
border-radius: 50%;
/* colouring the faux 'border': */
box-shadow: 0 0 0 2px #ccc;
}
/* adjusting the colours of the faux radio: */
input[type=radio]:checked+.col-sm-4 label::before {
box-shadow: 0 0 0 2px #000, inset 0 0 0 3px #fff;
background-color: limegreen;
}
<div class="row">
<input class="radioclass" id="id_radiobtn1" type="radio" name="group1" value="1" />
<div class="col-sm-4">
<label for="id_radiobtn1">row one first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">row one second label</label>
</div>
</div>
<div class="row">
<input class="radioclass" id="id_radiobtn2" type="radio" name="group1" value="2" />
<div class="col-sm-4">
<label for="id_radiobtn2">row two first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn2">row two second label</label>
</div>
</div>

You can do with Jquery on change event.
$(document).ready(function() {
$('#id_radiobtn1').on('change',function(){
$( "label:contains('second')" ).css( "background-color", "red" );
});
});
.radioclass:checked + label {
background-color: cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>

You can't do it with pure css, because backward in css is impossible.You can help of Jquery:
Note: I insert lab2 class to label.
$(document).ready(function(){
$('input[type=radio]').on('change',function(){
if($('.radioclass').is(':checked'))
$('.lab2').addClass('sel');
else
$('.lab2').removeClass('sel');
})
})
$(document).ready(function(){
$('input[type=radio]').on('change',function(){
if($('.radioclass').is(':checked'))
$('.lab2').addClass('sel');
else
$('.lab2').removeClass('sel');
})
})
#id_radiobtn1:checked + label {
background-color: green;
}
.sel {
background-color: red;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1" class="lab2">second label</label>
</div>
</div>

Related

I cannot select span:before with CSS

I want to access span elements inside .payment-method. For first span element I want to set image "image1.png" and for second element "image2.png".
Here is my HTML code:
.payment-group .payment-method:nth-child(0){
.payment-method-title label span:before{
content: url(https://icon-library.com/images/delivery-service-icon/delivery-service-icon-6.jpg);
}
}
.payment-group.payment-method:nth-child(1){
.payment-method-title label span:before{
content: url(https://icon-library.com/images/bank-transfer-icon/bank-transfer-icon-6.jpg);
}
}
<div class="payment-group">
<div class="payment-method">
<div class="payment-method-title field choice">
<input type="radio" class="radio" id="cashondelivery" value="cashondelivery"/>
<label class="label">
<span>Cash on delivery</span>
</label>
</div>
</div>
<div class="payment-method">
<div class="payment-method-title field choice">
<input type="radio" class="radio" id="banktransfer" value="banktransfer"/>
<label class="label">
<span>Bank transfer</span>
</label>
</div>
</div>
</div>
Can someone help me ?
(I am using LESS, but you can help me with plain CSS)
There are a few problems here:
nth-child starts at 1 not 0 in CSS.
The nesting of selectors does not exist in pure CSS, this snippet 'flattens' them
Space is a very important character in a CSS selector. It is a 'combinator'. The second selector missed it out before .payment-method
the before of a pseudo element nowadays should have a double colon as in ::before (this indicates a pseudo element as opposed to a pseudo class).
.payment-group .payment-method:nth-child(1) .payment-method-title label span::before {
content: url(https://icon-library.com/images/delivery-service-icon/delivery-service-icon-6.jpg);
}
.payment-group .payment-method:nth-child(2) .payment-method-title label span::before {
content: url(https://icon-library.com/images/bank-transfer-icon/bank-transfer-icon-6.jpg);
}
<div class="payment-group">
<div class="payment-method">
<div class="payment-method-title field choice">
<input type="radio" class="radio" id="cashondelivery" value="cashondelivery" />
<label class="label">
<span>Cash on delivery</span>
</label>
</div>
</div>
<div class="payment-method">
<div class="payment-method-title field choice">
<input type="radio" class="radio" id="banktransfer" value="banktransfer" />
<label class="label">
<span>Bank transfer</span>
</label>
</div>
</div>
</div>

Styling Radio Button Breaks Functionality

I have a Vue app with a HTML Form with multiple sets of radio buttons.
I customized their appearance using the SO answer written here
However, when I click on any of the radio buttons, only the first set of radio buttons are affected, even when clicking a different set's radio button.
This is the html and css (JSFiddle link)
Any idea why this is happening?
Update: The problem was with the label tags - their for attribute was still set to the first set of radio buttons!
<div class="time_input" >
<div class="time_input__radio_group">
<div class="radio_group">
<input type="radio" name="start" id="am" :value="true" v-model="startInMorning">
<label class="radio_label" for="am">AM</label>
</div>
<div class="radio_group">
<input type="radio" name="start" id="pm" :value="false" v-model="startInMorning">
<label class="radio_label" for="pm">PM</label>
</div>
</div>
</div>
<div class="days_open_input">
<div class="radio_group" >
<input type="radio" name="days_open" id="one_day" :value="1" v-model="days_open" checked>
<label class="radio_label" for="am">1</label>
</div>
<div class="radio_group">
<input type="radio" name="days_open" id="two_days" :value="2" v-model="days_open">
<label class="radio_label" for="pm">2</label>
</div>
<div class="radio_group">
<input type="radio" name="days_open" id="three_days" :value="3" v-model="days_open">
<label class="radio_label" for="pm">3</label>
</div>
</div>
<div class="tracks_limit_input">
<div class="radio_group">
<input type="radio" name="tracks_limit" id="eight_songs" value="8" v-model="tracks_limit" >
<label class="radio_label " for="am">8</label>
</div>
<div class="radio_group">
<input type="radio" name="tracks_limit" id="sixteen_songs" value="16" v-model="tracks_limit" checked class="tracks_limit_input__margin">
<label class="radio_label" for="pm">16</label>
</div>
</div>
/* completely hiding radio button */
input[type="radio"] {
display: none;
}
/* simulate radiobutton appearance using pseudoselector */
input[type="radio"] + *::before {
content: "";
/* create custom radiobutton appearance */
display: inline-block;
width: 15px;
height: 15px;
padding: 3px;
margin-right: 5px;
/* background-color only for content */
background-clip: content-box;
border: 1px solid #bbbbbb;
background-color: #e7e6e7;
border-radius: 50%;
}
/* appearance of checked radiobutton */
input[type="radio"]:checked + label::before {
background-color: black;
}
/* resetting default box-sizing */
*,
*:before,
*:after {
box-sizing: border-box;
}
/* optional styles for centering radiobuttons */
.radio-group label {
display: inline-flex;
align-items: center;
}
I think there is no mistake with the css
The code you are using for the HTML is the one causes problem:
1st it is Vue code not pure HTML code
I will take the 1st group - the time example:
<div class="time_input" >
<div class="time_input__radio_group">
<div class="radio_group">
<input type="radio" name="start" id="am" :value="true" v-model="startInMorning">
<label class="radio_label" for="am">AM</label>
</div>
<div class="radio_group">
<input type="radio" name="start" id="pm" :value="false" v-model="startInMorning">
<label class="radio_label" for="pm">PM</label>
</div>
</div>
</div>
Both of inputs is set to the same model startInMorning so if it is true both checked and vice versa.
So the fix is:
first remove the v-model="startInMorning" for both
next change the :value
for the first one :value="startInMorning",
for the second one :value="!startInMorning"
Do similar for others
The problem seemed to be with the HTML!
The for attribute on the label tags was set to the wrong radio buttons
ie
<input type="radio" name="days_open" id="two_days" :value="2" v-model="days_open">
<label class="radio_label" for="pm">2</label>
<input type="radio" name="days_open" id="two_days" :value="2" v-model="days_open">
<label class="radio_label" for="pm">2</label>

How to specify a complex absolute reference in CSS

I have a HTML page containing some radiobuttons and some related fieldsets, each one with its own id.
I want to hide/view the div depending on a specific radiobutton.
The attached Fiddle do the job.
Here is the HTML
<div id="FS1">External fieldset with radiobuttons
<div class="row">
<input checked="checked" id="R1" name="Group1" type="radio" value="1"/>
<label for="R1">Element 1</label>
<input id="R2" name="Group1" type="radio" value="2"/>
<label for="R2">Element 2</label>
<input id="R3" name="Group1" type="radio" value="3"/>
<label for="R3">Element 3</label>
<fieldset class="fs" id="FS1-1">
<legend>Header element 1</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText1" type="text"/>
<label for="exampleInputText1">Input field in the first element</label></div>
</fieldset>
<fieldset class="fs" id="FS1-2">
<legend>Header element 2</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText2" type="text"/>
<label for="exampleInputText2">This is the input field for the second</label>
</div>
</fieldset>
<fieldset class="fs" id="FS1-3">
<legend>Header element 3</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText3" type="text"/>
<label for="exampleInputText3">And this is the last input field</label>
</div>
</fieldset>
</div>
</div>
and this is the CSS
#FS1 div input:not(:checked) ~ #FS1-1, #FS1-2, #FS1-3 { display: none; } /* to hide all the detail elements */
#FS1 div input[value="1"]:checked ~ #FS1-1 { display: block; } /* to show only the 1th elem */
#FS1 div input[value="2"]:checked ~ #FS1-2 { display: block; } /* to show only the 2nd */
#FS1 div input[value="3"]:checked ~ #FS1-3 { display: block; } /* to show only the 3rd */
Now, what I want to achieve, is to relocate the fieldset outside the div, at the end of the fragment, but this brokes the css references and the fragment doesn't run anymore.
The following is the desired fragment, not running.
I need suggestions on how to redefine the CSS
<div id="FS1">External fieldset with radiobuttons
<div class="row">
<input checked="checked" id="R1" name="Group1" type="radio" value="1"/>
<label for="R1">Element 1</label>
<input id="R2" name="Group1" type="radio" value="2"/>
<label for="R2">Element 2</label>
<input id="R3" name="Group1" type="radio" value="3"/>
<label for="R3">Element 3</label>
</div>
<fieldset class="fs" id="FS1-1">
<legend>Header element 1</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText1" type="text"/>
<label for="exampleInputText1">Input field in the first element</label></div>
</fieldset>
<fieldset class="fs" id="FS1-2">
<legend>Header element 2</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText2" type="text"/>
<label for="exampleInputText2">This is the input field for the second</label>
</div>
</fieldset>
<fieldset class="fs" id="FS1-3">
<legend>Header element 3</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText3" type="text"/>
<label for="exampleInputText3">And this is the last input field</label>
</div>
</fieldset>
</div>
So what you're asking cannot be achieved with plain CSS, but a solution could be achieved with Javascript.
Here is why it is not possible with your current HTML structure, as your current HTML structure looks like this:
With corresponding (simplified) code:
#FS1 div input:not(:checked) ~ #FS1-1, #FS1-2, #FS1-3 { display: none; } /* to hide all the detail elements */
#FS1 div input[value="1"]:checked ~ #FS1-1 { display: block; } /* to show only the 1th elem */
#FS1 div input[value="2"]:checked ~ #FS1-2 { display: block; } /* to show only the 2nd */
#FS1 div input[value="3"]:checked ~ #FS1-3 { display: block; } /* to show only the 3rd */
<div id="FS1">
<div class="row">
<input checked="checked" id="R1" name="Group1" type="radio" value="1"/>
<fieldset class="fs" id="FS1-1"></fieldset>
<fieldset class="fs" id="FS1-2"></fieldset>
<fieldset class="fs" id="FS1-3"></fieldset>
</div>
</div>
As a simple example, I will explain why this line of code works:
#FS1 div input[value="1"]:checked ~ #FS1-1 { display: block; }
Accesses id FS1
Accesses HTML entity div which is a child of FS1
Accesses the HTML entity input for state :checked and internal value="1"
Accesses the sibling element (~) of id FS1-1
Sets the property of display: block for FS1-1
The key thing to note is that the sibling element is selected.
With your proposed changesets to the HTML (see below), you would be required to access the parent element of the HTML entity input for state :checked and internal value="1" and then from there access the sibling, as FS1-1 no longer resides in the div class="row", rather outside it.
As per Wikipedia:
Some noted limitations of the current capabilities of CSS include:
Selectors are unable to ascend. CSS currently offers no way to select a parent or ancestor of an element that satisfies certain criteria.
This has also been touched on here: Is there a CSS parent selector?
There is currently no way to select the parent of an element in CSS. In the meantime, you'll have to resort to JavaScript if you need to select a parent element.
Honestly, it's probably not worth the fight, leave the fieldset inside the div class="row" if possible.
Here are the relevant resources if you wish to go ahead and convert this to a Javascript solution:
Javascript Parent Element
Set Style Javascript
Get Attribute Javascript
Next Sibling Javascript

CSS Hidden First Child

I have an un-editable HTML, which cannot change anything.
I need to hide the first checkbox and the second one will show. It is done in CSS, but somehow it doesn't work as expected.
Here is its LIVE sample.
Please help.
.treeview-container .treeview-item:first-child .form-check label input[type="checkbox"] {
visibility: hidden;
}
<div class="treeview-container">
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />First Box
</label>
</div>
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />Second Box
</label>
</div>
</div>
</div>
</div>
The problem is that .treeview-item:first-child is targetting both of the checkboxes' respective .form-check containers (as they are both the first child of their parent .treeview-item).
This is perhaps a little counter-intuitive, as you may expect the :first-child pseudo-selector to only target the very first occurence of a child of .treeview-item. This is not the case, as the :first-child selector actually targets the first child of each of the .treeview-item parents.
In order to correct this, you can simply use two child combinator selectors (>) to ensure that .treeview-item is a direct child of .treeview-container, and .form-check is a direct child of that .treeview-item.
This can be seen in the following:
.treeview-container > .treeview-item > .form-check label input[type="checkbox"] {
visibility: hidden;
}
<div class="treeview-container">
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />First Box
</label>
</div>
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />Second Box
</label>
</div>
</div>
</div>
</div>
Hope this helps! :)
.treeview-item:first-of-type {
display: none;
}
You can create an ID and add it to any elements you want hidden. However this only hides the element. If you do not want the user to be able to change the checkbox you may want to remove that input type all together.
.treeview-container .treeview-item:first-child .form-check label input[type="checkbox"] {
visibility: hidden;
}
#hideMe {
display: none;
}
<div class="treeview-container">
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" id = "hideMe"/>First Box
</label>
</div>
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />Second Box
</label>
</div>
</div>
</div>
</div>
Using child combinator (>) in between two selectors will select a direct child of the parent. Currently, your code is selecting both inputs as you are just checking for decendents ..ie if the input has an ancestor as .treeview-container or not.
So using two consecutive child combinator will help you get expected result.
Code below.
.treeview-container > div > .form-check label input[type="checkbox"] {
visibility: hidden;
}

combination selector - Plus selector in css not working

May be i don't understand fully plus selector,
What i want, when user click on radio button home, div one should get displayed,
and when user click on radio button about, div two should get displayed, it did not work,
So i strip down the code, where is the problem, with this code i accepted div one to get displayed as home is by default checked. But it did not happened, so i know where is the problem but i dont know why,
Please read the comment, in the code, as i said which line is giving the problem hint it's css last section,
HTML CODE
<div class="container">
<input type="radio" name="option" id="home" checked />
<input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
CSS CODE
.navigation {margin-top:20px;}
.link{cursor:pointer;}
/*making div display*/
.one,.two{
display:none;
}
/*
###This line is not working## want to display div, if user click on radio
button
*/
#home:checked +.container > .one{
display:block;
}
if you want to run the code here is the code pen link https://codepen.io/arif_suhail_123/pen/KvdWey
.container is not a sibling of #home.
To select the element in question, when #home is checked, you can use the ~, which is the general sibling selector:
#home:checked ~ .display > .one
.navigation {margin-top:20px;}
.link {cursor:pointer;}
.one, .two {
display:none;
}
#home:checked ~ .display > .one {
display:block;
}
#about:checked ~ .display > .two {
display: block;
}
<div class="container">
<input type="radio" name="option" id="home" checked />
<input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
The + is the adjacent sibling combinator. Which requires:
The elements to be siblings
The selector on the left of + is the first positioned element
The selector on the right of + is the selector that follows.
There must be no other elements between them.
In the following demo:
Each radio was moved in front of the div it's associated with.
Each radio is display:none since there's no need to show them because the interface are the labels.
Demo
input[name='option'],
.one,
.two {
display: none
}
#home:checked+.one {
display: block;
}
#about:checked+.two {
display: block;
}
<div class="container">
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<input type="radio" name="option" id="home" checked />
<div class="one">
<h3>This is first</h3>
</div>
<input type="radio" name="option" id="about" />
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
I believe for the plus operator to work the element has to be the immediate next sibling - So in this case the .one div would have to immediately follow the #home label, and the css would have to be:
#home:checked + .one{
display:block;
}
The html:
<div class="container">
<input type="radio" name="option" id="home" checked />
<div class="one">
<h3>This is first</h3>
</div>
<input type="radio" name="option" id="about" />
...
+ Selector : The element+element selector is used to select elements that is placed immediately after (not inside) the first specified element.
~ Selector : The element1~element2 selector matches occurrences of element2 that are preceded by element1.
So,you must use ~ instead of +.
.navigation {
margin-top:20px;
}
.link{
cursor:pointer;
}
.one,.two{
display:none;
}
#home:checked ~ .display > .one{
display:block;
}
#about:checked ~ .display > .two{
display:block;
}
<div class="container">
Home: <input type="radio" name="option" id="home" checked />
About: <input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
The order of elements is important when using this selector.
So to use the ~ operator the element should be after the first part.
Ex.
input[type=radio]:checked ~ label {
display: none;
}
The Html should be:
<div class="radio-groupe">
<input type="radio" name="water" id="choice-2" value="more-than-8-cups-a-day">
<label for="choice-2">More</label>
</div>
and not:
<div class="radio-groupe">
<label for="choice-2">More</label>
<input type="radio" name="water" id="choice-2" value="more-than-8-cups-a-day">
</div>