Does CSS ever care about DOM “closeness” relationships?











up vote
1
down vote

favorite












Given the following code:






div, span {
padding: 10px;
display: block;
}

.light-background {
background-color: #cacaca;
}

.dark-background {
background-color: #333;
}

.dark-background span {
color: white;
}

.light-background span {
color: black;
}

<div class="light-background">
<div class="dark-background">
<span>Here is some light text on a dark background.</span>

<div class="light-background">
<span>Here is some dark text on a light background.</span>
</div>
</div>
<span>Here is some dark text on a light background.</span>
</div>





The inner-most span matches both .dark-background span as well as .light-background span, so there seems to only ever be a relationship to the CSS cascade weight and the last-rule-defined cascade, and never how close the two selectors in a rule are to each other in the HTML.



Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?










share|improve this question




















  • 2




    Maybe you're looking for > or ~ in a selector? But no, no specific selector for a "close" element
    – SuperDJ
    Nov 11 at 18:52












  • you firt need to define "closeness". For me both are closely equal
    – Temani Afif
    Nov 11 at 19:03










  • By "closeness" I mean more directly-descendant. E.g., the span which does not follow the rule (the first span) has a hierarchy in DOM as .light-background .dark-background span, but although the "closest" html structure is .dark-background span, this is ignored because the selector .light-background span is defined after .dark-background span
    – mindfullsilence
    Nov 11 at 19:31










  • directly-descendant is this selector >
    – Temani Afif
    Nov 11 at 19:43















up vote
1
down vote

favorite












Given the following code:






div, span {
padding: 10px;
display: block;
}

.light-background {
background-color: #cacaca;
}

.dark-background {
background-color: #333;
}

.dark-background span {
color: white;
}

.light-background span {
color: black;
}

<div class="light-background">
<div class="dark-background">
<span>Here is some light text on a dark background.</span>

<div class="light-background">
<span>Here is some dark text on a light background.</span>
</div>
</div>
<span>Here is some dark text on a light background.</span>
</div>





The inner-most span matches both .dark-background span as well as .light-background span, so there seems to only ever be a relationship to the CSS cascade weight and the last-rule-defined cascade, and never how close the two selectors in a rule are to each other in the HTML.



Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?










share|improve this question




















  • 2




    Maybe you're looking for > or ~ in a selector? But no, no specific selector for a "close" element
    – SuperDJ
    Nov 11 at 18:52












  • you firt need to define "closeness". For me both are closely equal
    – Temani Afif
    Nov 11 at 19:03










  • By "closeness" I mean more directly-descendant. E.g., the span which does not follow the rule (the first span) has a hierarchy in DOM as .light-background .dark-background span, but although the "closest" html structure is .dark-background span, this is ignored because the selector .light-background span is defined after .dark-background span
    – mindfullsilence
    Nov 11 at 19:31










  • directly-descendant is this selector >
    – Temani Afif
    Nov 11 at 19:43













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Given the following code:






div, span {
padding: 10px;
display: block;
}

.light-background {
background-color: #cacaca;
}

.dark-background {
background-color: #333;
}

.dark-background span {
color: white;
}

.light-background span {
color: black;
}

<div class="light-background">
<div class="dark-background">
<span>Here is some light text on a dark background.</span>

<div class="light-background">
<span>Here is some dark text on a light background.</span>
</div>
</div>
<span>Here is some dark text on a light background.</span>
</div>





The inner-most span matches both .dark-background span as well as .light-background span, so there seems to only ever be a relationship to the CSS cascade weight and the last-rule-defined cascade, and never how close the two selectors in a rule are to each other in the HTML.



Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?










share|improve this question















Given the following code:






div, span {
padding: 10px;
display: block;
}

.light-background {
background-color: #cacaca;
}

.dark-background {
background-color: #333;
}

.dark-background span {
color: white;
}

.light-background span {
color: black;
}

<div class="light-background">
<div class="dark-background">
<span>Here is some light text on a dark background.</span>

<div class="light-background">
<span>Here is some dark text on a light background.</span>
</div>
</div>
<span>Here is some dark text on a light background.</span>
</div>





The inner-most span matches both .dark-background span as well as .light-background span, so there seems to only ever be a relationship to the CSS cascade weight and the last-rule-defined cascade, and never how close the two selectors in a rule are to each other in the HTML.



Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?






div, span {
padding: 10px;
display: block;
}

.light-background {
background-color: #cacaca;
}

.dark-background {
background-color: #333;
}

.dark-background span {
color: white;
}

.light-background span {
color: black;
}

<div class="light-background">
<div class="dark-background">
<span>Here is some light text on a dark background.</span>

<div class="light-background">
<span>Here is some dark text on a light background.</span>
</div>
</div>
<span>Here is some dark text on a light background.</span>
</div>





div, span {
padding: 10px;
display: block;
}

.light-background {
background-color: #cacaca;
}

.dark-background {
background-color: #333;
}

.dark-background span {
color: white;
}

.light-background span {
color: black;
}

<div class="light-background">
<div class="dark-background">
<span>Here is some light text on a dark background.</span>

<div class="light-background">
<span>Here is some dark text on a light background.</span>
</div>
</div>
<span>Here is some dark text on a light background.</span>
</div>






html css css-selectors






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 19:06









Temani Afif

62.7k93574




62.7k93574










asked Nov 11 at 18:47









mindfullsilence

43829




43829








  • 2




    Maybe you're looking for > or ~ in a selector? But no, no specific selector for a "close" element
    – SuperDJ
    Nov 11 at 18:52












  • you firt need to define "closeness". For me both are closely equal
    – Temani Afif
    Nov 11 at 19:03










  • By "closeness" I mean more directly-descendant. E.g., the span which does not follow the rule (the first span) has a hierarchy in DOM as .light-background .dark-background span, but although the "closest" html structure is .dark-background span, this is ignored because the selector .light-background span is defined after .dark-background span
    – mindfullsilence
    Nov 11 at 19:31










  • directly-descendant is this selector >
    – Temani Afif
    Nov 11 at 19:43














  • 2




    Maybe you're looking for > or ~ in a selector? But no, no specific selector for a "close" element
    – SuperDJ
    Nov 11 at 18:52












  • you firt need to define "closeness". For me both are closely equal
    – Temani Afif
    Nov 11 at 19:03










  • By "closeness" I mean more directly-descendant. E.g., the span which does not follow the rule (the first span) has a hierarchy in DOM as .light-background .dark-background span, but although the "closest" html structure is .dark-background span, this is ignored because the selector .light-background span is defined after .dark-background span
    – mindfullsilence
    Nov 11 at 19:31










  • directly-descendant is this selector >
    – Temani Afif
    Nov 11 at 19:43








2




2




Maybe you're looking for > or ~ in a selector? But no, no specific selector for a "close" element
– SuperDJ
Nov 11 at 18:52






Maybe you're looking for > or ~ in a selector? But no, no specific selector for a "close" element
– SuperDJ
Nov 11 at 18:52














you firt need to define "closeness". For me both are closely equal
– Temani Afif
Nov 11 at 19:03




you firt need to define "closeness". For me both are closely equal
– Temani Afif
Nov 11 at 19:03












By "closeness" I mean more directly-descendant. E.g., the span which does not follow the rule (the first span) has a hierarchy in DOM as .light-background .dark-background span, but although the "closest" html structure is .dark-background span, this is ignored because the selector .light-background span is defined after .dark-background span
– mindfullsilence
Nov 11 at 19:31




By "closeness" I mean more directly-descendant. E.g., the span which does not follow the rule (the first span) has a hierarchy in DOM as .light-background .dark-background span, but although the "closest" html structure is .dark-background span, this is ignored because the selector .light-background span is defined after .dark-background span
– mindfullsilence
Nov 11 at 19:31












directly-descendant is this selector >
– Temani Afif
Nov 11 at 19:43




directly-descendant is this selector >
– Temani Afif
Nov 11 at 19:43












2 Answers
2






active

oldest

votes

















up vote
2
down vote













Before addressing your question, which is a high-level question about selector matching in general, let's get your actual problem out of the way. All you're really trying to do is style each span based on whether its parent is a .light-background or a .dark-background, and the solution for the problem in your CSS is simply to replace the descendant combinator with the child combinator:



.dark-background > span {
color: white;
}

.light-background > span {
color: black;
}





div, span {
padding: 10px;
display: block;
}

.light-background {
background-color: #cacaca;
}

.dark-background {
background-color: #333;
}

.dark-background > span {
color: white;
}

.light-background > span {
color: black;
}

<div class="light-background">
<div class="dark-background">
<span>Here is some light text on a dark background.</span>

<div class="light-background">
<span>Here is some dark text on a light background.</span>
</div>
</div>
<span>Here is some dark text on a light background.</span>
</div>





With that out of the way, why does your approach with descendant selectors not work as expected to begin with? That's where we turn to your question:




Does CSS ever care about DOM “closeness” relationships?




No, complex selectors matching the same element are compared only by specificity. And specificity does not take into account the proximity of the elements matched by each compound selector, because this requires information about the DOM, and specificity is never calculated based on any information about the DOM. Likewise, combinators themselves do not contribute to specificity.



Given the following example:



<div class="A">
<div class="B">
<div class="C"></div>
<div class="D"></div>
<div class="E"></div>
</div>
</div>


In each of these pairs, both selectors match the same element and are equally specific; therefore the second rule will always take precedence over the first:



.B .C   {}
.B > .C {}

.B > .C {}
.B .C {}

.A .C {}
.B .C {}

.B .C {}
.A .C {}

.D ~ .E {}
.D + .E {}

.D + .E {}
.D ~ .E {}

.C ~ .E {}
.D ~ .E {}

.D ~ .E {}
.C ~ .E {}



Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?




No, this is currently not possible. css-values-3 has a proposed feature called toggle() that will aid in solving problems that are somewhat similar to yours but not quite the same. But there hasn't been any interest in implementing it for the last several years, so it's been punted to level 4, and I don't expect implementations to surface for at least the next 5 years.






share|improve this answer























  • Saying 'XY problem' is a Z problem. Let's not parrot this obtuseness.
    – Jan Kyu Peblik
    Nov 12 at 7:43










  • @Jan Kyu Peblik: I'd considered ignoring the example altogether and addressing the question on its own merits, but I figured there's a reason they included their example, however detached it was from their question. I can remove my statement calling this an XY problem, but it still wouldn't change the nature of the question, or my answer as it now stands.
    – BoltClock
    Nov 12 at 7:55












  • It would change the inclusion of that bit of nonsense in your question, which would be great.
    – Jan Kyu Peblik
    Nov 13 at 23:57










  • @Jan Kyu Peblik: OK, I've removed it.
    – BoltClock
    Nov 14 at 9:00










  • Appreciated. 21
    – Jan Kyu Peblik
    Nov 15 at 17:10


















up vote
0
down vote













If you re-order the declarations, you'll see the text-color change. Later definitions override sooner ones.



You can use the > operator to restrict your selection somewhat #main > span will match only spans immediately beneath #main, the + selector selects the next-sibling in the dom. & you've got nth-child in/around that type of selection. Is that what you meant by:




Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?







div, span {
padding: 10px;
display: block;
}

.light-background {
background-color: #cacaca;
}

.dark-background {
background-color: #333;
}

.light-background span {
color: black;
}

.dark-background span {
color: white;
}

<div class="light-background">
<div class="dark-background">
<span>Here is some light text on a dark background.</span>

<div class="light-background">
<span>Here is some dark text on a light background.</span>
</div>
</div>
<span>Here is some dark text on a light background.</span>
</div>








share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252000%2fdoes-css-ever-care-about-dom-closeness-relationships%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Before addressing your question, which is a high-level question about selector matching in general, let's get your actual problem out of the way. All you're really trying to do is style each span based on whether its parent is a .light-background or a .dark-background, and the solution for the problem in your CSS is simply to replace the descendant combinator with the child combinator:



    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }





    div, span {
    padding: 10px;
    display: block;
    }

    .light-background {
    background-color: #cacaca;
    }

    .dark-background {
    background-color: #333;
    }

    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }

    <div class="light-background">
    <div class="dark-background">
    <span>Here is some light text on a dark background.</span>

    <div class="light-background">
    <span>Here is some dark text on a light background.</span>
    </div>
    </div>
    <span>Here is some dark text on a light background.</span>
    </div>





    With that out of the way, why does your approach with descendant selectors not work as expected to begin with? That's where we turn to your question:




    Does CSS ever care about DOM “closeness” relationships?




    No, complex selectors matching the same element are compared only by specificity. And specificity does not take into account the proximity of the elements matched by each compound selector, because this requires information about the DOM, and specificity is never calculated based on any information about the DOM. Likewise, combinators themselves do not contribute to specificity.



    Given the following example:



    <div class="A">
    <div class="B">
    <div class="C"></div>
    <div class="D"></div>
    <div class="E"></div>
    </div>
    </div>


    In each of these pairs, both selectors match the same element and are equally specific; therefore the second rule will always take precedence over the first:



    .B .C   {}
    .B > .C {}

    .B > .C {}
    .B .C {}

    .A .C {}
    .B .C {}

    .B .C {}
    .A .C {}

    .D ~ .E {}
    .D + .E {}

    .D + .E {}
    .D ~ .E {}

    .C ~ .E {}
    .D ~ .E {}

    .D ~ .E {}
    .C ~ .E {}



    Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?




    No, this is currently not possible. css-values-3 has a proposed feature called toggle() that will aid in solving problems that are somewhat similar to yours but not quite the same. But there hasn't been any interest in implementing it for the last several years, so it's been punted to level 4, and I don't expect implementations to surface for at least the next 5 years.






    share|improve this answer























    • Saying 'XY problem' is a Z problem. Let's not parrot this obtuseness.
      – Jan Kyu Peblik
      Nov 12 at 7:43










    • @Jan Kyu Peblik: I'd considered ignoring the example altogether and addressing the question on its own merits, but I figured there's a reason they included their example, however detached it was from their question. I can remove my statement calling this an XY problem, but it still wouldn't change the nature of the question, or my answer as it now stands.
      – BoltClock
      Nov 12 at 7:55












    • It would change the inclusion of that bit of nonsense in your question, which would be great.
      – Jan Kyu Peblik
      Nov 13 at 23:57










    • @Jan Kyu Peblik: OK, I've removed it.
      – BoltClock
      Nov 14 at 9:00










    • Appreciated. 21
      – Jan Kyu Peblik
      Nov 15 at 17:10















    up vote
    2
    down vote













    Before addressing your question, which is a high-level question about selector matching in general, let's get your actual problem out of the way. All you're really trying to do is style each span based on whether its parent is a .light-background or a .dark-background, and the solution for the problem in your CSS is simply to replace the descendant combinator with the child combinator:



    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }





    div, span {
    padding: 10px;
    display: block;
    }

    .light-background {
    background-color: #cacaca;
    }

    .dark-background {
    background-color: #333;
    }

    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }

    <div class="light-background">
    <div class="dark-background">
    <span>Here is some light text on a dark background.</span>

    <div class="light-background">
    <span>Here is some dark text on a light background.</span>
    </div>
    </div>
    <span>Here is some dark text on a light background.</span>
    </div>





    With that out of the way, why does your approach with descendant selectors not work as expected to begin with? That's where we turn to your question:




    Does CSS ever care about DOM “closeness” relationships?




    No, complex selectors matching the same element are compared only by specificity. And specificity does not take into account the proximity of the elements matched by each compound selector, because this requires information about the DOM, and specificity is never calculated based on any information about the DOM. Likewise, combinators themselves do not contribute to specificity.



    Given the following example:



    <div class="A">
    <div class="B">
    <div class="C"></div>
    <div class="D"></div>
    <div class="E"></div>
    </div>
    </div>


    In each of these pairs, both selectors match the same element and are equally specific; therefore the second rule will always take precedence over the first:



    .B .C   {}
    .B > .C {}

    .B > .C {}
    .B .C {}

    .A .C {}
    .B .C {}

    .B .C {}
    .A .C {}

    .D ~ .E {}
    .D + .E {}

    .D + .E {}
    .D ~ .E {}

    .C ~ .E {}
    .D ~ .E {}

    .D ~ .E {}
    .C ~ .E {}



    Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?




    No, this is currently not possible. css-values-3 has a proposed feature called toggle() that will aid in solving problems that are somewhat similar to yours but not quite the same. But there hasn't been any interest in implementing it for the last several years, so it's been punted to level 4, and I don't expect implementations to surface for at least the next 5 years.






    share|improve this answer























    • Saying 'XY problem' is a Z problem. Let's not parrot this obtuseness.
      – Jan Kyu Peblik
      Nov 12 at 7:43










    • @Jan Kyu Peblik: I'd considered ignoring the example altogether and addressing the question on its own merits, but I figured there's a reason they included their example, however detached it was from their question. I can remove my statement calling this an XY problem, but it still wouldn't change the nature of the question, or my answer as it now stands.
      – BoltClock
      Nov 12 at 7:55












    • It would change the inclusion of that bit of nonsense in your question, which would be great.
      – Jan Kyu Peblik
      Nov 13 at 23:57










    • @Jan Kyu Peblik: OK, I've removed it.
      – BoltClock
      Nov 14 at 9:00










    • Appreciated. 21
      – Jan Kyu Peblik
      Nov 15 at 17:10













    up vote
    2
    down vote










    up vote
    2
    down vote









    Before addressing your question, which is a high-level question about selector matching in general, let's get your actual problem out of the way. All you're really trying to do is style each span based on whether its parent is a .light-background or a .dark-background, and the solution for the problem in your CSS is simply to replace the descendant combinator with the child combinator:



    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }





    div, span {
    padding: 10px;
    display: block;
    }

    .light-background {
    background-color: #cacaca;
    }

    .dark-background {
    background-color: #333;
    }

    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }

    <div class="light-background">
    <div class="dark-background">
    <span>Here is some light text on a dark background.</span>

    <div class="light-background">
    <span>Here is some dark text on a light background.</span>
    </div>
    </div>
    <span>Here is some dark text on a light background.</span>
    </div>





    With that out of the way, why does your approach with descendant selectors not work as expected to begin with? That's where we turn to your question:




    Does CSS ever care about DOM “closeness” relationships?




    No, complex selectors matching the same element are compared only by specificity. And specificity does not take into account the proximity of the elements matched by each compound selector, because this requires information about the DOM, and specificity is never calculated based on any information about the DOM. Likewise, combinators themselves do not contribute to specificity.



    Given the following example:



    <div class="A">
    <div class="B">
    <div class="C"></div>
    <div class="D"></div>
    <div class="E"></div>
    </div>
    </div>


    In each of these pairs, both selectors match the same element and are equally specific; therefore the second rule will always take precedence over the first:



    .B .C   {}
    .B > .C {}

    .B > .C {}
    .B .C {}

    .A .C {}
    .B .C {}

    .B .C {}
    .A .C {}

    .D ~ .E {}
    .D + .E {}

    .D + .E {}
    .D ~ .E {}

    .C ~ .E {}
    .D ~ .E {}

    .D ~ .E {}
    .C ~ .E {}



    Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?




    No, this is currently not possible. css-values-3 has a proposed feature called toggle() that will aid in solving problems that are somewhat similar to yours but not quite the same. But there hasn't been any interest in implementing it for the last several years, so it's been punted to level 4, and I don't expect implementations to surface for at least the next 5 years.






    share|improve this answer














    Before addressing your question, which is a high-level question about selector matching in general, let's get your actual problem out of the way. All you're really trying to do is style each span based on whether its parent is a .light-background or a .dark-background, and the solution for the problem in your CSS is simply to replace the descendant combinator with the child combinator:



    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }





    div, span {
    padding: 10px;
    display: block;
    }

    .light-background {
    background-color: #cacaca;
    }

    .dark-background {
    background-color: #333;
    }

    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }

    <div class="light-background">
    <div class="dark-background">
    <span>Here is some light text on a dark background.</span>

    <div class="light-background">
    <span>Here is some dark text on a light background.</span>
    </div>
    </div>
    <span>Here is some dark text on a light background.</span>
    </div>





    With that out of the way, why does your approach with descendant selectors not work as expected to begin with? That's where we turn to your question:




    Does CSS ever care about DOM “closeness” relationships?




    No, complex selectors matching the same element are compared only by specificity. And specificity does not take into account the proximity of the elements matched by each compound selector, because this requires information about the DOM, and specificity is never calculated based on any information about the DOM. Likewise, combinators themselves do not contribute to specificity.



    Given the following example:



    <div class="A">
    <div class="B">
    <div class="C"></div>
    <div class="D"></div>
    <div class="E"></div>
    </div>
    </div>


    In each of these pairs, both selectors match the same element and are equally specific; therefore the second rule will always take precedence over the first:



    .B .C   {}
    .B > .C {}

    .B > .C {}
    .B .C {}

    .A .C {}
    .B .C {}

    .B .C {}
    .A .C {}

    .D ~ .E {}
    .D + .E {}

    .D + .E {}
    .D ~ .E {}

    .C ~ .E {}
    .D ~ .E {}

    .D ~ .E {}
    .C ~ .E {}



    Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?




    No, this is currently not possible. css-values-3 has a proposed feature called toggle() that will aid in solving problems that are somewhat similar to yours but not quite the same. But there hasn't been any interest in implementing it for the last several years, so it's been punted to level 4, and I don't expect implementations to surface for at least the next 5 years.






    div, span {
    padding: 10px;
    display: block;
    }

    .light-background {
    background-color: #cacaca;
    }

    .dark-background {
    background-color: #333;
    }

    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }

    <div class="light-background">
    <div class="dark-background">
    <span>Here is some light text on a dark background.</span>

    <div class="light-background">
    <span>Here is some dark text on a light background.</span>
    </div>
    </div>
    <span>Here is some dark text on a light background.</span>
    </div>





    div, span {
    padding: 10px;
    display: block;
    }

    .light-background {
    background-color: #cacaca;
    }

    .dark-background {
    background-color: #333;
    }

    .dark-background > span {
    color: white;
    }

    .light-background > span {
    color: black;
    }

    <div class="light-background">
    <div class="dark-background">
    <span>Here is some light text on a dark background.</span>

    <div class="light-background">
    <span>Here is some dark text on a light background.</span>
    </div>
    </div>
    <span>Here is some dark text on a light background.</span>
    </div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 at 8:45

























    answered Nov 12 at 7:32









    BoltClock

    513k12711511191




    513k12711511191












    • Saying 'XY problem' is a Z problem. Let's not parrot this obtuseness.
      – Jan Kyu Peblik
      Nov 12 at 7:43










    • @Jan Kyu Peblik: I'd considered ignoring the example altogether and addressing the question on its own merits, but I figured there's a reason they included their example, however detached it was from their question. I can remove my statement calling this an XY problem, but it still wouldn't change the nature of the question, or my answer as it now stands.
      – BoltClock
      Nov 12 at 7:55












    • It would change the inclusion of that bit of nonsense in your question, which would be great.
      – Jan Kyu Peblik
      Nov 13 at 23:57










    • @Jan Kyu Peblik: OK, I've removed it.
      – BoltClock
      Nov 14 at 9:00










    • Appreciated. 21
      – Jan Kyu Peblik
      Nov 15 at 17:10


















    • Saying 'XY problem' is a Z problem. Let's not parrot this obtuseness.
      – Jan Kyu Peblik
      Nov 12 at 7:43










    • @Jan Kyu Peblik: I'd considered ignoring the example altogether and addressing the question on its own merits, but I figured there's a reason they included their example, however detached it was from their question. I can remove my statement calling this an XY problem, but it still wouldn't change the nature of the question, or my answer as it now stands.
      – BoltClock
      Nov 12 at 7:55












    • It would change the inclusion of that bit of nonsense in your question, which would be great.
      – Jan Kyu Peblik
      Nov 13 at 23:57










    • @Jan Kyu Peblik: OK, I've removed it.
      – BoltClock
      Nov 14 at 9:00










    • Appreciated. 21
      – Jan Kyu Peblik
      Nov 15 at 17:10
















    Saying 'XY problem' is a Z problem. Let's not parrot this obtuseness.
    – Jan Kyu Peblik
    Nov 12 at 7:43




    Saying 'XY problem' is a Z problem. Let's not parrot this obtuseness.
    – Jan Kyu Peblik
    Nov 12 at 7:43












    @Jan Kyu Peblik: I'd considered ignoring the example altogether and addressing the question on its own merits, but I figured there's a reason they included their example, however detached it was from their question. I can remove my statement calling this an XY problem, but it still wouldn't change the nature of the question, or my answer as it now stands.
    – BoltClock
    Nov 12 at 7:55






    @Jan Kyu Peblik: I'd considered ignoring the example altogether and addressing the question on its own merits, but I figured there's a reason they included their example, however detached it was from their question. I can remove my statement calling this an XY problem, but it still wouldn't change the nature of the question, or my answer as it now stands.
    – BoltClock
    Nov 12 at 7:55














    It would change the inclusion of that bit of nonsense in your question, which would be great.
    – Jan Kyu Peblik
    Nov 13 at 23:57




    It would change the inclusion of that bit of nonsense in your question, which would be great.
    – Jan Kyu Peblik
    Nov 13 at 23:57












    @Jan Kyu Peblik: OK, I've removed it.
    – BoltClock
    Nov 14 at 9:00




    @Jan Kyu Peblik: OK, I've removed it.
    – BoltClock
    Nov 14 at 9:00












    Appreciated. 21
    – Jan Kyu Peblik
    Nov 15 at 17:10




    Appreciated. 21
    – Jan Kyu Peblik
    Nov 15 at 17:10












    up vote
    0
    down vote













    If you re-order the declarations, you'll see the text-color change. Later definitions override sooner ones.



    You can use the > operator to restrict your selection somewhat #main > span will match only spans immediately beneath #main, the + selector selects the next-sibling in the dom. & you've got nth-child in/around that type of selection. Is that what you meant by:




    Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?







    div, span {
    padding: 10px;
    display: block;
    }

    .light-background {
    background-color: #cacaca;
    }

    .dark-background {
    background-color: #333;
    }

    .light-background span {
    color: black;
    }

    .dark-background span {
    color: white;
    }

    <div class="light-background">
    <div class="dark-background">
    <span>Here is some light text on a dark background.</span>

    <div class="light-background">
    <span>Here is some dark text on a light background.</span>
    </div>
    </div>
    <span>Here is some dark text on a light background.</span>
    </div>








    share|improve this answer

























      up vote
      0
      down vote













      If you re-order the declarations, you'll see the text-color change. Later definitions override sooner ones.



      You can use the > operator to restrict your selection somewhat #main > span will match only spans immediately beneath #main, the + selector selects the next-sibling in the dom. & you've got nth-child in/around that type of selection. Is that what you meant by:




      Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?







      div, span {
      padding: 10px;
      display: block;
      }

      .light-background {
      background-color: #cacaca;
      }

      .dark-background {
      background-color: #333;
      }

      .light-background span {
      color: black;
      }

      .dark-background span {
      color: white;
      }

      <div class="light-background">
      <div class="dark-background">
      <span>Here is some light text on a dark background.</span>

      <div class="light-background">
      <span>Here is some dark text on a light background.</span>
      </div>
      </div>
      <span>Here is some dark text on a light background.</span>
      </div>








      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        If you re-order the declarations, you'll see the text-color change. Later definitions override sooner ones.



        You can use the > operator to restrict your selection somewhat #main > span will match only spans immediately beneath #main, the + selector selects the next-sibling in the dom. & you've got nth-child in/around that type of selection. Is that what you meant by:




        Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?







        div, span {
        padding: 10px;
        display: block;
        }

        .light-background {
        background-color: #cacaca;
        }

        .dark-background {
        background-color: #333;
        }

        .light-background span {
        color: black;
        }

        .dark-background span {
        color: white;
        }

        <div class="light-background">
        <div class="dark-background">
        <span>Here is some light text on a dark background.</span>

        <div class="light-background">
        <span>Here is some dark text on a light background.</span>
        </div>
        </div>
        <span>Here is some dark text on a light background.</span>
        </div>








        share|improve this answer












        If you re-order the declarations, you'll see the text-color change. Later definitions override sooner ones.



        You can use the > operator to restrict your selection somewhat #main > span will match only spans immediately beneath #main, the + selector selects the next-sibling in the dom. & you've got nth-child in/around that type of selection. Is that what you meant by:




        Is it possible to apply a rule if the elements matched by the selector are closer to each other than other rules which may match?







        div, span {
        padding: 10px;
        display: block;
        }

        .light-background {
        background-color: #cacaca;
        }

        .dark-background {
        background-color: #333;
        }

        .light-background span {
        color: black;
        }

        .dark-background span {
        color: white;
        }

        <div class="light-background">
        <div class="dark-background">
        <span>Here is some light text on a dark background.</span>

        <div class="light-background">
        <span>Here is some dark text on a light background.</span>
        </div>
        </div>
        <span>Here is some dark text on a light background.</span>
        </div>








        div, span {
        padding: 10px;
        display: block;
        }

        .light-background {
        background-color: #cacaca;
        }

        .dark-background {
        background-color: #333;
        }

        .light-background span {
        color: black;
        }

        .dark-background span {
        color: white;
        }

        <div class="light-background">
        <div class="dark-background">
        <span>Here is some light text on a dark background.</span>

        <div class="light-background">
        <span>Here is some dark text on a light background.</span>
        </div>
        </div>
        <span>Here is some dark text on a light background.</span>
        </div>





        div, span {
        padding: 10px;
        display: block;
        }

        .light-background {
        background-color: #cacaca;
        }

        .dark-background {
        background-color: #333;
        }

        .light-background span {
        color: black;
        }

        .dark-background span {
        color: white;
        }

        <div class="light-background">
        <div class="dark-background">
        <span>Here is some light text on a dark background.</span>

        <div class="light-background">
        <span>Here is some dark text on a light background.</span>
        </div>
        </div>
        <span>Here is some dark text on a light background.</span>
        </div>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 7:48









        admcfajn

        1,09311123




        1,09311123






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252000%2fdoes-css-ever-care-about-dom-closeness-relationships%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."