Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you limit the scope in HTML?

This answer to this question is probably extremely obvious and I’m just not seeing it, but how do you make sure certain style attributes only apply to one element when referencing the tag that they use.

I currently have a pure css Collapsible I snagged from DigitalOcean and I am using Ember Components.

The code uses a checkbox to activate the Collapsible – which means it hides the checkbox in the style tag referencing the input tag:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<style>
    input[type='checkbox'] {
      display: none;
    }
</style>

...

<input id="collapsible" class="toggle" type="checkbox">

However, in doing so, I end up with all the other checkboxes on the page becoming hidden as well.
(And this style tag lives inside the .hbs Component file for the dropdown, dunno if that changes anything)

How can I target just the Collapsible specifically for hiding the checkbox?
Also let me know if there is any other information that might be helpful 🙂

So far I’ve tried wrapping this attribute inside another attribute:

 .hide-checkbox {
    input[type='checkbox'] {
      display: none;
    }
  }

But that just gave me a text input box instead of a checkbox.

>Solution :

You can target elements in a variety of ways. In your case, if the element has an id, use that since id should be unique anyway:

#collapsible {
  display: none;
}
<input id="collapsible" class="toggle" type="checkbox">

So far I’ve tried wrapping this attribute inside another attribute

That would also work, if your element is wrapped in a matching element:

.hide-checkbox {
  input[type='checkbox'] {
    display: none;
  }
}
<div class="hide-checkbox">
  <input id="collapsible" class="toggle" type="checkbox">
</div>

Or, if you don’t want to introduce a wrapping element, use that class on the target element itself:

input.hide-checkbox[type='checkbox'] {
  display: none;
}
<input id="collapsible" class="toggle hide-checkbox" type="checkbox">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading