Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tailwind CSS not adding translate styling

I’ve got a <div> which is toggling between translate classes like so:

<div class={`w-96 fixed top-16 bottom-0 left-0 transition-transform -translate-x-${show ? '0' : '96'}`}>

And Tailwind is just not applying this style, and I don’t understand why. The class is added to the element, but not the styling.

class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The styling for all the other classes is added, except for -translate-x-96.

This is my app.css:

@tailwind base;
@tailwind components;
@tailwind utilities;

>Solution :

Tailwind v3 generates styles for classes it finds by scanning the files specified in content field in tailwind.config.js. This means that the classes should be present in the code as-is and should not be constructed by concatenation.

From https://tailwindcss.com/docs/content-configuration#class-detection-in-depth:

The way Tailwind scans your source code for classes is intentionally very simple — we don’t actually parse or execute any of your code in the language it’s written in, we just use regular expressions to extract every string that could possibly be a class name.

The fix in this case is to specify the full class name in both branches of the ternary operator:

`${show ? '-translate-x-0' : '-translate-x-96'}`
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading