0

I have just migrated from SCSS to PostCSS and encountered only one issue I cannot resolve.

The issue concerns how to alter a the styles of a nested element when its parent has a specific class.

The following is a simplified representation of the problem. Real use cases are much more nested than this which is why the function had merit when using SASS.

Input

.nav {
  position: relative;
  height: 50px;

  & .nav-backdrop {
    height: 100%
    background-color: green;
    
    .nav:not(.is-open) & {
      background-color: red;
    }
  }
}

Expected result:

.nav {
  position: relative;
  height: 50px;
}
.nav .nav-backdrop {
  height: 100%;
  background-color: green;
}
.nav:not(.is-open) .nav-backdrop {
  background-color: red;
}

Actual result:

.nav {
  position: relative;
  height: 50px;
}
.nav .nav-backdrop {
  height: 100%;
  background-color: green;
}
.nav:not(.is-open) :is(.nav .nav-backdrop) {
  background-color: red;
}

The use of the pseudo :is() is not supposed to be the intense output to support the compatible browsers and I cannot see why it is outputting in this way.

In SASS I used a custom mixin and function called parent-append to have complete control over how nested I wanted to go. Ideally I would like to try and replicate the same but I cannot even get this basic representation to work.

I also tried to make use of postcss-nested-ancestors but that did not change anything.

The error I receive is can not be transformed to an equivalent selector without ':is()

Is there a way to achieve what I am trying to do.

Plugins included:

{
  "postcss-calc": "^10.1.1",
  "postcss-combine-media-query": "^2.0.0",
  "postcss-nested-ancestors": "^3.0.0",
  "postcss-preset-env": "^10.3.1",
  "postcss-simple-vars": "^7.0.1"
}
4
  • the actual result is the correct result: css-tip.com/nesting-selector Commented Sep 17 at 18:35
  • @TemaniAfif Is there anyway to get the result I'd actually like? Commented Sep 17 at 18:36
  • 1
    You have to rethink your selectors or keep using Sass. You cannot really change the behavior of CSS Commented Sep 17 at 18:39
  • @TemaniAfif I'm starting to realise that. I've made some workarounds to make life simpler as I will stick with PostCSS. Commented Sep 17 at 18:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.