0

I want to select all tags with class p, that is child of .row

.row .p {
   ...
}

but exclude all, that have .disable as parent class (not directly).

I can select both of them like that:

.row .p {
  border: 3px solid blue;
}

.disable .row .p {
  border: 3px solid red;;
}

But we want to use just one selector

NOTE: between .disable and .row can be any elements.

CLEARIFICATION: .disable .row .p should have no color at all. So if .disable is present somehow in the parent line do NOT make a blue border. just leave it away.

example: https://codepen.io/miladfm/pen/ELbwMx

miladfm
  • 1,508
  • 12
  • 20

1 Answers1

0

Using CSS Custom Properties (aka CSS Variables) you can style an element according to some ancestor if you set a value to this ancestor and get its value on the former. It's inherited throughout the mighty cascade® and if not set, you can have a default value given by the fallback of var(--custom-prop, fallback)

Support is Edge 15+ or 16+ and other modern browsers for a longer time AFAIK

/* If any ascendant has a given class, no border */
.disable {
  --parent-disable-border: none;
}

/* Default border with fallback value of a CSS Variable / Custom Property */
.p {
  border: var(--parent-disable-border, 3px solid blue);
}

Codepen (sorry no snippet it currently fails in Firefox)

FelipeAls
  • 21,711
  • 8
  • 54
  • 74