Tailwind CSS styling appears to be malfunctioning in the production build of Next.js, despite functioning correctly in the

Despite following the proper build process with npm run build + npm run start, my Tailwind styling is not functioning correctly in the production build.

Oddly enough, when I use npm run dev, the styling works as expected and my page displays with a purple button:

https://i.sstatic.net/dPneN.png

However, after running npm run start following the build, my page appears differently:

https://i.sstatic.net/tR3JJ.png

Provided below is the structure of my folder:

https://i.sstatic.net/bvckW.png

The content of my tailwind.config.js file is as follows:

/** @type {import('tailwindcss').Config} */
/* eslint-env node */
module.exports = {
    content: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
    theme: {
        extend: {},
        fontFamily: {
            sans: ['Roboto', 'sans-serif'],
        },
    },
    plugins: [],
}

Here is what my postcss.config.js looks like:

/* eslint-env node */
module.exports = {
    plugins: {
        tailwindcss: {},
        autoprefixer: {},
    },
}

Within my global.css file, I have included the necessary Tailwind directives:

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

Even though I have added tailwindcss as both a dependency and a devDepenency, the issue persists. Any insights on how to resolve this for production builds would be greatly appreciated.

Answer №1

I encountered a similar issue as well, and it seems that in my experience tailwindcss is not given the highest priority; this may be affected if you are using other libraries such as material ui. In my scenario, react/emotion takes precedence over tailwind.

To resolve this, you will need to adjust the tailwind.config.js file and include the important field

module.exports = {
  // ...
  important: '#app',
}

Refer to the tailwind documentation - https://tailwindcss.com/docs/configuration#selector-strategy

Afterwards, in your nextjs application, locate the __document.jsx file and add the id attribute to the body element

<Html lang="en">
    <Head />
    <body id="app">
        <Main />
        <NextScript />
    </body>
</Html>

This should rectify the issue and ensure proper rendering in the production build

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

Encountering an issue while fetching information from a JSON file using JavaScript

I am encountering an Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data let mydata = JSON.parse("file.json"); console.log(myJSON) Here is a sample of the JSON file's data: [[1,1,0,1,1,0,0,0,1,1,1,1,1, ...

Exploring the Fundamentals of jQuery Ajax Methods: Introduction to $.get, $.post, $.ajax, $.getScript, and More

Our website utilizes jQuery for ajax calls, using various methods such as $.get, $.getScript, and $.post. I am interested in implementing a session check for every ajax call. Rather than manually adding this check to each method call, is there a more effi ...

Should a Service Worker be automatically installed on each page reload, or only when a user navigates to a new page?

Currently in the process of developing a PWA. I have encountered an issue where the service worker seems to be installing on every page reload or when navigating to a different page within my app. It appears that many files are being cached during the inst ...

What is causing this code to malfunction in AngularJS version 1.2?

Check out this code snippet I wrote using Angular 1.2: http://jsfiddle.net/VmkQy/1/ <div ng-app="app"> Title is: <span my-directive data-title="Test title">{{ title }}</span> </div> angular.module('app', []) .dir ...

Ways to transfer a v-model from the parent component to a template

I'm currently in the process of designing the user interface for a search page and I want to utilize components to help with code reusability. However, I am facing a challenge in figuring out how to pass the model of the page to the search component. ...

Error in VS Code related to Vue Array Prop JSDoc TypeScript: The properties of type 'ArrayConstructor' are not found in type 'MyCustomType[]'

After reading the article "Why I no longer use TypeScript with React and why you might want to switch too", I decided to work on a Vue CLI project using ES6 without TypeScript. Instead, I enabled type checking in Visual Studio Code by utilizing JSDoc / @ty ...

The fs.fsync(fd, callback) function in the node.js API allows you

Can you explain the purpose of fs.fsync(fd, callback) in the Node.js API? fs.fsync(fd, callback) This function is used for asynchronous fsync(2). The completion callback only returns an exception if there is one. fs.fsyncSync(fd) This function is for ...

Scrolling to the active list item in the navigation bar

Having an unordered list with the id mainul in the navigation bar is causing a problem when the page reloads. The navigation bar always starts at the beginning, but I would like it to automatically scroll to the active li element. This is my HTML code: & ...

Pinia store encountering a Typescript Vue component issue: error message "The property 'testStore' does not exist on the 'CreateComponentPublicInstance' type."

Within my <script setup> block, I have imported my testStore. However, whenever I attempt to use this.testStore.name in my Vue file, Vetur displays the following error: Property 'testStore' does not exist on type 'CreateComponentPublic ...

There seems to be a lack of information appearing on the table

I decided to challenge myself by creating a simple test project to dive into Angular concepts such as CRUD functions, utilizing ngFor, and understanding HttpClient methods (specifically get and post). After creating a Firebase database with an API key and ...

There appears to be an issue with the dynamic functionality of RouterLink in Angular 6

user-dashboard.html <ul class="nav flex-column"> <li class="nav-item"> <a class="nav-link" routerLink='/dashboard'>User Dashboard</a> </li> <li class="nav-item" *ngFor="let cat of categories; let i = in ...

I have added the same directive to both of my HTML files

Hello, I am facing an issue with a directive that I have included in two HTML files. The 'app' is set as ng-app. <dir auto=""></div> The code for the directive is as follows: app.directive("auto", function() { scope: { arr : ...

What is the reason for calling Proxy on nested elements?

Trying to organize Cypress methods into a helper object using getters. The idea is to use it like this: todoApp.todoPage.todoApp.main.rows.row .first().should('have.text', 'Pay electric bill'); todoApp.todoPage.todoApp.main.rows.ro ...

Contact creation not working on non-HubSpot form popups

I'm currently experiencing an issue with my website that has non-Hubspot forms. We have successfully integrated the tracking code to generate cookies for users, track their sessions, and enable the non-Hubspot forms. However, we are facing a problem s ...

React Native: Unable to update React state during logout process

Currently, I am working on a mobile app and learning new practices in React every day as a junior developer. Today, I need assistance with implementing a User Logout function. In my Profil.js file, I have the Logout function inside an IconButton that gets ...

Tips for dividing an array based on a defined regex pattern in JavaScript

I want to split a string of text into an array of sentences while preserving the punctuation marks. var text = 'This is the first sentence. This is another sentence! This is a question?' var splitText = text.split(/\b(?<=[.!?])/); split ...

What is the best way to develop a parent component tailored for individual pages in order to integrate react context within it?

I need to use a specific react context for certain pages in my project. Instead of creating the context in the root file app.js, I want to create it only for these specific pages. Is there a way to create a parent component (wrapper) that can be placed ar ...

Is it possible to run my JavaScript script immediately following the execution of npm run build?

Hey everyone! I am trying to run my JavaScript file right after receiving the successful message from npm run build. Currently, I am working on a Vue.js codebase. Is there a way for me to log and create a file in my code repository containing the 'run ...

Employing a provider within a different provider and reciprocally intertwining their functions

I'm currently facing an issue with two providers, which I have injected through the constructor. Here's the code for my user-data.ts file: @Injectable() export class UserDataProvider { constructor(private apiService: ApiServiceProvider) { ...

What advantages does the use of $(e).attr(name,value) offer compared to using e.setAttribute(name,value)?

Scenario: The variable "e" represents an element of type "HtmlElement" and not a "css selector" I am referring to any attribute, not just the standard allowed ones like "atom-type" or "data-atom-type". Regardless of the attribute name, will it function wi ...