Customizing Colors

Customizing the default color palette for your project.

Overview

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.

Colors - 图1

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:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. colors: {
  5. // Configure your color palette here
  6. }
  7. }
  8. }

When it comes to building a custom color palette, you can either curate your colors from our extensive included color palette, or configure your own custom colors by adding your specific color values directly.


Curating colors

If you don’t have a set of completely custom colors in mind for your project, you can curate your colors from our complete color palette by importing 'tailwindcss/colors' into your config file and choosing the colors you like.

  1. // tailwind.config.js
  2. const colors = require('tailwindcss/colors')
  3. module.exports = {
  4. theme: {
  5. colors: {
  6. transparent: 'transparent',
  7. current: 'currentColor',
  8. black: colors.black,
  9. white: colors.white,
  10. gray: colors.trueGray,
  11. indigo: colors.indigo,
  12. red: colors.rose,
  13. yellow: colors.amber,
  14. }
  15. }
  16. }

Don’t forget to include transparent and current if you’d like those available in your project.

Although each color has a specific name, you’re encouraged to alias them however you like in your own projects. We even do this in the default configuration, aliasing coolGray to gray, violet to purple, amber to yellow, and emerald to green.

See our complete color palette reference to see the colors that are available to choose from by default.


Custom colors

You can build a completely custom palette by adding your own color values from scratch:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. colors: {
  5. transparent: 'transparent',
  6. current: 'currentColor',
  7. blue: {
  8. light: '#85d7ff',
  9. DEFAULT: '#1fb6ff',
  10. dark: '#009eeb',
  11. },
  12. pink: {
  13. light: '#ff7ce5',
  14. DEFAULT: '#ff49db',
  15. dark: '#ff16d1',
  16. },
  17. gray: {
  18. darkest: '#1f2d3d',
  19. dark: '#3c4858',
  20. DEFAULT: '#c0ccda',
  21. light: '#e0e6ed',
  22. lightest: '#f9fafc',
  23. }
  24. }
  25. }
  26. }

By default, these colors are automatically shared by all color-driven utilities, like textColor, backgroundColor, borderColor, and more.


Color object syntax

You can see above that we’ve defined our colors using a nested object notation where the nested keys are added to the base color name as modifiers:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. colors: {
  5. indigo: {
  6. light: '#b3bcf5',
  7. DEFAULT: '#5c6ac4',
  8. dark: '#202e78',
  9. }
  10. }
  11. }
  12. }

The different segments of the color name are combined to form class names like bg-indigo-light.

Like many other places in Tailwind, the DEFAULT key is special and means “no modifier”, so this configuration would generate classes like text-indigo and bg-indigo, not text-indigo-DEFAULT or bg-indigo-DEFAULT.

You can also define your colors as simple strings instead of objects:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. colors: {
  5. 'indigo-lighter': '#b3bcf5',
  6. 'indigo': '#5c6ac4',
  7. 'indigo-dark': '#202e78',
  8. }
  9. }
  10. }

Note that when accessing colors using the theme() function you need to use the same notation you used to define them.

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. colors: {
  5. indigo: {
  6. // theme('colors.indigo.light')
  7. light: '#b3bcf5',
  8. // theme('colors.indigo.DEFAULT')
  9. DEFAULT: '#5c6ac4',
  10. },
  11. // theme('colors.indigo-dark')
  12. 'indigo-dark': '#202e78',
  13. }
  14. }
  15. }

Extending the defaults

As described in the theme documentation, if you’d like to extend the default color palette rather than override it, you can do so using the theme.extend.colors section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. extend: {
  5. colors: {
  6. 'regal-blue': '#243c5a',
  7. }
  8. }
  9. }
  10. }

This will generate classes like bg-regal-blue in addition to all of Tailwind’s default colors.

These extensions are merged deeply, so if you’d like to add an additional shade to one of Tailwind’s default colors, you can do so like this:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. extend: {
  5. colors: {
  6. blue: {
  7. 450: '#5F99F7'
  8. },
  9. }
  10. }
  11. }
  12. }

This will add classes like bg-blue-450 without losing existing classes like bg-blue-400 or bg-blue-500.


Disabling a default color

If you’d like to disable a default color because you aren’t using it in your project, the easiest approach is to just build a new color palette that doesn’t include the color you’d like to disable.

For example, this tailwind.config.js file excludes teal, orange, and pink, but includes the rest of the default colors:

  1. // tailwind.config.js
  2. const colors = require('tailwindcss/colors')
  3. module.exports = {
  4. theme: {
  5. colors: {
  6. transparent: 'transparent',
  7. current: 'currentColor',
  8. black: colors.black,
  9. white: colors.white,
  10. gray: colors.coolGray,
  11. red: colors.red,
  12. yellow: colors.amber,
  13. blue: colors.blue,
  14. pink: colors.pink,
  15. }
  16. }
  17. }

Alternatively, you could leave the color palette untouched and rely on tree-shaking unused styles to remove the colors you’re not using.


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. This ends up being fairly practical for most projects, but there are good reasons to use other naming conventions as well.

For example, if you’re working on a project that needs to support multiple themes, it might make sense to use more abstract names like primary and secondary:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. colors: {
  5. primary: '#5c6ac4',
  6. secondary: '#ecc94b',
  7. // ...
  8. }
  9. }
  10. }

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

  1. // tailwind.config.js
  2. const colors = require('tailwindcss/colors')
  3. module.exports = {
  4. theme: {
  5. colors: {
  6. primary: colors.indigo,
  7. secondary: colors.yellow,
  8. neutral: colors.gray,
  9. }
  10. }
  11. }

You could even define these colors using CSS custom properties (variables) to make it easy to switch themes on the client:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. colors: {
  5. primary: 'var(--color-primary)',
  6. secondary: 'var(--color-secondary)',
  7. // ...
  8. }
  9. }
  10. }
  1. /* In your CSS */
  2. :root {
  3. --color-primary: #5c6ac4;
  4. --color-secondary: #ecc94b;
  5. /* ... */
  6. }
  7. @tailwind base;
  8. @tailwind components;
  9. @tailwind utilities;

Note that colors defined using custom properties will not work with color opacity utilities like bg-opacity-50 without additional configuration. See this example repository for more information on how to make this work.


Generating colors

A common question we get is “how do I generate the 50–900 shades of my own custom colors?”.

Bad news, color is complicated and despite trying dozens of different tools, we’ve yet to find one that does a good job generating these sorts of color palettes. 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.


Color palette reference

This is a list of all of the colors available when you import tailwindcss/colors into your tailwind.config.js file.

  1. // tailwind.config.js
  2. const colors = require('tailwindcss/colors')
  3. module.exports = {
  4. theme: {
  5. colors: {
  6. // Build your palette here
  7. transparent: 'transparent',
  8. current: 'currentColor',
  9. gray: colors.trueGray,
  10. red: colors.red,
  11. blue: colors.lightBlue,
  12. yellow: colors.amber,
  13. }
  14. }
  15. }

Although each color has a specific name, you’re encouraged to alias them however you like in your own projects.

Colors - 图2 Colors - 图3 Colors - 图4

←Breakpoints   Spacing→