Dev Overflow Logo

Dev Overflow

Global search

Search across questions, answers, users and tags.

Loading...
save

Tailwind v4: how do I define reusable custom utilities?

clock icon

asked 1 month ago

message icon

1

eye icon

912

In v3 I had a tailwind.config.js with a plugin that added things like .flex-center. v4 dropped the JS config in favour of CSS. Where do custom utilities go now?

1 Answer

Use the @utility directive directly in your CSS:

1@utility flex-center {
2 @apply flex justify-center items-center;
3}
4
5@utility background-light900_dark200 {
6 @apply bg-light-900 dark:bg-dark-200;
7}
1@utility flex-center {
2 @apply flex justify-center items-center;
3}
4
5@utility background-light900_dark200 {
6 @apply bg-light-900 dark:bg-dark-200;
7}

Utilities declared this way work with every variant, so hover:flex-center and md:background-light900_dark200 both compile. Design tokens go in @theme:

1@theme {
2 --color-primary-500: #ff7000;
3}
1@theme {
2 --color-primary-500: #ff7000;
3}

1

of 1

Write your answer here

Introduce the problem and expand on what you've put in the title.

Top Questions