Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: how to select parent's sibling [duplicate]

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?

like image 206
Typhoon101 Avatar asked Jun 11 '13 15:06

Typhoon101


People also ask

How does CSS choose parent siblings?

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".

How do I select a second sibling in CSS?

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.

Is there a CSS parent selector?

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.

How do I select only the parent element in CSS?

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.


2 Answers

You cannot do that with CSS but

You can try using jQuery

$("#showCheckbox").click(function(){     $(this).parent().siblings().show(); }); 
like image 162
Mohammad Areeb Siddiqui Avatar answered Sep 23 '22 14:09

Mohammad Areeb Siddiqui


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.

like image 33
Sachin Avatar answered Sep 23 '22 14:09

Sachin