question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom utility: @apply can only be used for classes in the same CSS tree.

See original GitHub issue

Version

@nuxtjs/tailwindcss: v3.0.0 nuxt: 2.14.1

Reproduction Link

https://codesandbox.io/s/tailwindcss-issue-1jf8w

Steps to reproduce

Document: https://tailwindcss.com/docs/adding-new-utilities

In tailwind.pcss, I create a custom class like:

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

@responsive {
  .global-class { 
    @apply text-blue-400
  }
}

The .global-class or sm:global-class can be use in <template>. But it can’t be use in <style lang="postcss"> with @apply

<style lang="postcss">
.component-class {
  @apply text-blue-400; /* work */
}
.apply-component-class {
  @apply component-class; /* work */
}
.apply-global-class {
  @apply global-class; /* doesn't work, throw build error */
}
</style>

What is Expected?

@apply global-class; will be work

What is actually happening?

@apply global-class; throw build error

@apply can only be used for classes in the same CSS tree.

Module build failed (from ./node_modules/postcss-loader/src/index.js): SyntaxError (26:3) `@apply` cannot be used with `.global-class` because `.global-class` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that `.global-class` exists, make sure that any `@import` statements are being properly processed *before* Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.

image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:24

github_iconTop GitHub Comments

15reactions
peoraycommented, Sep 7, 2020

Check your tailwind.config.js file and ensure you’re properly nesting your object properties. I had the same issue, turns out I was missing the closing } for my theme property.

11reactions
michaelpumocommented, May 17, 2021

If anyone is getting an error in Vue SFC’s PostCSS blocks with custom utilities not ‘existing’, hopefully this will help:

This did NOT work (as a rule in a CSS file):

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

@layer utilities {
  .container {
    width: 100%;
    max-width: 1500px;
  }
}

This DOES work (created as a plugin inside tailwind.config.js):

const plugin = require('tailwindcss/plugin')
const pxToRem = (px, base = 16) => `${px / base}rem`

module.exports = {
  purge: [
    './components/**/*.{vue,js}',
    './layouts/**/*.vue',
    './pages/**/*.vue',
    './plugins/**/*.{js,ts}',
    './nuxt.config.{js,ts}',
  ],
  plugins: [
    plugin(function ({ addUtilities, theme }) {
      const customUtilities = {
        '.container': {
          width: '100%',
          maxWidth: '1500px',
        },
      }
      addUtilities(customUtilities)
    }),
  ],
}

Now I can do this in the SFC CSS (notice, you do not put a dot next to the custom class):

<style lang="postcss" scoped>
.Header {
  @apply container;
}
</style>

I hope this helps someone who is struggling to add custom utility classes to a Vue.js or Nuxt.js project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

apply can only be used for classes in the same CSS tree. ...
Custom utility : @apply can only be used for classes in the same CSS tree. ... Document: https://tailwindcss.com/docs/adding-new-utilities.
Read more >
tailwind: how to use @apply for custom class in nuxt2?
Fine, it seems to be a bug in nuxtjs/tailwindcss: Custom utility: @apply can only be used for classes in the same CSS tree.,...
Read more >
Adding Custom Styles
Any custom styles you add to the base , components , or utilities layers will only be included in your compiled CSS if...
Read more >
Tailwindcss SCSS error @apply class does not exist issue
I'm using SCSS in my project and all of the .scss files that I use the tailwind @apply in ... as `@apply` can...
Read more >
the `@apply` class does not exist. if `@apply` is a custom ...
Using @apply in a Vue style block throws "class does not exist" error . ... sees your CSS, as `@apply` can only be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found