Skip to content

Commit 1235b40

Browse files
authored
Document new @variant behavior where you can stack & compounds variants (#2481)
Direct link: https://tailwindcss-com-git-feat-document-compound-9d8eca-tailwindlabs.vercel.app/docs/adding-custom-styles#using-variants This PR documents the new `@variant` behavior that was introduced in tailwindlabs/tailwindcss#19996 where you can stack variants (like you would in normal HTML): ```css .my-element { background: white; @variant hover:focus { background: black; } } ``` Which compiles to: ```css .my-element { background: white; &:hover { @media (hover: hover) { &:focus { background: black; } } } } ``` Similarly, this adds docs for compound variants, where you can define the same CSS in different situations: ```css .my-element { background: white; @variant hover, focus { background: black; } } ``` Which compiles to: ```css .my-element { background: white; &:hover { @media (hover: hover) { background: black; } } &:focus { background: black; } } ``` <img width="709" height="1251" alt="image" src="https://github.com/user-attachments/assets/46089844-06c9-4640-ab3b-a05d69bea8f9" />
1 parent 36c64db commit 1235b40

1 file changed

Lines changed: 45 additions & 10 deletions

File tree

src/docs/adding-custom-styles.mdx

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ Use the `@variant` directive to apply a Tailwind variant within custom CSS:
328328

329329
</CodeExampleStack>
330330

331-
If you need to apply multiple variants at the same time, use nesting:
331+
If you need to apply multiple variants at the same time, stack them using the same syntax you would in HTML:
332332

333333
<CodeExampleStack>
334334

@@ -337,11 +337,9 @@ If you need to apply multiple variants at the same time, use nesting:
337337
.my-element {
338338
background: white;
339339

340-
/* [!code highlight:6] */
341-
@variant dark {
342-
@variant hover {
343-
background: black;
344-
}
340+
/* [!code highlight:4] */
341+
@variant hover:focus {
342+
background: black;
345343
}
346344
}
347345
```
@@ -351,10 +349,10 @@ If you need to apply multiple variants at the same time, use nesting:
351349
.my-element {
352350
background: white;
353351

354-
/* [!code highlight:7] */
355-
@media (prefers-color-scheme: dark) {
356-
&:hover {
357-
@media (hover: hover) {
352+
/* [!code highlight:8] */
353+
&:hover {
354+
@media (hover: hover) {
355+
&:focus {
358356
background: black;
359357
}
360358
}
@@ -364,6 +362,43 @@ If you need to apply multiple variants at the same time, use nesting:
364362

365363
</CodeExampleStack>
366364

365+
To apply the same styles for multiple variants, separate each variant with a comma:
366+
367+
<CodeExampleStack>
368+
369+
```css
370+
/* [!code filename:app.css] */
371+
.my-element {
372+
background: white;
373+
374+
/* [!code highlight:4] */
375+
@variant hover, focus {
376+
background: black;
377+
}
378+
}
379+
```
380+
381+
```css
382+
/* [!code filename:Compiled CSS] */
383+
.my-element {
384+
background: white;
385+
386+
/* [!code highlight:6] */
387+
&:hover {
388+
@media (hover: hover) {
389+
background: black;
390+
}
391+
}
392+
393+
/* [!code highlight:4] */
394+
&:focus {
395+
background: black;
396+
}
397+
}
398+
```
399+
400+
</CodeExampleStack>
401+
367402
## Adding custom utilities
368403

369404
### Simple utilities

0 commit comments

Comments
 (0)