Nextjs does not apply Tailwind styles correctly unless they are first applied in the parent component before being applied again in the child component

There seems to be a strange behavior in my Next.js code.

I have a pages file named app/(dashboard)/journals/new/pages.js that utilizes a layout containing a sidebar and a main section. Within this pages file, I am attempting to style the content section, which is passed as a child to the layout component by Next.js.

The issue arises when I try to apply styles to this specific page - the styles do not take effect. However, if I copy the component along with the styles to the layout.tsx page, save it, and then remove it, the styles from the pages.jsx file stick. In essence, Next.js only recognizes styles that are copied to the layout.tsx file. This behavior does not occur with other components or pages. Any insights on this?

// app/(dashboard)/journals/new/pages.tsx

import {
  ResizableHandle,
  ResizablePanel,
  ResizablePanelGroup,
} from "@/components/ui/resizable";

const NewJournal = () => {
  return (
      <div className="w-full min-h-screen flex flex-col">
        <div className='text-3xl text-blue-900 h-36 bg-red-400'>testing</div>
        <div className="w-full">
          <ResizablePanelGroup
            direction="horizontal"
            className="w-full rounded-lg border"
            autoSaveId='maistoria-panel'
          >
            <ResizablePanel defaultSize={80}>
              <div className="flex h-[200px] items-center justify-center p-6">
                <span className="font-semibold">One</span>
              </div>
            </ResizablePanel>
            <ResizableHandle />
            <ResizablePanel defaultSize={20}>
              <div className="flex h-[200px] items-center justify-center p-6">
                <span className="font-semibold bg-red-950 m-4">One</span>
              </div>
            </ResizablePanel>
          </ResizablePanelGroup>
        </div>
      </div>
  );
};

export default NewJournal;


// app/(dashboard)/layout.tsx


"use client"

import MobileSidebar from '@/components/dashboard/MobileSidebar';
import Sidebar from '@/components/dashboard/Sidebar';
import Logo from '@/components/logo';

const DashboardLayout = ({ children }: { children: React.ReactNode }) => {
  return (
    <main className='flex'>
      <Sidebar />
      <div className="md:hidden">
        <MobileSidebar />
      </div>
      <section className='w-full min-h-screen'>
        {children}
      </section>
    </main>
  )
}

export default DashboardLayout;
import type { Config } from "tailwindcss"

const config = {
  darkMode: ["class"],
  content: [
    './pages/**/*.{ts,tsx}',
    './components/**/*.{ts,tsx}',
    './app/**/*.{ts,tsx}',
    './src/**/*.{ts,tsx}',
    ],
  prefix: "",
  theme: {
    container: {
      center: true,
      padding: "2rem",
      screens: {
        "2xl": "1400px",
      },
    },
    extend: {
      colors: {
        border: "hsl(var(--border))",
        ...
        // Other color configurations omitted for brevity
      },
      borderRadius: {
        lg: "var(--radius)",
        md: "calc(var(--radius) - 2px)",
        sm: "calc(var(--radius) - 4px)",
      },
      keyframes: {
        "accordion-down": {
          from: { height: "0" },
          to: { height: "var(--radix-accordion-content-height)" },
        },
        "accordion-up": {
          from: { height: "var(--radix-accordion-content-height)" },
          to: { height: "0" },
        },
      },
      animation: {
        "accordion-down": "accordion-down 0.2s ease-out",
        "accordion-up": "accordion-up 0.2s ease-out",
      },
    },
  },
  plugins: [require("tailwindcss-animate")],
} satisfies Config

export default config

Please note: The styles seem to be applying correctly in other pages and components without any issues.

Answer №1

If the tailwind.config.js file is not configured properly, You need to specify your routes with specific layouts within the content like so:

 module.exports = {
  content: [
    './app/**/*.{js,ts,jsx,tsx,mdx}', // Remember to include the `app` directory.
    './pages/**/*.{js,ts,jsx,tsx,mdx}',
    './components/**/*.{js,ts,jsx,tsx,mdx}',
 
    // Alternatively, if using the `src` directory:
    './src/**/*.{js,ts,jsx,tsx,mdx}',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

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

Retrieve the top-level field from a Firestore document

I currently have a firestore database located at: https://i.sstatic.net/ebhXX.png My objective is to extract the value of the field "points/0/title" from its root. Is there any way to achieve this? ...

Checking for a defined data type using the ternary operator in Typescript

I am currently working on validating data types before passing them as props to a component. However, I encountered an issue with the following code where I received this error message: 'Teacher' only refers to a type, but is being used as a valu ...

Utilizing inline styles with the asset pipeline feature in Rails

<%= link_to root_path do %> <div class=""> <div style="background-image:<%= image_tag " image1.png "%>"> </div> <div> <h2>Title</h2> <p>Paragraph</p> </div& ...

Exploring the React library's map function to enhance

I started with a grouped object but later replaced it with an array: const objectToArray = ["Since", Array(7)]. Now, I am trying to map through this array to access objectToArray[0] (which represents a picture) and objectToArray[1] (a list of questions). T ...

I'm having trouble with my tablet view taking priority over my desktop view - what could be causing this issue?

Styling for different screen sizes: @media (min-width: 991px) and (max-width:768px){} .container, .container1, .container2 .container3{ float: left; } .container1{ width: 50%; } .container2{ width: 50%; } .container3{ width: 100%; } /**** ...

Give a style attribute to every other child element within a class. Each class should function independently, including their final child which contains an if statement

UPDATE: I kindly request a review of the answers provided. Utilizing JavaScript for this task may not be the most efficient method. I will keep this question as it is, as it could potentially benefit someone else. The aim is for every ".ulInCollapse li" t ...

Tips for preventing unnecessary re-renders when adding an item to a list

I'm facing an issue with my functional component that requires updating. export const VendorCategory = ({setShowProgress, user}) => { const classes = useStyles() const theme = useTheme() const listElements = [] const [dummyCategory, ...

I encountered an issue with Meteor js and a PDF library where Buffer was not defined

Every time I attempt to incorporate a pdf library into my Meteor/React application, I encounter the error Uncaught ReferenceError: Buffer is not defined at util.js. I have explored various solutions such as using init.js, executing meteor npm i --save buff ...

InitAuth0 Auth0 encountering deepPartial error in Next.js with TypeScript setup

Having some trouble setting up auth0 with nextjs using typescript. When I try to initialize Auth0, I encounter an error regarding deep partials, Argument of type '{ clientId: string; clientSecret: string; scope: string; domain: string; redirectUri: st ...

Delete items from several arrays on a click event in React

I'm working with an array of objects that contain multiple arrays. My goal is to remove the item when a button is clicked, but I keep getting undefined as a result. JSON Data [ { "services": [ { "id": "1b9 ...

Sending a date object from a server component to a client component within Next.js version 13

As I transition my next.js project to an app directory style project, I am faced with the task of fetching data from an API server that includes dates. Upon retrieving the data, I need to filter and order it based on the date field. Currently, I am utili ...

"Enhance your design with a stylish hover effect using

Can you have markup like this while also having a background overlay on hover? <figure class="gallery-item"> <div class="gallery-icon landscape" style="background-color: #333;"> <a href="www.google.com"> <img src ...

"Looking to conceal a div that contains text when a loop fails to return any posts? Here's

In the first loop, posts related to the current post are retrieved based on the primary tag. The second loop, displayed below, fetches posts based on the secondary tag. To prevent duplicate posts, any post displayed by the first loop is excluded from the r ...

Using "overflow-x: hidden" is ineffective on mobile devices

I'm facing an issue where the CSS property overflow-x: hidden is not working on mobile devices. Here is a snippet of my HTML code: <div class="topbar"> <div class="navbar-brand float-left"> <a href="#"><img src="asset ...

Executing Parent function in Reactjs (Triggering Drawer when menu item is clicked using Material-ui)

I'm having some trouble trying to activate the drawer when a user clicks on a menu item. Unfortunately, my current solution is not working as expected. Can anyone provide assistance? Parent import React, { Component } from 'react'; // Impo ...

Struggling to align a button in the middle of an HTML form using flexbox - see code snippet below

Hey everyone, I’m having trouble with centering my button within the flexbox container with the class "hero". Any thoughts on why this might be happening? The button appears below the email form but is aligned to the left of it (starting at the same pos ...

Steps for displaying a bootstrap modal in alignment with the triggering button's position

Recently diving into the world of bootstrap and AngularJs has been quite the learning experience for me. One challenge I encountered was trying to implement a bootstrap modal dialog box to show additional details within a table column. My goal was to hav ...

I hope to transfer the input values from the Form to the handleSubmit() function

I'm currently facing an issue while trying to pass input values from a form to the handleSubmit() function. I am using e.target.value but getting an error saying 'cannot property 'value' of undefined.' Here is the code block for t ...

If a particular <td> element is present on the page, exhibit a unique <div>

I have a webpage with various HTML elements. <div class="alert">Your message was not sent.</div> <p class="pending">Email Pending</p> I would like to display the div.alert only if the p.pending is present. Is ...

The functionality of Div.Show is not consistent while the code is running, yet it operates smoothly when the code is

When I trigger a save action, a hidden div is supposed to appear. The structure of this div is like so: <div class="Working" style="display: none;">Message</div> As part of the save process in my code, the following logic is implemented: fun ...