SCSS : change element style if child input is checked or not
Asked Answered
I

4

11

I have never used SCSS before and I would liketo know if it would allow me to style an element depending if the checkbox located inside is checked or not.

For instance, I have :

<label>
  <input type="checkbox"/>
</label>

and I would liketo change the content of label::before depending if the box is checked or not.

Does SCSS allows things like this ? If yes,how can I do it ?

The reason that led me to consider SCSS to execute this is that (if I'm not mistaking) it is indeed possible with SASS.
However since I am not familiar with SASS I'd ratherdo it with SCSS, as converting my file is apparently very easy.

Inconsistent answered 19/10, 2016 at 0:1 Comment(3)
Possible duplicate of CSS selector for a checked radio button's labelAriel
This is about targeting a label outside the target element, am actually trying to target the parent element, so this unfortunately doesn't apply to my situation.Inconsistent
Sure, but a minor restructuring of the HTML would allow you to target the label.Ariel
O
9

No, there is no CSS selector that allows one to style an element based on the state of its children.


Update: The :has() pseudo-class was introduced in 2022, and is supported by all major browsers except Firefox as of June 2023.

You can now style parent elements based on child state like so:

label::before {
  content: 'Not checked';
}

label:has(input:checked) {
  background: yellow;
}

label:has(input:checked)::before {
  content: 'Checked!';
}
<label>
  <input type="checkbox" />
</label>
Olsson answered 19/10, 2016 at 0:9 Comment(0)
H
12

I did a little change in your HTML, but I think it can really help you.

Sass code:

input[type=checkbox]
  + .checkbox-label
    color: blue
  &:checked + .checkbox-label
    color: red
    &:before
      content: ""
      border: 1px solid #a3adb3

input[type=checkbox]+.checkbox-label {
  color: blue;
}

input[type=checkbox]:checked+.checkbox-label {
  color: red;
}

input[type=checkbox]:checked+.checkbox-label:before {
  content: "";
  border: 1px solid #a3adb3;
}
<input type="checkbox" id="cbox1" value="first_checkbox">
<label for="cbox1" class="checkbox-label">Hello world</label>

I have helped you in some way.

Hujsak answered 19/10, 2016 at 0:36 Comment(3)
Oh shit, I stand corrected! :) I always forget about sibling selectors, damn.Jerryjerrybuild
Don't worry. The important thing is always the exchange of knowledge @Jerryjerrybuild :)Hujsak
Yes indeed that would work ! I haven't decided switchingto sass yet, but I am considering it to implement this type of behaviour without JS. ThanksInconsistent
O
9

No, there is no CSS selector that allows one to style an element based on the state of its children.


Update: The :has() pseudo-class was introduced in 2022, and is supported by all major browsers except Firefox as of June 2023.

You can now style parent elements based on child state like so:

label::before {
  content: 'Not checked';
}

label:has(input:checked) {
  background: yellow;
}

label:has(input:checked)::before {
  content: 'Checked!';
}
<label>
  <input type="checkbox" />
</label>
Olsson answered 19/10, 2016 at 0:9 Comment(0)
J
4

You wouldn't be able to change the content of the label based on the state of the input, you'd need JS for that.

However, you can use pseudo selector in regular ol CSS (and SCSS by extension) to style the checkbox itself using input:checked{}

EDIT: I stand corrected, see Leonardo Costa's solution using sibling selectors.

Jerryjerrybuild answered 19/10, 2016 at 0:8 Comment(0)
C
1

As of today you can use the new :has() pseudo-class as a parent selector:

label:has(:checked)::before {
    content: "«";
    color: blue;
}
Convoke answered 7/11, 2022 at 16:53 Comment(3)
except in firefox: caniuse.com/?search=hasRichrichara
By default, but it can be enabled in about:config by setting the layout.css.has-selector.enabled flag to true (See notes in the link)Convoke
yes, but it's important to note since you wouldn't want to ship it to production just yetRichrichara

© 2022 - 2024 — McMap. All rights reserved.