×

Please try us on a desktop

Our app relies on some desktop browser features to render design files and interactable code. Unfortunately, we haven't been able to make these work on mobile yet.

Please try Kombai using a chromium based browser on desktop.

Tailwind

How do colors work in Tailwind CSS and how to customize them?

Published on: May 17, 2023

Tailwind CSS is an innovative, utility-first CSS framework that simplifies and accelerates frontend development by offering a wide array of tools and customization options. Among its many powerful features, Tailwind CSS excels in providing an extensive color management system, allowing developers to effortlessly create and implement color schemes in their projects.

In this comprehensive guide, we will delve into the world of Tailwind CSS colors, examining their functionality, the default palette, customization opportunities, and diverse use cases.

How colors work in Tailwind?

Tailwind CSS uses a systematic approach to color management, providing a predefined set of color palettes based on shades of gray and named colors, such as red, yellow, blue, and more. Each color is available in various shades, numbered from 100 (lightest) to 900 (darkest), giving developers ample flexibility to create a visually appealing and consistent user interface.

To apply a color to an element, simply use the corresponding utility class. For instance, if you want to change the text color to red with a shade of 500, you would use the following class:

<p class="text-red-500">This is a red text.</p>

Default Color Palette of Tailwind

Tailwind CSS comes with a comprehensive default color palette that includes:

  • Grayscale: A range of gray shades from 100 (lightest) to 900 (darkest).

  • Named colors: A set of predefined colors such as red, yellow, green, blue, indigo, purple, and pink, available in shades from 100 to 900.

  • Transparent and white colors: transparent and white keywords can be used to apply transparent and white backgrounds, respectively.

The default color palette is designed to cover a broad range of use cases, making it easy for developers to create aesthetically pleasing designs.

However, if you need to further customize your color scheme, Tailwind CSS provides a simple way to extend or replace the default palette.

Customizing Tailwind CSS Colors

Tailwind CSS allows you to customize colors in two ways: extending the default palette or replacing it entirely. To extend the default palette, simply add new colors or modify existing ones in your configuration file.

For example, to add a custom orange color, you would include the following in your Tailwind CSS configuration:

module.exports = {
  theme: {
    extend: {
      backgroundColor: {
        orange: {
          100: '#FFFAF0',
          200: '#FEEBC8',
          300: '#FCD34D',
          400: '#FBBF24',
          500: '#F59E0B',
          600: '#D97706',
          700: '#B45309',
          800: '#92400E',
          900: '#78350F',
        },
      },
    },
  },
  variants: {},
  plugins: [],
};

To replace the entire color palette, define your custom colors directly under the theme object:

module.exports = {
  theme: {
    backgroundColor: {
      gray: {
        100: '#F5F5F5',
        200: '#E5E5E5',
        300: '#D4D4D4',
        400: '#A3A3A3',
        500: '#737373',
        600: '#525252',
        700: '#404040',
        800: '#262626',
        900: '#171717',
      },
      // Add other colors as needed
    },
  },
  variants: {},
  plugins: [],
};

Keep in mind that when replacing the entire color palette, you will lose access to the default colors unless you explicitly include them in your custom configuration.

Using Colors for Backgrounds, Text, and Borders

Tailwind CSS offers utility classes for applying colors to various aspects of an element. Here are some common use cases:

  • Background color: Use the bg-{color}-{shade} class to set the background color of an element. For example:
<div class="bg-blue-500">This div has a blue background.</div>

Text color: Use the text-{color}-{shade} class to set the text color of an element. For example:

<p class="text-green-700">This text is green.</p>
  • Border color: Use the border-{color}-{shade} class to set the border color of an element. For example:
<div class="border border-red-300">This div has a red border.</div>

Colors in Responsive Design and Hover States

Tailwind CSS also allows you to apply color classes conditionally based on screen size or user interactions. Here are a few examples:

  • Responsive design: Use the {screen}: prefix to apply a color class based on the screen size. For example:
<div class="bg-red-500 sm:bg-green-500 md:bg-blue-500 lg:bg-yellow-500">
  This div's background color changes based on screen size.
</div>
  • Hover states: Use the hover prefix to apply a color class when the user hovers over an element. For example:
<button class="bg-blue-500 hover:bg-blue-700">
  This button's background color changes on hover.
</button>

Tips for Creating Color Schemes

When creating custom color schemes for your project, consider the following tips to ensure visual appeal and consistency:

  • Stick to a limited color palette: Limiting your color palette to a few primary colors and their shades helps maintain consistency and visual harmony.

  • Use shades for depth and contrast: Utilize different shades of the same color for various elements, such as buttons, cards, or backgrounds, to create depth and contrast.

  • Consider accessibility: Ensure that your color choices provide sufficient contrast for text and interactive elements to meet accessibility standards, such as the Web Content Accessibility Guidelines (WCAG).

  • Test your color scheme on various devices and screen sizes: Make sure your color choices work well on different devices and screen sizes, as colors may appear differently depending on display characteristics.

Wrapping Up

In conclusion, Tailwind CSS colors provide an efficient and adaptable way to manage and apply color schemes in your web projects, allowing developers to create unique and visually appealing designs.

When designing your color schemes, remember to keep consistency in mind. It's also important to stay up-to-date with the latest trends and best practices in web design. By continuously learning and adapting your color choices, your projects will stay fresh and engaging, offering a great user experience.

With Tailwind CSS colors at your fingertips, the opportunities for creating amazing web designs are practically endless.

Happy Coding!