0

I want to toggle a <div> via a checkbox.


<div class="docs">...</div>

...

<input type="checkbox" id="toggle-docs">

The docs should only be visible if the checkbox is checked.

This should work without JS.

Up to now I only found solutions which require the checkbox to be near the div (via + or ~ selector).

Is there a way to solve this with modern CSS?

1 Answer 1

1

This is doable with :has, which has baseline support since December 2023.

div.docs {
    display: none;
}

body:has(#toggle-docs:checked) div.docs  {
    display: block;
}
<div class="docs">...</div>

<input type="checkbox" id="toggle-docs">

This shows docs only if the checkbox is checked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.