Home > Blockchain >  When colors are set in tailwind.config.js the default tailwind colors won't work anymore
When colors are set in tailwind.config.js the default tailwind colors won't work anymore

Time:05-14

I am using Tailwind for a little Wordpress project I am creating. So I have set things up with webpack. This works absolutely fine. Tailwind does its tree shaking and only adds the classes into to the end File I actually used.

I have the following config to overwrite some of the default colors:

module.exports = {
  content: [
    './inc/blocks/*.php',
    './archive.php',
    './footer.php',
    './header.php',
    './index.php',
    './single.php',
  ],
  darkMode: "class",
  theme: {
    extend: {},
      colors: {
        'amber': {
          100: '#fff8e1',
          200: '#ffecb3',
          300: '#ffe082',
          400: '#F7CE64',
          500: '#deb95a',
          600: '#c69e56',
          700: '#af8c4c',
          800: '#977b3c',
          900: '#856a2c',
        },
        'lime': {
          100: '#afeacd',
          200: '#9be5c1',
          300: '#86dfb4',
          400: '#36CA82',
          500: '#2ba268',
          600: '#1f9357',
          700: '#197b4e',
          800: '#126a45',
          900: '#0e5a3c',
        },
        'rose': {
          100: '#fbe9e7',
          200: '#ffccc7',
          300: '#ffa7a7',
          400: '#C88986',
          500: '#B77C7C',
          600: '#8A575C',
          700: '#7C4C4C',
          800: '#6F4240',
          900: '#633C3C',
        },
      },
      fontFamily: {
        'title': ['Poppins', 'sans-serif'],
        'body': ['Merriweather', 'serif'],
      },
    },
    plugins: [],
}

This works absolutely fine. For example, if I use one of the colors to set a background on a div, this works.

<div ></div>

But if I now want to use default tailwind colors, which I have not changed the value for like the following, it won't work.

<div ></div>

As an experiment I tried removing the color configuration I have set in the tailwind config. And sure enough it worked again. Is this supposed to be like that? All default colors being deleted when you overwrite just some of the default colors?

I would like to be able to just define my own colors on top of the existing ones, but keep the ones I haven't overwritten, how would I accomplish this?

CodePudding user response:

If you want to preserve tailwind's default colors along with your custom defined ones, you need to move the color object inside the extend.

module.exports = {
  content: [
    './inc/blocks/*.php',
    './archive.php',
    './footer.php',
    './header.php',
    './index.php',
    './single.php',
  ],
  darkMode: 'class',
  theme: {
    extend: {
      colors: {
        amber: {
          100: '#fff8e1',
          200: '#ffecb3',
          300: '#ffe082',
          400: '#F7CE64',
          500: '#deb95a',
          600: '#c69e56',
          700: '#af8c4c',
          800: '#977b3c',
          900: '#856a2c',
        },
        lime: {
          100: '#afeacd',
          200: '#9be5c1',
          300: '#86dfb4',
          400: '#36CA82',
          500: '#2ba268',
          600: '#1f9357',
          700: '#197b4e',
          800: '#126a45',
          900: '#0e5a3c',
        },
        rose: {
          100: '#fbe9e7',
          200: '#ffccc7',
          300: '#ffa7a7',
          400: '#C88986',
          500: '#B77C7C',
          600: '#8A575C',
          700: '#7C4C4C',
          800: '#6F4240',
          900: '#633C3C',
        },
      },
    },
    fontFamily: {
      title: ['Poppins', 'sans-serif'],
      body: ['Merriweather', 'serif'],
    },
  },
  plugins: [],
}

Demo: https://play.tailwindcss.com/SeTdKxyNlM?file=config

Source: https://tailwindcss.com/docs/theme#extending-the-default-theme

  • Related