Using the @apply directive to create custom classes that inherit styles from other classes
In this example, we're creating a custom class called .custom-button
that inherits the styles from .bg-orange-500
and .hover:bg-orange-700
. This allows us to reuse the styles from these classes without having to rewrite them.
/* Create a custom class called 'custom-button' that inherits styles from 'bg-orange-500' and 'hover:bg-orange-700' */
.custom-button {
@apply bg-orange-500 hover:bg-orange-700;
}
Why it's useful: This trick is useful when you want to create a custom class that builds upon existing classes, without having to duplicate code. It's also helpful when you want to create a consistent design language throughout your application.
How it works: The @apply
directive tells Tailwind to apply the styles from the specified classes to the current class. In this case, .custom-button
will inherit the background color and hover styles from .bg-orange-500
and .hover:bg-orange-700
, respectively.