2

I am trying to display or hide a paragraph in a html page. The html code is:

<input type="checkbox" id="show"> Show/Hide <p id="toggle">Lorem ipsum dolor sit amet</p> </div> 

Then I use CSS3 to hide/show the paragraph:

 #toggle { display:none; } #show:checked ~ p { display: block; } 

This does work. But if I try:

#toggle { display:none; } input[type='checkbox']:checked ~ p { display: block; } 

this doesn't work anymore. I can't understand why. Is there any error in this selector?

 input[type='checkbox']:checked 

Thanks for helping!!!

4

1 Answer 1

0

This has to do with the CSS specificity.
When looking at the specificity hierarchy , you'll see that id will go above the element.

That means that
#toggle is more specific then:
input[type='checkbox']:checked ~ p
and therefor will the id overrule the element selector.

So, use:

#toggle { display: none; } input[type='checkbox']:checked ~ p#toggle { display: block; }
<div> <input type="checkbox" id="show"> Show/Hide <p id="toggle">Lorem ipsum dolor sit amet</p> </div>

Or:

input[type='checkbox'] ~ p{ display: none; } input[type='checkbox']:checked ~ p { display: block; }
<div> <input type="checkbox" id="show"> Show/Hide <p id="toggle">Lorem ipsum dolor sit amet</p> </div>

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

1 Comment

you'll see that id will go above (pseudo)classes. ? it's about ID and element selector, pseudo class and classes has nothing to do here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.