Having trouble getting PostCSS nesting to work with CSS variables in Tailwind CSS and Next.js?

I've been attempting to utilize PostCSS nesting alongside CSS variables, but unfortunately the CSS variables are not being converted at all.

Instead, I am seeing Invalid property value in the DOM for CSS Variables.

The tailwind.css file I have includes several CSS variables:

tailwind.css

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

@layer base {
  :root {
    --font-mono: 'Fira Mono, monospace';

    --text-1: '12px';
    --text-2: '14px';

    --colors-black: 'rgba(19, 19, 21, 1)';
    --colors-white: 'rgba(255, 255, 255, 1)';
    --colors-gray: 'rgba(128, 128, 128, 1)';
    --colors-blue: 'rgba(3, 136, 252, 1)';
    --colors-red: 'rgba(249, 16, 74, 1)';
    --colors-yellow: 'rgba(255, 221, 0, 1)';
    --colors-pink: 'rgba(232, 141, 163, 1)';
    --colors-turq: 'rgba(0, 245, 196, 1)';
    --colors-orange: 'rgba(255, 135, 31, 1)';

    --space-1: '4px';
    --space-2: '8px';
    --space-3: '16px';

    --radii-1: '2px';
    --radii-2: '4px';
  }

  pre {
    --background: 'hsla(206 12% 89.5% / 5%)';
    --text: var('--colors-white');
    --syntax1: var('--colors-orange');
    --syntax2: var('--colors-turq');
    --syntax3: var('--colors-pink');
    --syntax4: var('--colors-pink');
    --comment: var('--colors-gray');
    --removed: var('--colors-red');
    --added: var('--colors-turq');

    box-sizing: 'border-box';
    padding: var('--space-3');
    overflow: 'auto';
    font-family: var('--font-mono');
    font-size: var('--text-2');
    line-height: var('--space-3');
    whitespace: 'pre';
    background-color: var('--background');
    color: var('--text');

    '& > code': {
      display: 'block';
    }

    '.token.parameter': {
      color: var('--text');
    }

    '.token.tag, .token.class-name, .token.selector, .token.selector .class, .token.function': {
      color: var('--syntax1');
    }

    '.token.attr-value, .token.class, .token.string, .token.number, .token.unit, .token.color': {
      color: var('--syntax2');
    }

    '.token.attr-name, .token.keyword, .token.rule, .token.operator, .token.pseudo-class, .token.important': {
      color: var('--syntax3');
    }

    '.token.punctuation, .token.module, .token.property': {
      color: var('--syntax4');
    }

    '.token.comment': {
      color: var('--comment');
    }

    '.token.atapply .token:not(.rule):not(.important)': {
      color: 'inherit';
    }

    '.language-shell .token:not(.comment)': {
      color: 'inherit';
    }

    '.language-css .token.function': {
      color: 'inherit';
    }

    '.token.deleted:not(.prefix), .token.inserted:not(.prefix)': {
      display: 'block';
      px: '$4';
      mx: '-$4';
    }

    '.token.deleted:not(.prefix)': {
      color: var('--removed');
    }

    '.token.inserted:not(.prefix)': {
      color: var('--added');
    }

    '.token.deleted.prefix, .token.inserted.prefix': {
      userselect: 'none';
    }
  }
}

My PostCSS Config already includes postcss-preset-env, which is supposed to support CSS nesting. Additionally, I have installed postcss-nested and postcss-css-variables.

postcss.config.js

module.exports = {
  plugins: [
    'postcss-import',
    'tailwindcss',
    'postcss-flexbugs-fixes',
    'postcss-nested',
    'postcss-css-variables',
    [
      'postcss-preset-env',
      {
        autoprefixer: {
          flexbox: 'no-2009',
        },
        stage: 3,
        features: {
          'custom-properties': false,
          'nesting-rules': true,
        },
      },
    ],
  ],
}

You can find the reproduction of this issue here → https://github.com/deadcoder0904/postcss-tailwind-next-bug

To see the CSS variables in action, run the app and check the DOM to view the Styles panel which currently displays Invalid property value.

Any ideas on how to make CSS variables work seamlessly with Tailwind CSS?

Answer â„–1

After encountering an issue, I realized the solution was to eliminate quotes and object notation in CSS. Initially, I had copied code from CSS-in-JS without making these necessary adjustments.

tailwind.css

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

@layer base {
  :root {
    --font-mono: 'Fira Mono, monospace';

    --text-1: 12px;
    --text-2: 14px;

    --colors-black: rgba(19, 19, 21, 1);
    --colors-white: rgba(255, 255, 255, 1);
    --colors-gray: rgba(128, 128, 128, 1);
    --colors-blue: rgba(3, 136, 252, 1);
    --colors-red: rgba(249, 16, 74, 1);
    --colors-yellow: rgba(255, 221, 0, 1);
    --colors-pink: rgba(232, 141, 163, 1);
    --colors-turq: rgba(0, 245, 196, 1);
    --colors-orange: rgba(255, 135, 31, 1);

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 16px;

    --radii-1: 2px;
    --radii-2: 4px;
  }

  pre {
    --background: hsla(206 12% 89.5% / 5%);
    --text: var(--colors-white);
    --syntax1: var(--colors-orange);
    --syntax2: var(--colors-turq);
    --syntax3: var(--colors-pink);
    --syntax4: var(--colors-pink);
    --comment: var(--colors-gray);
    --removed: var(--colors-red);
    --added: var(--colors-turq);

    box-sizing: border-box;
    padding: var(--space-3);
    overflow: auto;
    font-family: var(--font-mono);
    font-size: var(--text-2);
    line-height: var(--space-3);
    white-space: pre;
    background-color: var(--background);
    color: var(--text);

    & > code {
      display: block;
    }

    .token.parameter {
      color: var(--text);
    }

    .token.tag,
    .token.class-name,
    .token.selector,
    .token.selector .class,
    .token.function {
      color: var(--syntax1);
    }

    .token.attr-value,
    .token.class,
    .token.string,
    .token.number,
    .token.unit,
    .token.color {
      color: var(--syntax2);
    }

    .token.attr-name,
    .token.keyword,
    .token.rule,
    .token.operator,
    .token.pseudo-class,
    .token.important {
      color: var(--syntax3);
    }

    .token.punctuation,
    .token.module,
    .token.property {
      color: var(--syntax4);
    }

    .token.comment {
      color: var(--comment);
    }

    .token.atapply .token:not(.rule):not(.important) {
      color: inherit;
    }

    .language-shell .token:not(.comment) {
      color: inherit;
    }

    .language-css .token.function {
      color: inherit;
    }

    .token.deleted:not(.prefix),
    .token.inserted:not(.prefix) {
      display: block;
      px: $4;
      mx: -$4;
    }

    .token.deleted:not(.prefix) {
      color: var(--removed);
    }

    .token.inserted:not(.prefix) {
      color: var(--added);
    }

    .token.deleted.prefix,
    .token.inserted.prefix {
      userselect: none;
    }
  }
}

In a separate project, postcss-preset-env failed to function as expected, leading me to resort to using postcss-nested and postcss-css-variables in postcss.config.js.

postcss.config.js

module.exports = {
    plugins: [
        'postcss-import',
        'tailwindcss',
        'postcss-flexbugs-fixes',
        'postcss-nested',
        'postcss-custom-properties',
        'autoprefixer'
    ],
}

Interestingly, despite sharing the same configuration, postcss-preset-env worked smoothly in another project. After confirming this by attempting various troubleshooting methods, it became evident that the issue lay outside of my project's scope.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

HTML - Retain placeholder text while user inputs

My input is structured like this: <input value="My text" placeholder="Placeholder"> Typing in the input causes the placeholder text to disappear, which is expected. However, I am looking to keep the placeholder text visible as a background behind ...

Adding a class to the following DIV and smoothly transitioning the current one out can be achieved using a dynamic number of items in multiple instances

Is there a way to create a scalable CSS slideshow for text divs without using images? Here is the current HTML structure: <div class="mb-panel-container cf"> <div class="mb-panel-section mb-slider"> <div class="mb-panel active" ...

How about trying out some dropdowns?

Following the issue I mentioned in my previous question titled Margin-Right on CSS not working, I am currently engaged in a creative endeavor to redesign my school's website homepage. The progress so far has been significant, but I now require some as ...

The wonders of CSS flexbox and the power of justify-content

I am working on a layout where I need three flex items (rows) in a flex container, and my requirement is to justify them as space-between... The first row will consist of cloud tags, the second will have a price, and the third will contain a Read more link ...

Is it acceptable to use JavaScript files in the pages directory in NEXTJS13, or is it strongly advised to only use TypeScript files in the most recent version?

In the previous iterations of nextJS, there were JavaScript files in the app directory; however, in the most recent version, TypeScript files have taken their place. Is it still possible to begin development using JavaScript? I am working on creating an a ...

Encountering 404 errors on dynamic routes following deployment in Next.JS

In my implementation of a Next JS app, I am fetching data from Sanity to generate dynamic routes as shown below: export const getStaticPaths = async () => { const res = await client.fetch(`*[_type in ["work"] ]`); const data = await re ...

What is the best approach to execute the jquery script exclusively on mobile devices?

I want to modify this code so that it only functions on mobile devices and is disabled on computers. How can I achieve this? <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <ul id="pri ...

Creating Awesome Icons in Kendo Grid with Code In this tutorial, we will learn how to programm

Looking to have a Kendo grid display a green fas-fa-clock icon if isActive is true, and a grey far-fa-clock icon if false. Clicking on the icon should toggle between true and false. Currently, the grid just shows the word true or false in the column. Cod ...

Show a div pulsating while simultaneously animating another div

After coming across this query on Stack Overflow about adding a class without jQuery if hovering over another class I encountered an issue with a div element that displays upon hovering over a span, which was functioning correctly. However, I also have a ...

Adjusting Text Color on Buttons

I am looking for a simple button with clear color and black text, but the default button has white text. I need a way to change the text color. Check out my current app <View style={styles.fastlog}> <Button style={styles.bouton} title= "Con ...

Adjusting div to not be absolutely positioned

Trying to create a div with animated borders, but it only works when positioned absolutely in the center. For illustration, here is the HTML and CSS code: .bb, .bb::before, .bb::after { position: absolute; top: 0; bottom: 0; left: 0; right: 0; ...

Getting 100% height on floated neighboring divs: Tips and tricks

​<div id='container'> <div class='left'></div> <div class='right'></div> <div class='clear'></div> </div>​​​​​​​​​​​​​​​​â ...

Is there a way to make my red div switch its background color from red to green when I press the swap button?

How can I make the red div change its background color to toggle between red and green when I click on the swap button in the following code? $(document).ready(onReady); var numberOfClicks = 0; function onReady() { console.log('inside on ready ...

Getting tags with text containing specific substring by utilizing CSS selectors

Here is the HTML snippet I am working with: <tr bgcolor="#DEDEDE"> <td> <b>OK:Have idea</b> </td> <td> <b>KO:Write code</b> </td> <td> <b>KO:Ru ...

Tips for getting flex-end to function properly in IE11

My attempt to implement justify-content: flex-end; for a DIV with hidden overflow content in IE11 was unsuccessful. Despite trying various approaches, I managed to create a basic code snippet that functions properly in Chrome but fails in IE11: .token- ...

What is the best way to make a div expand to the full width of the screen while still being contained within its parent div?

I am facing an issue with the black bar at the bottom of the slider not stretching full-width on the screen. While it works fine on the left, the right side is cut off at the container edge. I am using Master Slider if that's relevant information. Can ...

Develop an XML document that includes CSS and DTD seamlessly incorporated

Could someone please provide me with a short code example of an XML file that includes both a DTD and CSS styles all in one file? Just need one element as an example. I'm new to XML and can't seem to find any examples with both XML and CSS comb ...

Is it possible to customize the color of the placeholder and clear-icon in the ion-search bar without affecting

I am working with two ion-search bars and I need to customize the placeholder and clear icon color for just one of them. <ion-searchbar class="search-bar" placeholder="search"></ion-searchbar> My goal is to target a specific i ...

Tips for keeping your button fixed in place

I am encountering an issue where my button moves below the select dropdown list when I try to make a selection. Is there a way to keep the button in place even when the dropdown list from the select box is visible? For reference, here is the current outp ...

Putting Nextjs <Link /> to the Test with Jest + Testing-Library

While testing the behavior of <Link />, I encountered a TypeError (Cannot read property 'push' of null) when expecting it to redirect to a specific route. This is the component under test: import React from "react" import Link fr ...