Skip to content

Commit 3406ca0

Browse files
Document the important option (#2030)
This PR adds documentation for the `important` option on the `@import "tailwindcss";` at-rule. Remaining questions: - [ ] Is this the correct spot to document this? (We redirect `/docs/configuration` to `/docs/theme`, so not 100% sure) - [ ] Should we document a solution for the `#app` selector strategy that is mentioned in the v3 docs? Fixes: #2028 --------- Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
1 parent 69bcdb0 commit 3406ca0

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/docs/styling-with-utility-classes.mdx

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,3 +894,102 @@ Here's what a `btn-primary` class might look like, using [theme variables](/docs
894894
</Figure>
895895

896896
Again though, for anything that's more complicated than just a single HTML element, we highly recommend using template partials so the styles and structure can be encapsulated in one place.
897+
898+
## Managing style conflicts
899+
900+
{/* All of the styles in Tailwind have fairly low specificity which can lead to style conflicts */}
901+
902+
### Conflicting utility classes
903+
904+
When you add two classes that target the same CSS property, the class that appears later in the stylesheet wins. So in this example, the element will receive `display: grid` even though `flex` comes last in the actual `class` attribute:
905+
906+
<CodeExampleStack>
907+
908+
```html
909+
<!-- [!code filename:HTML] -->
910+
<!-- prettier-ignore -->
911+
<div class="grid flex">
912+
<!-- ... -->
913+
</div>
914+
```
915+
916+
```css
917+
/* [!code filename: CSS] */
918+
.flex {
919+
display: flex;
920+
}
921+
.grid {
922+
display: grid;
923+
}
924+
```
925+
926+
</CodeExampleStack>
927+
928+
In general, you should just never add two conflicting classes to the same element — only ever add the one you actually want to take effect:
929+
930+
```jsx
931+
// [!code word:gridLayout\ \?\ \"grid\"\ \:\ \"flex\"]
932+
function Example({ gridLayout }) {
933+
return <div className={gridLayout ? "grid" : "flex"}>{/* ... */}</div>;
934+
}
935+
```
936+
937+
Using component-based libraries like React or Vue, this often means exposing specific props for styling customizations instead of letting consumers add extra classes from outside of a component, since those styles will often conflict.
938+
939+
### Using the important modifier
940+
941+
When you really need to force a specific utility class to take effect and have no other means of managing the specificity, you can add `!` to the end of the class name to make all of the declarations `!important`:
942+
943+
<CodeExampleStack>
944+
945+
```html
946+
<!-- [!code filename:HTML] -->
947+
<!-- [!code classes:bg-red-500!] -->
948+
<!-- prettier-ignore -->
949+
<div class="bg-teal-500 bg-red-500!">
950+
<!-- ... -->
951+
</div>
952+
```
953+
954+
```css
955+
/* [!code filename: Generated CSS] */
956+
/* [!code word:!important] */
957+
.bg-red-500\! {
958+
background-color: var(--color-red-500) !important;
959+
}
960+
.bg-teal-500 {
961+
background-color: var(--color-teal-500);
962+
}
963+
```
964+
965+
</CodeExampleStack>
966+
967+
### Using the important flag
968+
969+
If you're adding Tailwind to a project that has existing complex CSS with high specificity rules, you can use the `important` flag when importing Tailwind to mark _all_ utilities as `!important`:
970+
971+
<CodeExampleStack>
972+
973+
```css
974+
/* [!code filename:app.css] */
975+
/* [!code word:important] */
976+
@import "tailwindcss" important;
977+
```
978+
979+
```css
980+
/* [!code filename:Compiled CSS] */
981+
/* [!code word:!important] */
982+
@layer utilities {
983+
.flex {
984+
display: flex !important;
985+
}
986+
.gap-4 {
987+
gap: 1rem !important;
988+
}
989+
.underline {
990+
text-decoration-line: underline !important;
991+
}
992+
}
993+
```
994+
995+
</CodeExampleStack>

src/docs/theme.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CodeExampleStack } from "@/components/code-example";
12
import { TipGood, TipBad, TipInfo } from "@/components/tips";
23
import { Iframe } from "@/components/iframe";
34
import { Example } from "@/components/example";

0 commit comments

Comments
 (0)