-7

How can I get the parent element by reference with their child element using pure css?

#checkbox{
  width: 24px;
  height: 24px;
  vertical-align: middle;
}
#checkbox:checked (parentName){
  color: red;
}
<label for="checkbox">
  <input type="checkbox" id="checkbox" />
  click me
</label>

I know we can simply use jQuery to find the parent of the child element. Something like this;-

$(this).parent().css({'color': 'red'});

Is there anyway to identify the parent selector using pure css selector?

Krish
  • 1,884
  • 2
  • 16
  • 40

1 Answers1

1

Is there anyway to identify the parent selector using pure css selector?

No.

Still we don't have parent selector in native css. but in coming future that might be possible as some css methods are now available.

A simple workaround to your issue can be solved like wrapping the text content of parent label in some element like span:

#checkbox{
  width: 24px;
  height: 24px;
  vertical-align: middle;
}
#checkbox:checked + span{
  color: red;
}
<label for="checkbox">
  <input type="checkbox" id="checkbox" />
  <span>click me</span>
</label>
Jai
  • 74,255
  • 12
  • 74
  • 103
  • I do not understand why have people downvoted a concise answer. –  Jul 08 '16 at 12:00
  • @J.C.Rocamonde I'm not the downvoter, but I think this is a over-duplicated question. – Marcos Pérez Gude Jul 08 '16 at 12:01
  • That's not the fault of the user answering. –  Jul 08 '16 at 12:01
  • I guess i just answered what was asked.....that's it. I don't know if anything i miss. – Jai Jul 08 '16 at 12:02
  • @J.C.Rocamonde I agree, but this community works as this. I grow many downvotes in my questions only because some discussions in unrelated topics. Imagine a perfect world.... stackoverflow is not. – Marcos Pérez Gude Jul 08 '16 at 12:05
  • @Jai, you did it the right job. Now i can understand from your answer. – Krish Jul 08 '16 at 12:06
  • @J.C.Rocamonde Because this is a duplicate answer and question and it encourages no research from OP, these questions shouldn't be answered, I can downvote for whatever reason I find valid and this I find valid, -1. – Martin Dawson Jan 13 '18 at 00:11
  • @MartinDawson i answered it because OP has tried something and it shows he might have researched a little. Because he done something and failed to achive that. I answered it. and it almose a year old post. Yet you are welcome. – Jai Jan 13 '18 at 15:30