How to Append New Colors in Tailwind CSS Without Removing Originals

Avatar

By squashlabs, Last Updated: Oct. 6, 2023

How to Append New Colors in Tailwind CSS Without Removing Originals

To append new colors in Tailwind CSS without removing the original colors, you can follow these steps:

Step 1: Create a Custom Configuration File

1. Create a new file in your project's root directory called tailwind.config.js.

2. Open the tailwind.config.js file in a text editor.

Related Article: How To Troubleshoot CSS Position Sticky Not Working

Step 2: Add Your Custom Colors

3. Inside the tailwind.config.js file, find the theme object.

4. Inside the theme object, add a new property called extend.

5. Inside the extend object, add a new property called colors.

6. Inside the colors object, add your custom color definitions.

Here is an example of how you can add custom colors:

module.exports = {
  theme: {
    extend: {
      colors: {
        'custom-blue': '#1E40AF',
        'custom-red': '#FF0000',
      },
    },
  },
  variants: {},
  plugins: [],
}

In the example above, we added two custom colors: custom-blue and custom-red. You can define your own custom colors using any valid CSS color value.

Step 3: Use Your Custom Colors

7. Save the tailwind.config.js file.

8. In your HTML or CSS, you can now use your custom colors by referencing their respective classes:

<div class="bg-custom-blue"></div>
<div class="text-custom-red"></div>

In the example above, we used the bg-custom-blue class to set the background color of a <div> element to our custom blue color, and the text-custom-red class to set the text color to our custom red color.

Note: By default, Tailwind CSS generates utility classes only for the colors defined in the default color palette. With the custom configuration, you can extend the color palette with your own custom colors.

Alternative Approach: Manually Update the CSS File

If you don't want to create a custom configuration file, you can also manually update the CSS file generated by Tailwind CSS.

1. Locate the generated CSS file in your project. By default, it is named styles.css.

2. Open the styles.css file in a text editor.

3. Search for the section related to colors.

4. Add your custom colors directly in the CSS file.

Here is an example of how you can add custom colors directly in the CSS file:

/* Custom Colors */
.custom-blue {
  --tw-bg-opacity: 1;
  background-color: rgba(30, 64, 175, var(--tw-bg-opacity));
}
.custom-red {
  --tw-text-opacity: 1;
  color: rgba(255, 0, 0, var(--tw-text-opacity));
}

In the example above, we added the custom-blue and custom-red classes with their respective color values. You can define your own custom colors using any valid CSS color value.

Related Article: How to Implement an Onclick Effect in CSS

Best Practices

When appending new colors in Tailwind CSS without removing the original colors, consider the following best practices:

1. Choose meaningful names for your custom colors to make your code more readable and maintainable.

2. Consider using a consistent naming convention for your custom colors to improve consistency and organization.

3. Avoid overriding existing Tailwind CSS color classes to prevent conflicts and maintain compatibility with future updates.

Now you know how to append new colors in Tailwind CSS without removing the originals. Whether you choose to create a custom configuration file or manually update the CSS file, you can easily extend the color palette and customize your project's colors to suit your needs.

More Articles from the Cascading Style Sheets (CSS) Guide series:

How to Create a Text Border Using CSS

This guide provides step-by-step instructions on how to create a text border using CSS for web design. Learn two methods: using the text-stroke prope… read more

How to Center a Position Absolute Element in CSS

Centering an element with position absolute in CSS can be achieved using different techniques. Two commonly used methods are using the Transform Prop… read more

How to Apply Outline Effect to Text in CSS

Adding an outline effect to text in CSS is a simple process that can enhance the visual appeal of your website. This article provides a step-by-step … read more

How to Set Distance Between Flexbox Items in CSS

Setting the distance between Flexbox items in CSS is made easy with two methods: using the gap property or using margin or padding. This article prov… read more

CSS Position Relative: A Complete Guide

This tutorial is a comprehensive resource that dives deep into the fundamentals, practical examples, real-world applications, and advanced techniques… read more

How to Implement Gradient Borders using CSS

Implement gradient borders using CSS with ease by following this guide. Learn how to use the border-image property and the linear-gradient() function… read more

How to Center Elements Horizontally and Vertically Using Flexbox

This article provides a step-by-step guide on using CSS Flexbox to center elements both horizontally and vertically. Learn how to achieve center alig… read more

How to Fix CSS Text Overflow Ellipsis Issue

Resolving CSS text-overflow ellipsis issues can be frustrating, but it doesn't have to be. This article provides a simple guide to fix this problem b… read more

CSS Padding: An Advanced Guide

Dive into advanced techniques for perfect spacing and visually appealing layouts with CSS padding. This comprehensive guide covers lesser-known topic… read more

How to Horizontally Center a Div in CSS

Aligning a div to the middle width of a webpage using CSS can be achieved in a few different ways. One method is to use the "margin: auto;" property,… read more