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

What is the process for installing and utilizing the custom CSSLint rules that I have developed for the command line interface?

After investing a significant amount of time into following the instructions outlined here and here for creating custom CSS linting rules using CSSLint via the CLI, I have successfully generated and tested my own set of rules. However, I am now facing the ...

Ensuring a full height div using CSS

I have encountered an issue and created a fiddle to help illustrate it: http://jsfiddle.net/XJpGT/ The challenge I am facing is ensuring that the height of the green box always remains at 100%, while also making sure that the green and orange boxes do not ...

Creating a D3 graph within a static HTML document without the need for additional wrappers or packages installation

I am currently working on a next.js (react) application and I am looking to incorporate multiple graphs that have been created as static files (HTML, CSS, JS). Can someone guide me on how to achieve this? One of the example graphs I am referring to can be ...

Problematic: BS4 Cards cannot be accommodated within the parent div.Improved: The

Within a dynamic-height parent div, I have two cards stacked on top of each other. However, the issue arises when these two cards do not completely fit inside the parent div. The problem likely stems from the table within the lower card, which is not respo ...

What is the best way to center the 'email promo' text in the top navigation bar alongside the image and other text links?

Struggling with learning CSS and HTML, particularly when it comes to positioning elements on the page. I'm having trouble vertically aligning the elements inside the top nav bar and can't seem to figure out what I'm doing wrong. Any insights ...

The antithesis of a feature that encapsulates a chosen area with a span

Hi everyone, I have a function that works as follows: define(function () { 'use strict'; var sel, range, span; return function () { span = document.createElement("span"); span.className = 'highlight'; if (window.ge ...

I require a picture to fill up as much space as possible, regardless of its initial dimensions

As I work on my react-native app, I encounter a challenge regarding the layout. My application consists of a top bar and a bottom bar with an image in between. The image needs to fill up as much space as possible without overlapping the bars. Despite numer ...

Resize the div blocks by scaling the button placed beneath them

I decided to modify the g-recaptcha widget to fit my specific requirements, and it is working flawlessly. However, I noticed that the ng-click button directly beneath it is no longer responsive. The reason behind this issue eludes me. Here is a snippet of ...

What is the reason behind the body element's background styling impacting the entire screen?

Why does styling the background of the body element affect the entire screen instead of just the body itself? For example, consider this CSS rule: body { width: 700px; height:200px; border: 5px dotted red; background-color: blue; } While the ...

Improving the method of retrieving RTK result within getServerSideProps

Currently, I am utilizing RTK Query to make an API call within my getServerSideProps function. While I can successfully retrieve the result using the code snippet provided below, I find the process somewhat awkward. Additionally, the result lacks proper ty ...

The issue with Bootstrap Vue is that it fails to render the CSS properly when utilizing cards

Recently, I delved into the world of Bootstrap Vue and wrote the code snippet below: <template> <div> <div> <b-card-group> <b-card border-variant="dark" header="Dark" align="center"> &l ...

Customizing the appearance of the 'Submit' button compared to the <a href=""></a> button using CSS

I am experiencing some issues. Even though the CSS code for these two buttons is exactly the same, their appearance is different. I am unable to make the :hover or :active effects work either. My goal is to have the left 'input type="submit' but ...

How can the parent div's height be determined by the child div when using position: absolute?

I am struggling with setting the height of a parent div that contains a nested child div with absolute positioning. The child div has a fixed height of 652px, but I cannot seem to get the parent div to match this height. I have tried adjusting the clear an ...

Apollo Client's useQuery does not trigger a re-render when new data arrives

I am currently developing a Next.js application. This app pulls data from the server using graphql. The server being used is a postgraphile server. To generate the Apollo-client hooks, I am utilizing graphql-codegen. However, I have come across an issu ...

Embrace the words enveloped within large quotation marks

I am seeking a way to format paragraphs with large quotation marks surrounding them. I have attempted several methods, but my line-height seems to be affected, as shown in the following image: Can anyone suggest a proper method for achieving this? (Addit ...

Previewing a PDF file with multiple headers displayed above the text content

Creating multiple headers for each page of a PDF document using dompdf can be tricky. The headers you generated are fixed at the top with a width of 100%. However, you are facing an issue where on the second and subsequent pages, the header overlaps with t ...

The Next.js development was hindered by a webpack issue causing the build to

Whenever I try to build my next.js app using npm run build, npm fails and crashes while trying to access the logo.png file from the public directory. This directory is where all static assets like images and icons are stored, so I'm unsure why this er ...

Displaying Hierarchical Data with AngularJS and ng-repeat

Recently, I've been working on finding an elegant solution to represent hierarchical data in my application. Specifically, I have implemented a slide effect for bootstrap badges that showcase sub-categories using AngularJS. With some guidance from th ...

Unlocking the secrets: accessing environment variables from a Docker image within a NextJS application

After setting an environment variable in a Dockerfile, I expected to access this value within my application running inside the docker image (a nextJS application). The docker image creation process looks like this: FROM node:20.0.0-alpine3.17 RUN apk add ...

What is the best way to nest a div within a flexbox container?

I am attempting to create a div that is based on percentages, and I want to nest a div inside another. Specifically, I want the center (xyz div) to only take up 90% of the height of the content-div. My goal is for the "content" div to be responsive to 90% ...