I have a solution that helps with non-background related styles. You can see the fiddle http://jsfiddle.net/7ku3g/83/ which is only a roughed concept. Some key points: 1) it is imperative that the prepended styleControl
element is added to the inner most containing element. For example, you had the .highlight
class added to the .bClass
div but there was another full wrapping element, the blockquote
within that. 2) You would want to remove the styleControl
element upon removal of the highlight class.
In general, the idea is to add an invisible styling element first in the inner most wrapper, then use the general sibling selector ~
and :not
selector to style further down elements. Again, this does not solve your background issues, but does seem to be a way to help solve true inheriting properties since those styles are NOT applied to the wrapper div, but to the newly created first sibling in the group and the properties applied based off that sibling only to those they need to be applied to.
One issue with this technique is any "naked text" (see http://jsfiddle.net/7ku3g/84/) does not then get styled. So it is not a whole solution to your issue. EDIT: For such cases, grabbing and adding a carefully crafted span
around those naked nodes might work. Something similar to: Style a certain character in a string.
CSS
.highlight.bClass {
background-image: url('http://hostilefork.com/shared/stackoverflow/cc-by-nc-nd.png');
background-repeat: repeat;
}
.highlight.bClass .styleControl {
width: 0;
height: 0;
position: absolute;
}
.highlight.bClass .styleControl ~ *:not(.cClass) {
color: red;
}
UPDATE: Some further explanation based on HostileFork's comments. First, my solution here only matters if there is a child element that you want to prevent from inheriting styles (actual inheritable styles) you are adding. Second, multiple may need to be added depending on how deep things are nested. Here's some pseudocode html to discuss:
<div> (containing licensed material)
<childA1> (this is the wrapping blockquote in your example; could be anything)
<scriptAddedStyleElementB0>
<childB1>content<endchildB1> (apply styles)
<childB2>content<endchildB2> (don't apply styles)
<childB3> (don't apply styles because a child is not having styles applied)
<scriptAddedStyleElementC0>
<childC1> (apply styles)
<!-- noScriptAddedStyleElementD0 -->
<childD1>content<endchildD1>
<childD2>content<endchildD2>
<endchildC1>
<childC2>content<endchildC2> (don't apply styles)
<endchildB3>
<end-childA1>
<end-div1>
You are targeting the outermost div
to apply the highlight
class to but certain children you don't want affected. Note how A1 and B3 don't contain any content (text/images) themselves, they just contain other elements. Those are "wrappers" in my use of the term (they are just used to group their children, though they may have styling themselves).
To answer your question of "when the delving needs to stop," it is when you reach a point where you no longer care about styles inheriting. In the example above, 2 styling elements would need to be added, one at B0 and one at C0 but not at D0. This is because the general sibling selector ~
only applies to elements that have the same parent and that follow that sibling. So B0 is going to apply the styles to B1 but not B2 (non-licensed) or B3 (contains a non-licensed). Element C0 is going to apply the styles to C1 (and its licensed children D1 and D2) but avoid styling C2. The content of B2 and C2 would still be inheriting the original styles from the wrapping div (C2 by way of B3), while B1, C1, D1, D2 would all get the licensing styling.
The difficulties in this are: 1) earlier versions of IE (7 and 8) do not recognize some of this css, specifically the :not
selector. That might potentially be worked around. 2) my previous note about "naked text" or content. If B3 looked like:
<childB3>
<scriptAddedStyleElementC0>
Some unwrapped text
<childC1>...<endchildC1>
Some other unwrapped text and an <img> tag
<childC2>...<endchildC2>
More unwrapped text
<endchildB3>
If B3 unwrapped text was needing to be styled for licensing (turned red in your example) then you would need to locate all the text nodes via javascript and wrap them in span
elements to apply the styling, because if you apply the style to B3, then it will inherit to C2 which you don't want.
There is no doubt that what you are looking for is difficult to achieve. It would have been impossible via CSS prior to the general sibling selector coming about, and it may get easier with CSS4 standards that are being discussed (being able to style parent elements from children might be leveraged for this situation).