2
<script>
export default {
  name: "TestTailwind",
};
</script>
<script setup></script>

<template>
  <div class="">
    <div v-for="i in 20" :key="i" :class="[`m-${i}`]">m-{{ i }}</div>
    <div v-for="i in 20" :key="i" :class="[`p-${i}`]">p-{{ i }}</div>

  </div>
</template>

<style lang="scss" scoped>
</style>

I try to use dynamic class , but tailwind not give me the class right.

enter image description here

Expect:

enter image description here

radiorz
  • 1,459
  • 4
  • 18
  • 36
  • Does this answer your question? [React and Tailwind CSS: dynamically generated classes are not being applied](https://stackoverflow.com/questions/71063619/react-and-tailwind-css-dynamically-generated-classes-are-not-being-applied) – Ihar Aliakseyenka May 24 '22 at 06:29

1 Answers1

2

It's because of tailwind tree shaking. You need to add the classes you want to generate dynamically to the safelist in your tailwind.config file because tailwind can't detect used classes with this dinamic syntax.

Here is the code:

module.exports = {
  content: [
    './pages/**/*.{html,js}',
    './components/**/*.{html,js}',
  ],
  safelist: [
    'p-1',
    'p-2',
    'p-3',
    // ...
  ],
  // ...
}

You can read the docs here

mo3n
  • 1,522
  • 2
  • 10
  • 34