In my global css file where I import:
@tailwind base;
@tailwind components;
@tailwind utilities;
I moved this piece of code below body{...} and everything works now
body {
font-family: "DIN Alternate", sans-serif;
font-size: 16px;
}
@tailwind base;
@tailwind components;
@tailwind utilities;
To the first thing you should check if the same error occured is how you import tailwind css
I moved body to the bottom since fonts didn't work and it WORKED. Weird problem. I think tailwind just needed some refresh in styles UPD:
@tailwind base;
@tailwind components;
@tailwind utilities;
body {
font-family: "DIN Alternate", sans-serif;
font-size: 16px;
}
Answer from Rostyk on Stack OverflowIn my global css file where I import:
@tailwind base;
@tailwind components;
@tailwind utilities;
I moved this piece of code below body{...} and everything works now
body {
font-family: "DIN Alternate", sans-serif;
font-size: 16px;
}
@tailwind base;
@tailwind components;
@tailwind utilities;
To the first thing you should check if the same error occured is how you import tailwind css
I moved body to the bottom since fonts didn't work and it WORKED. Weird problem. I think tailwind just needed some refresh in styles UPD:
@tailwind base;
@tailwind components;
@tailwind utilities;
body {
font-family: "DIN Alternate", sans-serif;
font-size: 16px;
}
"PurgeCSS" may be the problem
(you haven't provided usage in the code)
when classes are created dynamically (interpolated), optimizer will simply not export them
force optimizer to include them always
with exact value:
// tailwind.config.js
module.exports = {
// ...
safelist: [
'brand-red', // Add more classes as needed
// Patterns are also supported
'brand-*', // Safelists all brand color utilities
],
// ...
}
or with regex pattern:
// tailwind.config.js
module.exports = {
// ...
safelist: [
{
pattern: /brand-(green|amber|red|grey)-[0-9]{3}/,
},
],
// ...
}
Videos
I'm trying to create a sidebar. I am using a template I found and am trying to change the color from indigo to cyan. When I try to change the code to be
bg-cyan-700
I get the grayed out photo whereas the indigo shows indigo background. How could this be happening? I inspected the element and it shows cyan but the front end doesn't reflect the change...
How could this be happening?
<aside
className={classNames({
// "bg-cyan-700 text-zinc-50 fixed md:static md:translate-x-0 z-20": true, // want to change to cyan
// ORIGINAL COLOR
"bg-indigo-700 text-zinc-50 fixed md:static md:translate-x-0 z-20":
true,
"transition-all duration-300 ease-in-out": true, // makes the transition really nice and smooth
"w-[200px]": !collapsed,
"w-16": collapsed,
"-translate-x-full": !shown, //not super sure what this does
})}
>
As the title says whenever I set the bg to any colour inside any button it just doesn't work.
in Tailwind you can't use dynamic class naming like bg-${color}.
This because when Tailwind compiles its CSS, it looks up over all of your code and checks if a class name matches.
If you want dynamic name classes you should write all the class name.
But for your specific use case, I would not use the JIT of Tailwind and instead use the style attribute and dynamically change the backgroundColor value.
It will use less CSS and also give you less headache.
Finally, this is my suggestion
const colors = ['#7a5195', '#bc5090','#ef5675'];
export default function App() {
const names = ['Tyler', "Charles", 'Vince']
const labels = {};
names.forEach((name, index) => {
labels[name] = colors[index];
});
return (
<>
{
names.map((name) => (
<div style={{ backgroundColor: `${labels[name]}` }}>
{name}
</div>
)
}
</>
);
}
You can also add classes to your safelist in your tailwind config.
// tailwind.config.js
// Create an array for all of the colors you want to use
const colorClasses = [
'#7a5195',
'#bc5090',
'#ef5675'
];
module.exports = {
purge: {
content: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"],
// Map over the labels and add them to the safelist
safelist: [
...colorClasses.map((color) => `bg-${color}`)
],
},
darkMode: false, // or 'media' or 'class'
variants: {
extend: {},
},
plugins: [require("@tailwindcss/forms")],
}
This way you can use the colors that were included in the colorClasses array dynamically as they will not be purged.
Note: If you want to do bg-blue-500 for example, you'll need to include all of the color weights as part of the safelist (as well as add that color to the array).
...colorClasses.map((color) => `bg-${color}-500`)