0

For a custom stylesheet (via Stylish) I want to apply a style to element A only if another element B has a certain attribute set:

<section>
    <div id="some-element" class="optional-class">...</div>
</section>
...
<section>
    <div id="concrete-element">...</div>
</section>
#concrete-element {
    /* become pretty */
}

Now I want #concrete-element to become pretty only if #some-element has the .optional-class class attribute.

Is there any way I can solve this without JavaScript?

x3ro
  • 329
  • 1
  • 5
  • 11
  • 3
    No, CSS alone can't do that. – Scimonster Feb 24 '15 at 16:53
  • I'm voting to close this question as off-topic because the answer is no – Liam Feb 24 '15 at 16:54
  • 6
    @Liam - Really? – BoltClock Feb 24 '15 at 16:54
  • The answer is no because `#some-element` and `concrete-element` doesn't have the same parent. If `#some-element` was out of `section` it could be selected. – emmanuel Feb 24 '15 at 16:57
  • CSS doesn't yet have a parent selector, so there's no way to traverse the DOM in the way you need without JavaScript. – j08691 Feb 24 '15 at 16:58
  • 3
    @Paulie_D: I'm starting to wonder if there's a better way of handling these questions that aren't necessarily straight-up "is there a parent selector?" even though they *boil down* to that. – BoltClock Feb 24 '15 at 17:01
  • @BoltClock I'm with you but any other response would probably be too broad or opinion based - I dunno. – Paulie_D Feb 24 '15 at 17:03
  • @Paulie_D: True, there isn't much more of an answer anyway than "it has to be expressed using selectors, and there is simply no parent selector / no previous sibling selector / etc". Although that is neither broad nor opinion-based, in fact it's pretty straightforward. But it essentially just points to existing questions. – BoltClock Feb 24 '15 at 17:04
  • 1
    @BoltClock By "response" I meant actually trying to achieve the intended result in an **answer** form. I could redo the HTML to get it to do what the OP wanted but that. in itself, would just be **my** interpretation of what is required. So, potentially too broad and opinion based. – Paulie_D Feb 24 '15 at 17:11
  • @Paulie_D: Ah yeah I see what you mean. In that case, a non-broad and non-opinionated version would be "you will need to modify the HTML". There is almost always more than one way to skin a cat, but if they all boil down to one common principle, then that's not really broad. – BoltClock Feb 24 '15 at 17:47
  • Why is this marked as a duplicate? I don't want a parent selector, I need to know if there is a way to apply a style to one element if a completely other element (that not necessarily has to be a child of it) has a certain attribute. And in case it was not clear enough: This is for a custom style. I cannot modify the HTML (unless I use a userscript to manipulate the dom) as I do not own YouTube... – x3ro Feb 25 '15 at 11:24
  • But thanks @Scimonster, I was afraid it won't work – x3ro Feb 25 '15 at 11:28

0 Answers0