I have the following HTML in my page.
<ul id="detailsList">
<li>
<input type="checkbox" id="showCheckbox" />
<label for="showCheckbox">Show Details</label>
</li>
<li>content ...</li>
<li>content ...</li>
<li>content ...</li>
<li>content ...</li>
<li>content ...</li>
</ul>
I am unable to change this HTML. I have hidden all LI's with the exception of the first by using the following CSS
ul#detailsList li:nth-child(1n+2) {
display:none;
}
So far so good. What I want to do now is to show those hidden LI's when the the checkbox is ticked, using pure CSS. My best attempt so far is
ul#detailsList li input#showCheckbox:checked + li {
display:block;
}
Obviously this doesn't work, as the + li will only select LI's immediately after the checkbox (i.e. siblings), not siblings of the parent.
Is this even possible?
The adjacent sibling selector is used to select an element that is directly after another specific element. Sibling elements must have the same parent element, and "adjacent" means "immediately following".
You use the general sibling selector (~) in combination with :hover . The ~ combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent.
Let's be clear here, just in case someone is finding this from a search engine: there are no parent selectors in CSS, not even in CSS3.
The element>element selector is used to select elements with a specific parent. Note: Elements that are not directly a child of the specified parent, are not selected.
You cannot do that with CSS but
You can try using jQuery
$("#showCheckbox").click(function(){ $(this).parent().siblings().show(); });
You can't do it with CSS alone, you need to use javascript
for this. As You need to catch the change event of the checkbox.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With