Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Controlling Cascade

Tags:

html

css

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.

like image 456
Chris Seufert Avatar asked Aug 28 '15 06:08

Chris Seufert


People also ask

How do CSS rules Cascade?

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.

Why CSS is called cascading?

Cascading style sheets are called cascading because several different style sheets can be active for a single document at the same time.

What are the 3 CSS principles?

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.

How do I apply a CSS to a specific element?

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.


1 Answers

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>
like image 146
Maximillian Laumeister Avatar answered Sep 22 '22 03:09

Maximillian Laumeister