Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

HTML & CSS trouble with css selectors combining

MercuryRising

New Coder
This code:
[CODE lang="css" title="Old CSS" highlight="1"].cl1 clChk:checked ~ cl2 {
opacity: 1;
height: auto;
}[/CODE]

worked perfectly fine with this structure:
[CODE lang="html" title="old html" highlight="2,3"]<div class="cl1">
<label class="clLabel" for="chk_1"></label>
<input class="clChk" type="checkbox" id="chk_1">

<article class="cl2">
..some text..
</article>
</div>[/CODE]

But i do not know how to change the css for this html structure to get the same result:
[CODE lang="html" title="New Html" highlight="2"]<div class="cl1">
<label class="clLabel"><input class="clChk" type="checkbox"></label>

<article class="cl2">
..some text..
</article>
</div[/CODE]

I have tried many variations, but i cannot seem to get it to work; so if the checkbox isn't nested in the label, there is no issue, but i need the checkbox nested in the <label>.

Thank you so much for your help!
 
I think the problem is that the ~ selector selects from siblings of the selector. When the label, input and article are all on the same level, they are siblings, but with the input inside of the label it is no longer a sibling to article.

You could put the article inside of the label, that might work. It's not really a good thing to do though because the label would apply to the whole article.

What if you made some Javascript that listened for the click on the checkbox and added a class to the article to make it visible?
 
Thank you for responding.

There is noway to do this with pure css?, because i cannot use javascript on this page, that's prohibited unfortunately.

I can do:
CSS:
.cl1 clLabel ~ cl2 {
, but then i am missing if the checkbox is indeed checked or not (and all the article text is always shown, since it's always true.
Is there a way to compound it? to have the statement as written just above here , but then on the next something like :

Code:
clChk:checked {

}
?
(i tried like this it doesn't work, but it has to be possible somehow right? with pseudo selectors or something?
 
Heres's a solution to the given problem for you need to target the changes with help of id and use pseudo-class to trigger the change like so

[CODE lang="css" title="CSS styling"]#chk_1:checked + .cl2 {
opacity: 1;
}
.cl2 {
opacity: 0;
}[/CODE]

[CODE lang="html" title="HTML"]<div class="cl1">
<label for="chk_1" class="clLabel"></label>
<input id="chk_1" type="checkbox" />

<article class="cl2">
..some text..
</article>
</div>[/CODE]

Learn more about selectors
 
Last edited by a moderator:
Heres's a solution to the given problem for you need to target the changes with help of id and use pseudo-class to trigger the change like so

[CODE lang="css" title="CSS styling"]#chk_1:checked + .cl2 {
opacity: 1;
}
.cl2 {
opacity: 0;
}[/CODE]

[CODE lang="html" title="HTML"]<div class="cl1">
<label for="chk_1" class="clLabel"></label>
<input id="chk_1" type="checkbox" />

<article class="cl2">
..some text..
</article>
</div>[/CODE]

Learn more about selectors
Thank you, but I specifically cannot use the id structure, which is why i am in need of a solution with the nested check boxes; else the old html and css are enough...
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom