i want to hide all content, i want them to show when checked
like show content when checkbox checked
hide when unchecked so that when i check others they show up too
here is my try but its not working
<style>
#myBike:not(:checked) +#bike {
display: block !important;
}
#myCar:not(:checked) +#car {
display: block !important;
}
</style>
<input type="checkbox" id="myBike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="myCar">
<label for="vehicle2"> I have a car</label><br>
<div class="row" id="bike">
Something for bike
</div>
<div class="row" id="car">
Something for car
</div>
Please check the code below.
#bike {
display: none;
}
#myBike:checked~#bike {
display: block;
}
#car {
display: none;
}
#myCar:checked~#car {
display: block;
}
<input type="checkbox" id="myBike" />
<label for="vehicle1">I have a bike</label>
<input type="checkbox" id="myCar" />
<label for="vehicle2">I have a car</label>
<div class="row" id="bike">Something for bike</div>
<div class="row" id="car">Something for car</div>
Expanation:
You have used a wrong syntax ~ vs +
By default all div are set as display:block. You should set display:none as its initial.
It is work when the radio buttons are same div level with "content1" and "content2",
How to make it work, if I put radio button to another div that outside the div "second"
suppose that the toggle1 is checked then content1 will show up
(using CSS and HTML ONLY, no javascript)
.content1 {
display: none;
}
.content2 {
display: none;
}
.toggle1:checked ~ .grid-container .content1 {
display: block;
}
.toggle2:checked ~ .grid-container .content2 {
display: block;
}
<div class="level1">
<div class="level2">
<input type=radio id="toggle1" name="toggle" class="toggle1">
<label for="toggle1">toggle1</label>
<input type=radio id="toggle2" name="toggle" class="toggle2">
<label for="toggle2">toggle2</label>
<div>
<div>
<div class="second">
<div class="tab content1">Content1</div>
<div class="tab content2">Content2</div>
</div>
When using the general sibling combinator ~, the two elements must be "children of the same parent element." Given your existing code, apply the "grid-container" class to the <div> that is a sibling of the <input> elements.
.content1,
.content2 {
display: none;
}
.toggle1:checked ~ .grid-container .content1,
.toggle2:checked ~ .grid-container .content2 {
display: block;
}
<div class="level1">
<div class="level2">
<input type=radio id="toggle1" name="toggle" class="toggle1">
<label for="toggle1">toggle1</label>
<input type=radio id="toggle2" name="toggle" class="toggle2">
<label for="toggle2">toggle2</label>
<div class="grid-container">
<div>
<div class="second">
<div class="tab content1">Content1</div>
<div class="tab content2">Content2</div>
</div>
</div>
</div>
</div>
</div>
I am using this code. I would also like when it is the display: block the color of the label changes.
label.divcheck { color:blue; text-decoration:underline; }
input.divcheck { display:none; }
input.divcheck + div { display:none; }
input.divcheck:checked + div { display:block;}
<label class="divcheck" for="navigation">Button Nav</label>
<label class="divcheck" for="other">Button Other</label>
<input type="checkbox" class="divcheck" id="navigation"/>
<div class="navigation">
Foo
</div>
<input type="checkbox" class="divcheck" id="other"/>
<div class="navigation">
Other
</div>
https://stackoverflow.com/a/35781115/15949286
The only way to do this without using JavaScript is to parse the label elements and position them after the input. The example for this is as follows.
label.divcheck {
color: blue;
text-decoration: underline;
}
input.divcheck {
display: none;
}
input.divcheck~div {
display: none;
}
input.divcheck:checked~div {
display: block;
}
input.divcheck:checked~label.divcheck {
color: red;
}
section {
display: flex;
}
<section>
<div>
<input type="checkbox" class="divcheck" id="other" />
<label class="divcheck" for="other">Button Other</label>
<div class="navigation">
Foo
</div>
</div>
<div>
<input type="checkbox" class="divcheck" id="navigation" />
<label class="divcheck" for="navigation">Button Nav</label>
<div class="navigation">
Other
</div>
</div>
</section>
Explanation
I want to switch between images selecting radios, but I'd like it to be HTML/CSS only. As you can see in the code below, it does work, but in my main code the radio button and the image are within their own divs, which then this code won't work because ~ only acts if .radio-image is directly preceded by input.radio3:checked; also in my main code, the images comes before the radio button.
I also wrote an example like it'd in my main code.
Codes
working code
input.radio1:checked~.image1 {
display: block;
}
input.radio1:checked~.image2 {
display: none;
}
input.radio1:checked~.image3 {
display: none;
}
input.radio2:checked~.image2 {
display: block;
}
input.radio2:checked~.image1 {
display: none;
}
input.radio2:checked~.image3 {
display: none;
}
input.radio3:checked~.image3 {
display: block;
}
input.radio3:checked~.image1 {
display: none;
}
input.radio3:checked~.image2 {
display: none;
}
<input type="radio" name="test" class="radio1" checked/>
<input type="radio" name="test" class="radio2" />
<input type="radio" name="test" class="radio3" />
<div class="image1">
<img src="http://placehold.it/50x50">
</div>
<div class="image2">
<img src="http://placehold.it/75x75">
</div>
<div class="image3">
<img src="http://placehold.it/100x100">
</div>
main code example
input.radio1:checked~.image1 {
display: block;
}
input.radio1:checked~.image2 {
display: none;
}
input.radio1:checked~.image3 {
display: none;
}
input.radio2:checked~.image2 {
display: block;
}
input.radio2:checked~.image1 {
display: none;
}
input.radio2:checked~.image3 {
display: none;
}
input.radio3:checked~.image3 {
display: block;
}
input.radio3:checked~.image1 {
display: none;
}
input.radio3:checked~.image2 {
display: none;
}
<section class="testSection">
<div class="firstDiv">
<div class="image1">
<img src="http://placehold.it/50x50">
</div>
<div class="image2">
<img src="http://placehold.it/75x75">
</div>
<div class="image3">
<img src="http://placehold.it/100x100">
</div>
</div>
<div class="secondDiv">
<form class="testForm">
<div class="thirdDiv">
<div class="inputDiv">
<input type="radio" name="test" class="radio1" checked />
</div>
<div class="inputDiv">
<input type="radio" name="test" class="radio2" />
</div>
<div class="inputDiv">
<input type="radio" name="test" class="radio3" />
</div>
</div>
</form>
</div>
</section>
Thanks in advance,
Luiz.
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>