html - How to write a CSS selector that works on nested divs and selects the closest match? -
imagine have dynamically rendered page use classes class1
, class2
on divs contain each other:
<div class="class1"> ... <div class="class2"> ... <div class="insider">
i have these selectors in css:
.class1 .insider { ... } .class2 .insider { ... }
now problem both selectors match above .insider
, 1 later in css source code win. how can make selector win has classn
parent closest .insider
.
below snippet reproduced problem, showed expectations inline styles. .other
, .another
div
s there show depth of .insider not known, direct child selector (>
) out of picture.
/* layout: irrelevant issue */ .outer { height: 100px; width: 100px; } .inner { display: inline-block; height: 50px; width: 50px; margin: 25px 25px; } .insider { text-align: center; } /* colors: visual part should fixed */ .class1 { background-color: black; } .class2 { background-color: lightgray; } .class1 .insider { color: red; } .class2 .insider { color: green; }
<table> <tr><th>title</th><th>actual</th><th>expected</th><th>explanation</th></tr> <tr> <td>inside class1-2</td> <td> <div class="outer class1"> <div class="inner class2"> <div class="other"> <p class="insider">asdf</p> </div> </div> </div> </td> <td> <div class="outer" style="background-color: black;"> <div class="inner" style="background-color: lightgray;"> <div class="other"> <p class="insider" style="color: green;">asdf</p> </div> </div> </div> </td> <td>the text color should green because closest styled parent "class2" , not because ".class2 .insider" declared after ".class1 .insider" in css code. swap red , green styles in css , text turns red.</td> </tr> <tr> <td>inside class2-1</td> <td> <div class="outer class2"> <div class="inner class1"> <div class="other"> <div class="another"> <p class="insider">asdf</p> </div> </div> </div> </div> </td> <td> <div class="outer" style="background-color: lightgray;"> <div class="inner" style="background-color: black;"> <div class="other"> <div class="another"> <p class="insider" style="color: red;">asdf</p> </div> </div> </div> </div> </td> <td>the text color should red because closest styled parent "class1".</td> </tr> <tr> <td>inside class2-1-2</td> <td> <div class="outer class2"> <div class="inner class1"> <div class="other class2"> <div class="another"> <p class="insider">asdf</p> </div> </div> </div> </div> </td> <td> <div class="outer" style="background-color: lightgray;"> <div class="inner" style="background-color: black;"> <div class="other" style="background-color: lightgray;"> <div class="another"> <p class="insider" style="color: green;">asdf</p> </div> </div> </div> </div> </td> <td>the text color should green because closest styled parent "class2", , not because outermost styled parent "class2", nor because of declaration order, see first case.</td> </tr> </table>
all of examples show in chrome dev tools inspector:
it's visible green color wins because has bigger line number.
.class1 .insider, .class2 .class1 .insider{ color: red; } .class2 .insider, .class1 .class2 .insider{ color: green; }
the downside of solution n increase number of selector have write increase recursively. can helped pre-processing library sass or less.
demo:
/* layout: irrelevant issue */ .outer { height: 100px; width: 100px; } .inner { display: inline-block; height: 50px; width: 50px; margin: 25px 25px; } .insider { text-align: center; } /* colors: visual part should fixed */ .class1 { background-color: black; } .class2 { background-color: lightgray; } .class1 .insider, .class2 .class1 .insider{ color: red; } .class2 .insider, .class1 .class2 .insider{ color: green; }
<table> <tr><th>title</th><th>actual</th><th>expected</th><th>explanation</th></tr> <tr> <td>inside class1-2</td> <td> <div class="outer class1"> <div class="inner class2"> <div class="other"> <p class="insider">asdf</p> </div> </div> </div> </td> <td> <div class="outer" style="background-color: black;"> <div class="inner" style="background-color: lightgray;"> <div class="other"> <p class="insider" style="color: green;">asdf</p> </div> </div> </div> </td> <td>the text color should green because closest styled parent "class2" , not because ".class2 .insider" declared after ".class1 .insider" in css code. swap red , green styles in css , text turns red.</td> </tr> <tr> <td>inside class2-1</td> <td> <div class="outer class2"> <div class="inner class1"> <div class="other"> <div class="another"> <p class="insider">asdf</p> </div> </div> </div> </div> </td> <td> <div class="outer" style="background-color: lightgray;"> <div class="inner" style="background-color: black;"> <div class="other"> <div class="another"> <p class="insider" style="color: red;">asdf</p> </div> </div> </div> </div> </td> <td>the text color should red because closest styled parent "class1".</td> </tr> <tr> <td>inside class2-1-2</td> <td> <div class="outer class2"> <div class="inner class1"> <div class="other class2"> <div class="another"> <p class="insider">asdf</p> </div> </div> </div> </div> </td> <td> <div class="outer" style="background-color: lightgray;"> <div class="inner" style="background-color: black;"> <div class="other" style="background-color: lightgray;"> <div class="another"> <p class="insider" style="color: green;">asdf</p> </div> </div> </div> </div> </td> <td>the text color should green because closest styled parent "class2", , not because outermost styled parent "class2", nor because of declaration order, see first case.</td> </tr> </table>
edit n-independent version in need add selectors if nested html layout more complex:
.unique_class_name1 .insider, [class~=unique_class_name] .unique_class_name1 .insider{ color: red; } .unique_class_name2 .insider, [class~=unique_class_name] .unique_class_name2 .insider{ color: green; }
in version though, important make sure pattern unique_class_name
unique, since class~=unique_class_name
hit elements class attribute contains key.
/* layout: irrelevant issue */ .outer { height: 100px; width: 100px; } .inner { display: inline-block; height: 50px; width: 50px; margin: 25px 25px; } .insider { text-align: center; } /* colors: visual part should fixed */ .unique_class_name1 { background-color: black; } .unique_class_name2 { background-color: lightgray; } .unique_class_name1 .insider, [class~=unique_class_name] .unique_class_name1 .insider{ color: red; } .unique_class_name2 .insider, [class~=unique_class_name] .unique_class_name2 .insider{ color: green; }
<table> <tr><th>title</th><th>actual</th><th>expected</th><th>explanation</th></tr> <tr> <td>inside class1-2</td> <td> <div class="outer unique_class_name1"> <div class="inner unique_class_name2"> <div class="other"> <p class="insider">asdf</p> </div> </div> </div> </td> <td> <div class="outer" style="background-color: black;"> <div class="inner" style="background-color: lightgray;"> <div class="other"> <p class="insider" style="color: green;">asdf</p> </div> </div> </div> </td> <td>the text color should green because closest styled parent "class2" , not because ".class2 .insider" declared after ".class1 .insider" in css code. swap red , green styles in css , text turns red.</td> </tr> <tr> <td>inside class2-1</td> <td> <div class="outer unique_class_name2"> <div class="inner unique_class_name1"> <div class="other"> <div class="another"> <p class="insider">asdf</p> </div> </div> </div> </div> </td> <td> <div class="outer" style="background-color: lightgray;"> <div class="inner" style="background-color: black;"> <div class="other"> <div class="another"> <p class="insider" style="color: red;">asdf</p> </div> </div> </div> </div> </td> <td>the text color should red because closest styled parent "class1".</td> </tr> <tr> <td>inside class2-1-2</td> <td> <div class="outer unique_class_name2"> <div class="inner unique_class_name1"> <div class="other unique_class_name2"> <div class="another"> <p class="insider">asdf</p> </div> </div> </div> </div> </td> <td> <div class="outer" style="background-color: lightgray;"> <div class="inner" style="background-color: black;"> <div class="other" style="background-color: lightgray;"> <div class="another"> <p class="insider" style="color: green;">asdf</p> </div> </div> </div> </div> </td> <td>the text color should green because closest styled parent "class2", , not because outermost styled parent "class2", nor because of declaration order, see first case.</td> </tr> </table>
Comments
Post a Comment