0

I have next html:

<div class="a"> 
   <div class="sub_a"> ... </div> 
</div>
<div class="b"> ... </div>

How I can apply rule for class b, only if class sub_a exists? Only in css way.

Ted
  • 1,193
  • 3
  • 15
  • 32
  • You cannot do it. – Mr. Alien Sep 07 '17 at 09:42
  • @Mr. Alien there is an alternative approach not mentioned in the other question. Please reopen. Also, OP did not ask for a parent selector specifically. – Tom M Sep 07 '17 at 09:47
  • @TomM There's no other approach without changing the DOM. Also, technically, parent selector approach applies here, so till I don't get the exact reason, I am not reopening it as there are lots of questions like this which ends up having JavaScript/jQuery answers. – Mr. Alien Sep 07 '17 at 09:57
  • in OPs example, checking for `:empty` should work – Tom M Sep 07 '17 at 09:57
  • @TomM Won't work either - https://jsfiddle.net/twq5jqts/ – Mr. Alien Sep 07 '17 at 09:59
  • aye it does - https://jsfiddle.net/4o1puaed/1/ – Tom M Sep 07 '17 at 10:02
  • @TomM No it doesn't - https://jsfiddle.net/4o1puaed/3/ :) Also, don't assume that OP has a literal data of `...`, it's just a placeholder here.. he has data in there for sure. – Mr. Alien Sep 07 '17 at 10:05
  • you obviously need to define two rule blocks. One without `:empty` and the one with `:empty` is for when the sub class does not exist. – Tom M Sep 07 '17 at 10:07
  • well whatever. Do your worst – Tom M Sep 07 '17 at 10:08
  • @TomM Not getting your point here, and you don't have to be rude :) – Mr. Alien Sep 07 '17 at 10:13
  • I was not trying to be rude here. Okay, look. If you want to style `.b` only if `.a` has children, then you define a block for `.a + .b`. After that, you define another block `.a:empty + .b` and reset the rule. This means, the style will only apply if `.a` is not empty. – Tom M Sep 07 '17 at 10:18
  • Your fiddle does not work because you only have the latter. – Tom M Sep 07 '17 at 10:18

0 Answers0