返回介绍

Customizing Colors

发布于 2024-07-30 22:16:08 字数 16222 浏览 0 评论 0 收藏 0

Customizing the default color palette for your project.

​Default color palette

Tailwind includes an expertly-crafted default color palette out-of-the-box that is a great starting point if you don’t have your own specific branding in mind.

Slate50#f8fafc100#f1f5f9200#e2e8f0300#cbd5e1400#94a3b8500#64748b600#475569700#334155800#1e293b900#0f172a950#020617Gray50#f9fafb100#f3f4f6200#e5e7eb300#d1d5db400#9ca3af500#6b7280600#4b5563700#374151800#1f2937900#111827950#030712Zinc50#fafafa100#f4f4f5200#e4e4e7300#d4d4d8400#a1a1aa500#71717a600#52525b700#3f3f46800#27272a900#18181b950#09090bNeutral50#fafafa100#f5f5f5200#e5e5e5300#d4d4d4400#a3a3a3500#737373600#525252700#404040800#262626900#171717950#0a0a0aStone50#fafaf9100#f5f5f4200#e7e5e4300#d6d3d1400#a8a29e500#78716c600#57534e700#44403c800#292524900#1c1917950#0c0a09Red50#fef2f2100#fee2e2200#fecaca300#fca5a5400#f87171500#ef4444600#dc2626700#b91c1c800#991b1b900#7f1d1d950#450a0aOrange50#fff7ed100#ffedd5200#fed7aa300#fdba74400#fb923c500#f97316600#ea580c700#c2410c800#9a3412900#7c2d12950#431407Amber50#fffbeb100#fef3c7200#fde68a300#fcd34d400#fbbf24500#f59e0b600#d97706700#b45309800#92400e900#78350f950#451a03Yellow50#fefce8100#fef9c3200#fef08a300#fde047400#facc15500#eab308600#ca8a04700#a16207800#854d0e900#713f12950#422006Lime50#f7fee7100#ecfccb200#d9f99d300#bef264400#a3e635500#84cc16600#65a30d700#4d7c0f800#3f6212900#365314950#1a2e05Green50#f0fdf4100#dcfce7200#bbf7d0300#86efac400#4ade80500#22c55e600#16a34a700#15803d800#166534900#14532d950#052e16Emerald50#ecfdf5100#d1fae5200#a7f3d0300#6ee7b7400#34d399500#10b981600#059669700#047857800#065f46900#064e3b950#022c22Teal50#f0fdfa100#ccfbf1200#99f6e4300#5eead4400#2dd4bf500#14b8a6600#0d9488700#0f766e800#115e59900#134e4a950#042f2eCyan50#ecfeff100#cffafe200#a5f3fc300#67e8f9400#22d3ee500#06b6d4600#0891b2700#0e7490800#155e75900#164e63950#083344Sky50#f0f9ff100#e0f2fe200#bae6fd300#7dd3fc400#38bdf8500#0ea5e9600#0284c7700#0369a1800#075985900#0c4a6e950#082f49Blue50#eff6ff100#dbeafe200#bfdbfe300#93c5fd400#60a5fa500#3b82f6600#2563eb700#1d4ed8800#1e40af900#1e3a8a950#172554Indigo50#eef2ff100#e0e7ff200#c7d2fe300#a5b4fc400#818cf8500#6366f1600#4f46e5700#4338ca800#3730a3900#312e81950#1e1b4bViolet50#f5f3ff100#ede9fe200#ddd6fe300#c4b5fd400#a78bfa500#8b5cf6600#7c3aed700#6d28d9800#5b21b6900#4c1d95950#2e1065Purple50#faf5ff100#f3e8ff200#e9d5ff300#d8b4fe400#c084fc500#a855f7600#9333ea700#7e22ce800#6b21a8900#581c87950#3b0764Fuchsia50#fdf4ff100#fae8ff200#f5d0fe300#f0abfc400#e879f9500#d946ef600#c026d3700#a21caf800#86198f900#701a75950#4a044ePink50#fdf2f8100#fce7f3200#fbcfe8300#f9a8d4400#f472b6500#ec4899600#db2777700#be185d800#9d174d900#831843950#500724Rose50#fff1f2100#ffe4e6200#fecdd3300#fda4af400#fb7185500#f43f5e600#e11d48700#be123c800#9f1239900#881337950#4c0519

But when you do need to customize your palette, you can configure your colors under the colors key in the theme section of your tailwind.config.js file:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    colors: {
      // Configure your color palette here
    }
  }
}

When it comes to building a custom color palette, you can either configure your own custom colors from scratch if you know exactly what you want, or curate your colors from our extensive included color palette if you want a head start.


Using custom colors

If you’d like to completely replace the default color palette with your own custom colors, add your colors directly under the theme.colors section of your configuration file:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    colors: {
      transparent: 'transparent',
      current: 'currentColor',
      'white': '#ffffff',
      'purple': '#3f3cbb',
      'midnight': '#121063',
      'metal': '#565584',
      'tahiti': '#3ab7bf',
      'silver': '#ecebff',
      'bubble-gum': '#ff77e9',
      'bermuda': '#78dcca',
    },
  },
}

By default, these colors will be made available everywhere in the framework where you use colors, like the text color utilities, border color utilities, background color utilities, and more.

<div class="bg-midnight text-tahiti">
  <!-- ... -->
</div>

Don’t forget to include values like transparent and currentColor if you want to use them in your project.

Color object syntax

When your palette includes multiple shades of the same color, it can be convenient to group them together using our nested color object syntax:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    colors: {
      transparent: 'transparent',
      current: 'currentColor',
      'white': '#ffffff',
      'tahiti': {
        100: '#cffafe',
        200: '#a5f3fc',
        300: '#67e8f9',
        400: '#22d3ee',
        500: '#06b6d4',
        600: '#0891b2',
        700: '#0e7490',
        800: '#155e75',
        900: '#164e63',
      },
      // ...
    },
  },
}

The nested keys will be combined with the parent key to form class names like bg-tahiti-400.

Like many other places in Tailwind, the special DEFAULT key can be used when you want to define a value with no suffix:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    colors: {
      // ...
      'tahiti': {
        light: '#67e8f9',
        DEFAULT: '#06b6d4',
        dark: '#0e7490',
      },
      // ...
    },
  },
}

This will create classes like bg-tahiti, bg-tahiti-light, and bg-tahiti-dark.

Arbitrary values

If you need a one-off custom color in your project, consider using Tailwind’s arbitrary value notation to generate a class for that color on-demand instead of adding it to your theme:

<button class="bg-[#1da1f2] text-white ...">
  <svg><!-- ... --></svg>
  Share on Twitter
</button>

Learn more in the using arbitrary values documentation.

Generating colors

If you’re wondering how we automatically generated the 50–950 shades of each color, bad news — color is complicated and to get the absolute best results we picked all of Tailwind’s default colors by hand, meticulously balancing them by eye and testing them in real designs to make sure we were happy with them.

If you are creating your own custom color palette and don’t feel confident doing it by hand, UI Colors is a great tool that can give you a good starting point based on any custom color.

Two other useful tools we recommend for building your own palettes are Palettte and ColorBox — they won’t do the work for you but their interfaces are well-designed for doing this sort of work.


Using the default colors

If you don’t have a set of completely custom colors in mind for your project, you can curate your colors from our default palette by importing tailwindcss/colors in your configuration file and choosing the colors you want to use:

tailwind.config.js
const colors = require('tailwindcss/colors')

module.exports = {
  theme: {
    colors: {
      transparent: 'transparent',
      current: 'currentColor',
      black: colors.black,
      white: colors.white,
      gray: colors.gray,
      emerald: colors.emerald,
      indigo: colors.indigo,
      yellow: colors.yellow,
    },
  },
}

This can be helpful if you want to deliberately limit your color palette and reduce the number of class names suggested by IntelliSense.

Aliasing color names

You can also alias the colors in our default palette to make the names simpler and easier to remember:

tailwind.config.js
const colors = require('tailwindcss/colors')

module.exports = {
  theme: {
    colors: {
      transparent: 'transparent',
      current: 'currentColor',
      black: colors.black,
      white: colors.white,
      gray: colors.slate,
      green: colors.emerald,
      purple: colors.violet,
      yellow: colors.amber,
      pink: colors.fuchsia,
    },
  },
}

This is especially common for grays, as you usually only use one set in any given project and it’s nice to be able to type bg-gray-300 instead of bg-neutral-300 for example.

Adding additional colors

If you’d like to add a brand new color to the default palette, add it in the theme.extend.colors section of your configuration file:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      colors: {
        brown: {
          50: '#fdf8f6',
          100: '#f2e8e5',
          200: '#eaddd7',
          300: '#e0cec7',
          400: '#d2bab0',
          500: '#bfa094',
          600: '#a18072',
          700: '#977669',
          800: '#846358',
          900: '#43302b',
        },
      }
    },
  },
}

You can also use theme.extend.colors to add additional shades to an existing color if it’s needed for your design:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      colors: {
        blue: {
          950: '#17275c',
        },
      }
    },
  },
}

Disabling a default color

If you’d like to disable any of the default colors, the best approach is to override the default color palette and just include the colors you do want:

tailwind.config.js
const colors = require('tailwindcss/colors')

module.exports = {
  theme: {
    colors: {
      transparent: 'transparent',
      current: 'currentColor',
      black: colors.black,
      white: colors.white,
      gray: colors.gray,
      emerald: colors.emerald,
      indigo: colors.indigo,
      yellow: colors.yellow,
    },
  },
}

Naming your colors

Tailwind uses literal color names (like red, green, etc.) and a numeric scale (where 50 is light and 900 is dark) by default. We think this is the best choice for most projects, and have found it easier to maintain than using abstract names like primary or danger.

That said, you can name your colors in Tailwind whatever you like, and if you’re working on a project that needs to support multiple themes for example, it might make sense to use more abstract names:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    colors: {
      primary: '#5c6ac4',
      secondary: '#ecc94b',
      // ...
    }
  }
}

You can configure those colors explicitly like we have above, or you can pull in colors from our default color palette and alias them:

tailwind.config.js
const colors = require('tailwindcss/colors')

module.exports = {
  theme: {
    colors: {
      primary: colors.indigo,
      secondary: colors.yellow,
      neutral: colors.gray,
    }
  }
}

Again, we recommend sticking to the default naming convention for most projects, and only using abstract names if you have a really good reason.


Using CSS variables

If you’d like to define your colors as CSS variables, you’ll need to define those variables as just the color channels if you want them to work with the opacity modifier syntax:

Define your CSS variables as channels with no color space function

main.css
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  :root {
    --color-primary: 255 115 179;
    --color-secondary: 111 114 185;
    /* ... */
  }
}

Don’t include the color space function or opacity modifiers won’t work

main.css
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  :root {
    --color-primary: rgb(255 115 179);
    --color-secondary: rgb(111 114 185);
    /* ... */
  }
}

Then define your colors in your configuration file, being sure to include the color space you’re using, and the special <alpha-value> placeholder that Tailwind will use to inject the alpha value when using an opacity modifier:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    colors: {
      // Using modern `rgb`
      primary: 'rgb(var(--color-primary) / <alpha-value>)',
      secondary: 'rgb(var(--color-secondary) / <alpha-value>)',

      // Using modern `hsl`
      primary: 'hsl(var(--color-primary) / <alpha-value>)',
      secondary: 'hsl(var(--color-secondary) / <alpha-value>)',

      // Using legacy `rgba`
      primary: 'rgba(var(--color-primary), <alpha-value>)',
      secondary: 'rgba(var(--color-secondary), <alpha-value>)',
    }
  }
}

When defining your colors this way, make sure that the format of your CSS variables is correct for the color function you are using. You’ll want to use spaces if using the modern space-separated syntax, and commas if using legacy functions like rgba or hsla:

main.css
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  :root {
    /* For rgb(255 115 179 / <alpha-value>) */
    --color-primary: 255 115 179;

    /* For hsl(198deg 93% 60% / <alpha-value>) */
    --color-primary: 198deg 93% 60%;

    /* For rgba(255, 115, 179, <alpha-value>) */
    --color-primary: 255, 115, 179;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文