The goal is to have an element's parent dictate it’s style. So .dark .light p would have light text, .dark p would have dark text, and .light .dark .light p would have light text.
The p element might not even be a direct child of .dark or .light so .light .dark div.wrap div.inner p would have dark text.
Because of the cascading nature of CSS, it seems to prefer the last rule. Is this even possible?
/* Doesn't work - switching the order isn't helpful
as the opposite problem occurs */
.dark p {
color: #000;
}
.light p {
color: #aaa;
}
/* Works, but in my use case I need to specify attributes on
specific element. */
/*
.dark {
color: #000;
}
.light {
color: #aaa;
}
*/
/* Works but not realistic if paragraph is nested deeper */
/*
.dark > p {
color: #000;
}
.light > p {
color: #aaa;
}
*/
/* Only works on the first two levels */
/*
.dark p {
color: #000;
}
.light p {
color: #aaa;
}
.dark .light p {
color: #aaa;
}
.light .dark p {
color: #000;
}
*/
<div class="light">
<p>Light text</p>
<div class="dark">
<p>Dark text</p>
<div class="light">
<p>Light text</p>
</div>
</div>
</div>
<div class="dark">
<p>Dark text</p>
<div class="light">
<p>Light text</p>
<div class="dark">
<p>Dark text</p>
</div>
</div>
</div>
http://codepen.io/acondiff/pen/KaaqxP
CSS follows the flow of the css document, therefore if .dark comes after .light in the CSS file, any element in your html with both classes will always display the .dark style.
.light {
background: #ccc;
}
.dark {
background: #999;
}
<p class="dark light">This will be dark as it comes after light in the css file.</p>
!important
You can override the .dark styles by putting !important on your .light styles.
.light {
background: #eee !important;
}
.dark {
background: #999;
}
<p class="light dark">This will be light as the light styles has !important</p>
Alternatively you can apply the light styles to all elements and just override the elements you need to:
p {
background: #eee;
}
.dark {
background: #999;
}
<p>this will be light</p>
<p class="dark">this will be dark</p>
Consider giving the same color to all text elements.
Then overriding the color for either light or dark.
body {
color: red;
}
.light > p {
color: blue;
}
<div class="light">
<p>Light text</p>
<div class="dark">
<p>Dark text</p>
<div class="light">
<p>Light text</p>
</div>
</div>
</div>
<div class="dark">
<p>Dark text</p>
<div class="light">
<p>Light text</p>
<div class="dark">
<p>Dark text</p>
</div>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With