Skip to content

After V4 upgrade I am confused on how to use @apply with custom css #17443

Answered by wongjn
newbelaravel asked this question in Help
Discussion options

You must be logged in to vote

You'd need to register those class names with @utility so that Tailwind knows about them:

@utility bg-size-200 {
  …
}

@utility bg-pos-0 {
  …
}

@utility bg-pos-100 {
  &:hover {
    …
  }
}

Alternatively, you could forgo them altogether:

@utility btn-calculate {
  …
  background-size: …;
  background-position: …;
  
  &:hover {
    background-position: …;
  }
}

Or better yet, use your templating system:

-<button class="btn-calculate">
+<CalculateButton>
<button class="… bg-[size:200%_200%] bg-left-top hover:bg-right-bottom">
  {{ content }}
</button>

As an aside, Adam Wathan (creator of Tailwind) does seem to advocate avoiding @apply:

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by newbelaravel
Comment options

You must be logged in to vote
1 reply
@wongjn
Comment options

wongjn Mar 29, 2025
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants