Encountering Nested CSS bugs while implementing Tailwind in a Next.js/React project

Just diving into the world of Next.js/React. I recently went through the Tailwind CSS for Next.js tutorial and successfully integrated Tailwind into my project by following these steps:

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

Following the provided instructions, I made changes to the generated tailwind.config.js as shown below:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./pages/**/*.{js,jsx}",
    "./components/**/*.{js,jsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

As well as updating my globals.css file like this:

@tailwind base;
@tailwind components;
@tailwind utilities;

...additional CSS styles are included here (quite a few)

Upon launching the development server with npm run dev, I encountered errors related to nested CSS which led me to an helpful article on resolving CSS nesting issues with Tailwind.

To address this, I modified my postcss.config.js to the following:

module.exports = {
  plugins: {
    'postcss-import': {},
    'tailwindcss/nesting': {},
    tailwindcss: {},
    autoprefixer: {},
  }
}

However, I am now facing the following errors:

wait  - compiling...
wait  - compiling /404 (client and server)...
warn  - ./node_modules/next/dist/build/webpack/loaders/css-loader/src/index.js??ruleSet[1].rules[2].oneOf[8].use[1]!./node_modules/next/dist/build/webpack/loaders/postcss-loader/src/index.js??ruleSet[1].rules[2].oneOf[8].use[2]!./styles/globals.css
Warning

(42:5) Nested CSS was detected, but CSS nesting has not been configured correctly.
Please enable a CSS nesting plugin *before* Tailwind in your configuration.
See how here: https://tailwindcss.com/docs/using-with-preprocessors#nesting
... [repeating warning messages]

I'm puzzled by these errors, why are they occurring, and what steps can I take to resolve them?

Answer №1

Looks like you may have overlooked the final step, Step 4. Here is the command to run:

npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

Step 1:

npm install -D tailwindcss
npx tailwindcss init

Step 2:

Ensure all template file paths are added in your tailwind.config.js file.

module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

Step 3:

Add the necessary @tailwind directives for each of Tailwind’s layers in your main CSS file

@tailwind base;
@tailwind components;
@tailwind utilities;

Step 4:

Initialize the Tailwind CLI build process

npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

Answer №2

After cleaning up the nested CSS errors within my global.css document, Tailwind is now functioning flawlessly.

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

Displaying data-table with only the values that are considered true

Right now, I am utilizing the AgReact table to exhibit data fetched from my endpoints. The data-table is functioning properly, however, it seems to be unable to display false values received from the endpoints on the table. Below are the snippets of my cod ...

What is the process for implementing a loading state during the next auth signin on my application?

After submitting the form, the signin function is triggered and everything happens in the background without allowing me to set a state for it. To enable the signin functionality, I have imported the following: import { signIn } from "next-auth/react ...

Encountering an issue when trying to dynamically import React Material UI Icons

Hey there, I'm currently working on dynamically loading icons from MUI using the following code snippet: import React from "react"; import * as MuiIcons from "@mui/icons-material"; console.log("MuiIcons: ", MuiIcons); co ...

The AJAX call is failing to update the state data in my React component

I've recently delved into ReactJS and encountered an issue regarding setting state while using an AJAX request. The AJAX call successfully communicates with the server, receiving a 200 code response, indicating that the API request functions properly ...

How to Retrieve the href Property of a NextJs Link in a Child Component

I'm currently implementing an interactive button feature using NextJs. Currently, each button is wrapped in a Next link component like this: <Link href={href} key={key} passHref> <MenuItem disableRipple style={{ color: router.p ...

Tips for incorporating a visible marker beside the dropdown arrow

When developing my React application with React hooks forms, I have a select component where I need to include a clear indicator 'x' icon next to the dropdown icon. The current select component code is: <Form.Control size="sm" as=&q ...

Browsing through a collection of JSON objects with a user interface

I need a way to filter and display a list of Dogs based on a search query. I initially stored the data in an array within the state, but with an increasing number of entries, I've decided to switch to JSON format for better management. However, I&apo ...

Is React.lazy() compatible with Create React App?

I recently developed a React App using npx create-react-app my-app command, utilizing the React 17 version. However, I ran into an issue when trying to update a traditional component. import Component from './Component' to const Component = Rea ...

The absence of the essential "src" property in the image component is causing a roadblock. Ensure that you include the "src" in the props when using the `next/image` component. This hurdle has me completely st

The image you are trying to display is missing the required "src" property. Please make sure to pass the "src" in props to the next/image component. I'm really stuck this time as I have checked everywhere but couldn't fix it. import Link from &qu ...

Steps for incorporating a quantity option in ReactJS for PayPal transactions

I am having trouble adding the quantity of an order. The code I have doesn't seem to be working properly. It works fine without specifying the quantity, but then it defaults to 1. Additionally, I'm unsure how to add a second product as it's ...

At what point should the term "function" be included in a ReactJS component?

As a beginner in ReactJS, I have been working through some tutorials and noticed that some code examples use the keyword function while others do not. This got me wondering what the difference is and when I should use each one. Render Example with functi ...

Is it possible to dynamically modify the className of a specific tr in the antd table?

In my Ant Design table, I have a toggler button in each row. When the user clicks on it, I want to add or remove a specific class from that row's parent element. While there are ways to do this with vanilla JS, I am working with the React library and ...

Tips for integrating Stripe into a React test

I am currently conducting a test on a React component that utilizes Stripe, and I am unsure about the best way to structure the test. An error message has been popping up as follows: Error: In order to use react-stripe-elements, ensure that Stripe.js ( ...

Leveraging the Spread Operator in Redux mapDispatchToProps with SweetAlert2

When I add the swal action creators to the mapDispatchToProps function like this: function mapDispatchToProps(dispatch) { return { getAnimal: (_id) => dispatch(getAnimal(_id)), ...swal } } The issue aris ...

Encountered a discrepancy with npm dependencies during the update or installation process on a legacy project

I am encountering issues while attempting to run an older project. Every time I try to npm install or start the project, it throws various dependency errors, particularly related to the outdated npm version. My current node version is 16.x, whereas the pro ...

What steps do I need to take in order to transform this code into a MUI component complete with

Having some trouble converting my hero banner code to MUI in a React project. The styling is not coming out correctly. Here is the HTML code: <svg data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 120 ...

Is it possible to align divs so that they touch when they wrap to a new line, creating a grid-like appearance?

My React board component consists of an array of divs that I want to arrange in a grid-like map. The issue is, when the div wraps to a new line, there is significant space between each row. I aim to have the divs close together with no gaps. GameMap state ...

Is it possible to utilize target="blank" in an external link when working with MDX?

For my latest project, I am developing a blog using NextJS and MDX. One of the features I would like to implement is opening external links in a new tab by adding a target="_blank" attribute. In typical Markdown syntax, I usually achieve this by using [wo ...

What is the best way to extract parameters from MERN routing?

Recently, I've been facing difficulties while trying to retrieve data from MongoDB using dynamic routing. The URL structure is as follows: http://localhost:3000/paprogram/:_id` http://localhost:3000/paprogram/60bfbf12f8d33aef9ae4ebb9` I am attempting ...

Tips for minimizing unnecessary rerenders in child components that rely on cached information from the parent component

check out the sandbox here Application maintains state to compute a memoized value, which is then passed as props to the Options. When a change occurs in the state triggered by a callback function in Option, it causes a rerender of the main Application, r ...