I have built an editor for users to use, it has a pallette of styles to choose from, for this example we'll call these .p1
and `.p2'. The problem i have is that they are overriding each other depending on what order they appear in the style sheet.
I cant find a way that is going to solve this, because i do not know how these panels may be nested.
This example is significantly simplified, and I cannot control exactly where things like the a tag will be, they could be nested in tables, lists, paragraphs, or any other tags, at any depth. This would also apply to headings and any other child elements that need there style customized (eg H1, ul...) to look good in the context that it is shown in.
.p1 a {
color: red;
}
.p2 a {
color: green;
}
<div class="pl p2">
<a href="#">Hello 2</a>
<div class="pl p1">
<a href="#">Hello 2-1</a>
<div class="pl p2">
<a href="#">Hello 2-1-2</a>
</div>
</div>
</div>
<div class="pl p1">
<a href="#">Hello 1</a>
<div class="pl p2">
<a href="#">Hello 1-2</a>
<div class="pl p1">
<a href="#">Hello 1-2-1</a>
</div>
</div>
</div>
I have played around with :not selector, and also using all:default, on children, but have had no luck.
In CSS, styles sheets cascade by order of importance. If rules in different style sheets conflict with one another, the rule from the most important style sheet wins.
Cascading style sheets are called cascading because several different style sheets can be active for a single document at the same time.
The three principles I use to build and maintain my CSS are consistency, succinctness, and separation (or CSS). These principles are philosophically agnostic, meaning they can be practiced regardless of the methodology and/or framework you choose to adopt.
The CSS id Selector The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
It looks like what you may be looking for is the direct child selector >
. Learn more about the direct child selector on MDN.
Live Demo:
.p1 > a {
color: red;
}
.p2 > a {
color: green;
}
<div class="pl p2">
<a href="#">Hello 2</a>
<div class="pl p1">
<a href="#">Hello 2-1</a>
<div class="pl p2">
<a href="#">Hello 2-1-2</a>
</div>
</div>
</div>
<div class="pl p1">
<a href="#">Hello 1</a>
<div class="pl p2">
<a href="#">Hello 1-2</a>
<div class="pl p1">
<a href="#">Hello 1-2-1</a>
</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