最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to handle colors in Next.js 14 with Tailwind CSS? - Stack Overflow

programmeradmin0浏览0评论

I am developing a product application using Next.js 14 and Shadcn with Tailwind CSS. I want to know the best approach to handling colors in my project. Since this is a product, I want to ensure that colors are managed efficiently. Currently, I am using inline styles for colors, as shown below.

 <div className="bg-gray-200 rounded-lg mt-5 text-sm">

Can anyone tell me the standard way to manage colors easily, especially if the product's color scheme changes? I am new to Next.js. In the future, I hope to make changes for dark mode as well.

I am developing a product application using Next.js 14 and Shadcn with Tailwind CSS. I want to know the best approach to handling colors in my project. Since this is a product, I want to ensure that colors are managed efficiently. Currently, I am using inline styles for colors, as shown below.

 <div className="bg-gray-200 rounded-lg mt-5 text-sm">

Can anyone tell me the standard way to manage colors easily, especially if the product's color scheme changes? I am new to Next.js. In the future, I hope to make changes for dark mode as well.

Share Improve this question asked Mar 5 at 16:44 StarsStars 234 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Not sure what you mean by "best approach to handling colors". If you want to apply custom colors you would extend them through a theme, such as:

// tailwind.config.js
  module.exports = {
    theme: {
      extend: {
        colors: {
          'custom-color':'#001122',
        }
      },
    },  
  }

or if you want just set colors do something like:

theme: {
  extend: {
    colors: {
      'primary': '#000',
      'secoondary': '#111',
    }
  },
},

then you'd use the color for example with changing the text color it be

<p class="text-primary">foobar</p>

or like in your example it would be:

<div className="bg-primary rounded-lg mt-5 text-sm">

or

<div className="bg-custom-color rounded-lg mt-5 text-sm">

if you dont want to use the default colors you can override them in the config:

  • Customizing your theme

Have you considered theme variables?

https://tailwindcss/docs/theme

You should be able to define your colour scheme as variables and then reference your theme variables. Should you want to change your colour scheme, it should all be centralised in this one place.

In a .css file you can put

@import "tailwindcss";
@theme {
  --color-mint-500: oklch(0.72 0.11 178);
}

and then when you want to reference your colours you can use

<div style="background-color: var(--color-mint-500)">
  <!-- ... -->
</div>
发布评论

评论列表(0)

  1. 暂无评论