CSS/SCSS: How to select a descendent of div.box-type-1, but only when div.box-type-1 is the closest ancestor with a .box-type-* class?

I’ve got containers with colored backgrounds that fall into one of three different categories, represented by the sass placeholders %dark-box, %light-box, and %gradient-box. I need to adjust the styling of elements that are descendants of these boxes. These elements can be nested at any arbitrary depth within the box.

The problem is that the boxes can be nested inside each other, also at any arbitrary depth, and there’s no limit to how many levels deep the nesting can go. Boxes, inside boxes, inside boxes, etc. So the selectors targeting elements inside the boxes should only target elements when that box is the nearest ?-box ancestor.

Consider this HTML:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><div class=".dark-box"> <!-- backgrund-color:$dark-box-bg-color -->
<div>
<div>
<div>
<p>This should have color:$dark-box-text-color</p>
</div>
</div>
</div>
<div>
<div class=".light-box"> <!-- backgrund-color:$light-box-bg-color -->
<div>
<div>
<div>
<p>This should have color:$light-box-text-color</p>
</div>
<div>
<p>This should have color:$light-box-text-color</p>
</div>
<div>
<div class=".dark-box"> <!-- backgrund-color:$dark-box-bg-color -->
<div>
<div class=".light-box"> <!-- backgrund-color:$light-box-bg-color -->
<div>
<div class=".dark-box"> <!-- backgrund-color:$dark-box-bg-color -->
<div>
<div>
<p>This should have color:$dark-box-text-color</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</code>
<code><div class=".dark-box"> <!-- backgrund-color:$dark-box-bg-color --> <div> <div> <div> <p>This should have color:$dark-box-text-color</p> </div> </div> </div> <div> <div class=".light-box"> <!-- backgrund-color:$light-box-bg-color --> <div> <div> <div> <p>This should have color:$light-box-text-color</p> </div> <div> <p>This should have color:$light-box-text-color</p> </div> <div> <div class=".dark-box"> <!-- backgrund-color:$dark-box-bg-color --> <div> <div class=".light-box"> <!-- backgrund-color:$light-box-bg-color --> <div> <div class=".dark-box"> <!-- backgrund-color:$dark-box-bg-color --> <div> <div> <p>This should have color:$dark-box-text-color</p> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </code>
<div class=".dark-box"> <!-- backgrund-color:$dark-box-bg-color -->
  <div>
    <div>
      <div>
        <p>This should have color:$dark-box-text-color</p>
      </div>
    </div>
  </div>
  <div>
    <div class=".light-box"> <!-- backgrund-color:$light-box-bg-color -->
      <div>
        <div>
          <div>
            <p>This should have color:$light-box-text-color</p>
          </div>
          <div>
            <p>This should have color:$light-box-text-color</p>
          </div>
          <div>
            <div class=".dark-box"> <!-- backgrund-color:$dark-box-bg-color -->
              <div>
                <div class=".light-box"> <!-- backgrund-color:$light-box-bg-color -->
                  <div>
                    <div class=".dark-box"> <!-- backgrund-color:$dark-box-bg-color -->
                      <div>
                        <div>
                          <p>This should have color:$dark-box-text-color</p>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

With this scss:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>%dark-box {
background-color: $dark-box-bg-color;
// other stuff ...
}
%light-box {
background-color: $light-box-bg-color;
// other stuff ...
}
%gradient-box {
background-color: $gradient-box-bg-color;
// other stuff ...
}
.dark-box {@extend %dark-box;}
.light-box {@extend %light-box;}
.gradient-box {@extend %gradient-box;}
%dark-box-text {
color: $dark-box-text-color;
// other stuff ...
}
%light-box-text {
color: $light-box-text-color;
// other stuff ...
}
%gradient-box-text {
color: $gradient-box-text-color;
// other stuff ...
}
:is(%dark-box) > p,
:is(%dark-box) > /*! n descendants, none of which have a %light-box or %gradient-box class */ > p {
@extend %dark-box-text;
}
:is(%light-box) > p,
:is(%light-box) > /*! n descendants, none of which have a %dark-box or %gradient-box class */ > p {
@extend %light-box-text;
}
:is(%gradient-box) > p,
:is(%gradient-box) > /*! n descendants, none of which have a %dark-box or %light-box class */ > p {
@extend %gradient-box-text;
}
</code>
<code>%dark-box { background-color: $dark-box-bg-color; // other stuff ... } %light-box { background-color: $light-box-bg-color; // other stuff ... } %gradient-box { background-color: $gradient-box-bg-color; // other stuff ... } .dark-box {@extend %dark-box;} .light-box {@extend %light-box;} .gradient-box {@extend %gradient-box;} %dark-box-text { color: $dark-box-text-color; // other stuff ... } %light-box-text { color: $light-box-text-color; // other stuff ... } %gradient-box-text { color: $gradient-box-text-color; // other stuff ... } :is(%dark-box) > p, :is(%dark-box) > /*! n descendants, none of which have a %light-box or %gradient-box class */ > p { @extend %dark-box-text; } :is(%light-box) > p, :is(%light-box) > /*! n descendants, none of which have a %dark-box or %gradient-box class */ > p { @extend %light-box-text; } :is(%gradient-box) > p, :is(%gradient-box) > /*! n descendants, none of which have a %dark-box or %light-box class */ > p { @extend %gradient-box-text; } </code>
%dark-box {
  background-color: $dark-box-bg-color;
  // other stuff ...
}

%light-box {
  background-color: $light-box-bg-color;
  // other stuff ...
}

%gradient-box {
  background-color: $gradient-box-bg-color;
  // other stuff ...
}

.dark-box {@extend %dark-box;}
.light-box {@extend %light-box;}
.gradient-box {@extend %gradient-box;}

%dark-box-text {
  color: $dark-box-text-color;
  // other stuff ...
}

%light-box-text {
  color: $light-box-text-color;
  // other stuff ...
}

%gradient-box-text {
  color: $gradient-box-text-color;
  // other stuff ...
}

:is(%dark-box) > p,
:is(%dark-box) > /*! n descendants, none of which have a %light-box or %gradient-box class */ > p {
  @extend %dark-box-text;
}

:is(%light-box) > p,
:is(%light-box) > /*! n descendants, none of which have a %dark-box or %gradient-box class */ > p {
  @extend %light-box-text;
}

:is(%gradient-box) > p,
:is(%gradient-box) > /*! n descendants, none of which have a %dark-box or %light-box class */ > p {
  @extend %gradient-box-text;
}

How can I craft a selector that targets the first and last paragraph, but neither of the paragraphs in the middle? And vice versa?

I know I could do something that explicitly targets every possible depth that a paragraph could be at (like shown below), but then I would have to enforce a maximum depth limit. It would also mean an unacceptable increase in sass preprocessing time, when you consider that the box placeholders could be extended by a number of different selectors, and paragraphs are just one of many elements that I’ll need to target in this way.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>:is(%dark-box) > p,
:is(%dark-box) > :not(:where(%light-box), :where(%gradient-box)) > p,
:is(%dark-box) > :not(:where(%light-box), :where(%gradient-box)) > :not(:where(%light-box), :where(%gradient-box)) > p,
:is(%dark-box) > :not(:where(%light-box), :where(%gradient-box)) > :not(:where(%light-box), :where(%gradient-box)) > :not(:where(%light-box), :where(%gradient-box)) > p,
// etc...
{
@extend %dark-box-text;
}
</code>
<code>:is(%dark-box) > p, :is(%dark-box) > :not(:where(%light-box), :where(%gradient-box)) > p, :is(%dark-box) > :not(:where(%light-box), :where(%gradient-box)) > :not(:where(%light-box), :where(%gradient-box)) > p, :is(%dark-box) > :not(:where(%light-box), :where(%gradient-box)) > :not(:where(%light-box), :where(%gradient-box)) > :not(:where(%light-box), :where(%gradient-box)) > p, // etc... { @extend %dark-box-text; } </code>
:is(%dark-box) > p,
:is(%dark-box) > :not(:where(%light-box), :where(%gradient-box)) > p,
:is(%dark-box) > :not(:where(%light-box), :where(%gradient-box)) > :not(:where(%light-box), :where(%gradient-box)) > p,
:is(%dark-box) > :not(:where(%light-box), :where(%gradient-box)) > :not(:where(%light-box), :where(%gradient-box)) > :not(:where(%light-box), :where(%gradient-box)) > p,
// etc...
{
  @extend %dark-box-text;
}

I’ve tried some variations of the following, but haven’t found anything that works yet.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>:is(%dark-box) > p,
:is(%dark-box) :not(:where(:where(%light-box), :where(%gradient-box))) p {
@extend %dark-box-text;
}
</code>
<code>:is(%dark-box) > p, :is(%dark-box) :not(:where(:where(%light-box), :where(%gradient-box))) p { @extend %dark-box-text; } </code>
:is(%dark-box) > p,
:is(%dark-box) :not(:where(:where(%light-box), :where(%gradient-box))) p {
  @extend %dark-box-text;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật