I am having trouble getting the daisyui theme to properly apply in my next.js application

Utilizing a theme from "Daisyui," a tailwind-based CSS component library available at , is my current endeavor. In my next.js application, the starting point is pages/_app.tsx. As per examples and documentation on the website, it appears that the theme gets passed on from the top-level component to all inner components.

The process involves adding daisyui to the tailwind.config.js file in this manner:
https://i.sstatic.net/iTwKb.png

This configuration resulted in my tailwind.config.js looking like:

/** @type {import('tailwindcss').Config} */
module.exports = {
    //...
    content: ['./pages/**/*.{js,ts,jsx,tsx}'],
    plugins: [require("daisyui")],
    daisyui: {
      themes: true
    }
  }

With true signifying inclusion of all themes.

I've integrated the "synthwave" theme into the top level component of my next.js app, which encapsulates Component within a div tag:

import '../styles/globals.css'
import type { AppProps } from 'next/app'

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <div theme-data="synthwave">
  <Component {...pageProps} />
  </div>
  )
}

export default MyApp

In addition, I created a postcss.config.js file:

module.exports = {
    plugins: ['tailwindcss', 'autoprefixer'],
  };

Furthermore, I included:

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

at the beginning of the globals.css file.

However, this setup failed as the resulting webpage displayed as plain white.
https://i.sstatic.net/vA6Y3.jpg

Strangely, when I amended daisyui: { themes: true } in tailwind.config.js to

daisyui: { themes: ["synthwave"] }
, the webpage was correctly themed with Daisyui's synthwave:
https://i.sstatic.net/bknAu.jpg

Presumably, this works because it overrides ALL styles across every page, which is not the desired outcome. The goal is to define a theme for the entire app while retaining the ability to override it on specific pages if needed. How can I achieve this?

Answer №1

This resource is incredibly useful https://nextjs.org/docs/advanced-features/custom-document. By creating a file named _document.js, you can modify the tags used for page rendering. Following the instructions in the link, I generated a _document.js file within the pages directory with the following contents:
pages/_document.js:

import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
  return (
    <Html data-theme="synthwave">
      <Head />
        <body>
          <Main />
          <NextScript />
        </body>
    </Html>
  )
}

Take note of this line of code

<Html data-theme="synthwave">
. It correctly applied the synthwave theme from daisyui to index.tsx.

Answer №2

Encountered the same issue but found a solution that worked for me

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    './pages/**/*.{js,ts,jsx,tsx,mdx}',
    './components/**/*.{js,ts,jsx,tsx,mdx}',
    './app/**/*.{js,ts,jsx,tsx,mdx}',
  ],
  theme: {
    extend: {
    },
  },
  daisyui: {
    themes: true,
  },
  plugins: [require("daisyui")],
}
export default function RootLayout({ children }) {
  return (
    <html lang="en" data-theme={'aqua'}>
      <body className={inter.className}>{children}</body>
    </html>
  )
}

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

Unlock the secret: Using Javascript and Protractor to uncover the elusive "hidden" style attribute

My website has a search feature that displays a warning message when invalid data, such as special characters, is used in the search. Upon loading the page, the CSS initially loads like this: <div class="searchError" id="isearchError" style="display: ...

When using JavaScript, an error may occur that says "Cannot read property 'style' of null," but strangely, this issue disappears when the code is copied and

I seem to be encountering an error in my codepen and I can't figure out what I'm doing wrong. It appears that I may have misplaced something, but I would greatly appreciate any help or explanation of my mistake. function on() { document ...

Achieve this effect by making sure that when a user scrolls on their browser, 50% of the content is entered into view and the remaining 50%

Is there a way to achieve this effect? Specifically, when the user scrolls in the browser, 50% of the content is displayed at the top and the other 50% is shown at the bottom. ...

Tabbable bootstrap with dropdown functionality

I am currently in the process of integrating Bootstrap tabs and dropdown menus using version 2.3.4 <div class="container"> <div class="navbar navbar-fixed-top"> <div class="navbar-inner"> <div class="container-fluid"> ...

The React-Bootstrap Table in a Vite project lacks vertical scrolling when the "responsive" property is enabled

I'm encountering an issue with my React-Bootstrap Table overflowing the viewport in the x-direction on smaller screens. I've tried making it responsive by using the responsive prop, but the overflow-x: scroll feature isn't functioning proper ...

What is the best way to use JavaScript or jQuery to place divs in the desired position?

Here is the code snippet I am working with and I need assistance in positioning the div elements accordingly: <div id="outer" style="width:300px"> <canvas></canvas> <div id="inner"> <div class="a"> val1 </div> <div c ...

Display a div every 3 seconds with the help of jQuery

I am seeking a solution to display the second div (box2) every 3 seconds. Can anyone help me achieve this using jQuery? <div id="box1" style="background-color:#0000FF"> <h3>This is a heading in a div element</h3> <p>This ...

The footer fails to adhere to the bottom of the page

Struggling to keep my footer at the bottom of the page when there is minimal content. I attempted using: position:absolute; and bottom:0; It appears to stay in place, but when more content is added, it ends up going "under" the footer that remains n ...

Problem with pseudo element hover effect on margin and padding

I designed four interactive boxes, where clicking or hovering on one box increases the padding or margin, causing it to overlap with the adjacent boxes. I attempted the following CSS: .marketing-service.active { margin: 0; } and .marketing-service. ...

Is there a way to adjust the size of a table display to ensure that both vertical and horizontal scroll bars are constantly visible?

[edits added below per the request to see code. Edits also added to the original post to clarify the ask - marked in bold] My application features a broad table with the potential for many rows, generated by django-tables2 using the bootstrap5-responsive ...

Encountering issues trying to display state value retrieved from an AJAX call within componentDidMount in React

I recently implemented an AJAX call in my React application using Axios, but I am a bit confused about how to handle the response data. Here is the code snippet that I used: componentDidMount() { axios.get('https://jsonplaceholder.typicode.com/us ...

"Encounter an error due to an unexpected token, specifically 'Doctype

In my current project, I am utilizing react, react-router-dom, and express. Whenever I initiate a request by clicking on a link, everything functions correctly. However, issues arise when sending requests via the browser. Strangely, requests such as http ...

Tips for placing an element in the top left corner and turning it into a functional "back to top" button

I need to add a "back to top" button on my website and I want to place it in the top left corner, as shown in the image above. Here is the HTML code I am using in my Jekyll site: <body> <a name="top"></a> <a href="#top" id="to ...

There are currently no articles found that match the search query

Recently, I started working with Django, but I am facing an issue with slug. Whenever I send a request to the Slug, I encounter an error. The error message I receive is: Articles matching query does not exist. Furthermore, I am facing another error while ...

Adding a vertical menu in PHP is a great way to enhance the

<ul id="verticalmenu"> <li><a href="http://www.yourlink.com/">Home</a></li> <li><a href="http://www.yourlink.com/">Vertical Menu</a></li> <li><a href="http://www.yourlink.com/">Dro ...

What's the best way to modify the style property of a button when it's clicked in

I am working with a react element that I need to hide when a button is clicked. The styles for the element are set in the constructor like this: constructor(props) { super(props); this.state = { display: 'block' }; this. ...

The issue with NextJs Firebase Hosting not reading environment variables when built on GitHub Actions

I am currently in the process of deploying my NextJs application on Firebase hosting using Github Actions. Below is the workflow file I have set up for this deployment: name: Deploy to Firebase Hosting on merge on: push: branches: - main jobs: ...

When working with TextareaAutosize component in MUI, an issue surfaces where you need to click on the textarea again after entering each character

One issue that arises when using TextareaAutosize from MUI is the need to click on the textarea again after entering each character. This problem specifically occurs when utilizing StyledTextarea = styled(TextareaAutosize) The initial code snippet accompl ...

[react]: useMemo will always return the first element from an array if provided as an argument

Take a look at this code snippet export default function App() { const [urls] = React.useMemo( () => ['example.com', 'sample.com'],[] ) return ( <div className="App"> <button onClick={() => c ...

How to handle HTML responses instead of data when using axios.post in Node.js and React

My requirement is to have a functionality where, upon clicking the SUBMIT button, some data will be posted using axios.post const sb = async () => { axios.post('/tagSearch',{ qual: qualQuery, sem: semQuery, etc: etcQuery, ...