我正在try 使用"Daisyui"中的一个主题,它是一个基于tailwind 的CSS组件库https://daisyui.com/.我有一个next.js应用程序,入口点是pages/_app.tsx.从查看示例和阅读网站的文档来看,主题似乎是从最高组件传递到所有内部组件.

You add daisyui to the tailwind.config.js file, Like this:
enter image description here

我让我的tailwind.config.js%文件看起来是这样的:

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

true表示包含所有主题.

我在next.js应用程序的最高级别组件中添加了主题"SynthWave",该组件是一个包装大约Component个的div标签:

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

我还制作了一个postcss.config.js文件

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

然后我进口了

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

在global als.css的顶部.

This fails because the resulting webpage is just plain white.
enter image description here

But what's weird is when I change daisyui: { themes: true } in tailwind.config.js to daisyui: { themes: ["synthwave"] }. The webpage IS correctly themed with daisyui's synthwave:
enter image description here

我认为这只会起作用,因为它覆盖了每个页面上的所有样式,而我不希望这样.我希望能够声明任何页面上的不同主题,如果我想的话.那么,我如何正确地设置整个应用程序的主题,以便我可以根据需要在单独的页面上覆盖它?

推荐答案

This link is very helpful https://nextjs.org/docs/advanced-features/custom-document. You can make a file called _document.js that can update the and tags used to render a page. Just like the link says, I created a file called _document.js in pages that looks like this:
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>
  )
}

请注意这段代码<Html data-theme="synthwave">.它正确地将index.tsx设置为daisyuisynthwave主题.

Css相关问答推荐

为什么图像加载请求没有显示在我的网络请求中?

如何提高数学输出的质量?

为什么我的Angular material css文件不工作?

如何在CSS中使用nextJs来指定基色?

Vue3日期 Select 器:作用域样式不起作用

如何仅以WooCommerce单一产品页面为目标?

Safari CSS扫描动画效果不起作用

设置ScaleImageButton的样式

在 Tailwind css 中,间距对我不起作用

老虎机插槽上的 CSS 对齐问题

适合剩余高度

我怎样才能让这个边框出现在按钮元素的上方?

如何在 CSS Grid 布局中指定行高?

如何设置表格单元格的最大高度?

如何从标题中删除粗体?

让 div 扩展全高

go 除文本输入的内阴影

CSS3 过渡:过渡:全部是否比过渡:x慢?

使用 CSS 使 span 不可点击

如何重置或覆盖 IE CSS 过滤器?