🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 59 (from laksa071)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
✅
CRAWLED
5 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.2 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://tailwindcss.com/docs/adding-custom-styles
Last Crawled2026-04-16 00:41:10 (5 days ago)
First Indexed2021-12-09 18:35:37 (4 years ago)
HTTP Status Code200
Meta TitleAdding custom styles - Core concepts - Tailwind CSS
Meta DescriptionBest practices for adding your own custom styles in Tailwind projects.
Meta Canonicalnull
Boilerpipe Text
Best practices for adding your own custom styles in Tailwind projects. Often the biggest challenge when working with a framework is figuring out what you’re supposed to do when there’s something you need that the framework doesn’t handle for you. Tailwind has been designed from the ground up to be extensible and customizable, so that no matter what you’re building you never feel like you’re fighting the framework. This guide covers topics like customizing your design tokens, how to break out of those constraints when necessary, adding your own custom CSS, and extending the framework with plugins. Customizing your theme If you want to change things like your color palette, spacing scale, typography scale, or breakpoints, add your customizations using the @theme directive in your CSS: CSS @theme { --font-display : "Satoshi" , "sans-serif" ; --breakpoint-3xl : 120 rem ; --color-avocado-100 : oklch ( 0.99 0 0 ); --color-avocado-200 : oklch ( 0.98 0.04 113.22 ); --color-avocado-300 : oklch ( 0.94 0.11 115.03 ); --color-avocado-400 : oklch ( 0.92 0.19 114.08 ); --color-avocado-500 : oklch ( 0.84 0.18 117.33 ); --color-avocado-600 : oklch ( 0.53 0.12 118.34 ); --ease-fluid : cubic-bezier ( 0.3 , 0 , 0 , 1 ); --ease-snappy : cubic-bezier ( 0.2 , 0 , 0 , 1 ); /* ... */ } Learn more about customizing your theme in the theme variables documentation . Using arbitrary values While you can usually build the bulk of a well-crafted design using a constrained set of design tokens, once in a while you need to break out of those constraints to get things pixel-perfect. When you find yourself really needing something like top: 117px to get a background image in just the right spot, use Tailwind's square bracket notation to generate a class on the fly with any arbitrary value: HTML < div class = " top-[117px] " > <!-- ... --> </ div > This is basically like inline styles, with the major benefit that you can combine it with interactive modifiers like hover and responsive modifiers like lg : HTML < div class = " top-[117px] lg:top-[344px] " > <!-- ... --> </ div > This works for everything in the framework, including things like background colors, font sizes, pseudo-element content, and more: HTML < div class = " bg-[#bada55] text-[22px] before:content-['Festivus'] " > <!-- ... --> </ div > If you're referencing a CSS variable as an arbitrary value, you can use the custom property syntax: HTML < div class = " fill-(--my-brand-color) ..." > <!-- ... --> </ div > This is just a shorthand for fill-[var(--my-brand-color)] that adds the var() function for you automatically. Arbitrary properties If you ever need to use a CSS property that Tailwind doesn't include a utility for out of the box, you can also use square bracket notation to write completely arbitrary CSS: HTML < div class = " [mask-type:luminance] " > <!-- ... --> </ div > This is really like inline styles, but again with the benefit that you can use modifiers: HTML < div class = " [mask-type:luminance] hover:[mask-type:alpha] " > <!-- ... --> </ div > This can be useful for things like CSS variables as well, especially when they need to change under different conditions: HTML < div class = " [--scroll-offset:56px] lg:[--scroll-offset:44px] " > <!-- ... --> </ div > Arbitrary variants Arbitrary variants are like arbitrary values but for doing on-the-fly selector modification, like you can with built-in pseudo-class variants like hover:{utility} or responsive variants like md:{utility} but using square bracket notation directly in your HTML. HTML < ul role = "list" > {#each items as item} < li class = "lg: [&:nth-child(-n+3)] :hover:underline" > {item} </ li > {/each} </ ul > Learn more in the arbitrary variants documentation. Handling whitespace When an arbitrary value needs to contain a space, use an underscore ( _ ) instead and Tailwind will automatically convert it to a space at build-time: HTML < div class = "grid grid-cols-[1fr_500px_2fr] " > <!-- ... --> </ div > In situations where underscores are common but spaces are invalid, Tailwind will preserve the underscore instead of converting it to a space, for example in URLs: HTML < div class = " bg-[url('/what_a_rush.png')] " > <!-- ... --> </ div > In the rare case that you actually need to use an underscore but it's ambiguous because a space is valid as well, escape the underscore with a backslash and Tailwind won't convert it to a space: HTML < div class = " before:content-['hello\_world'] " > <!-- ... --> </ div > If you're using something like JSX where the backslash is stripped from the rendered HTML, use String.raw() so the backslash isn't treated as a JavaScript escape character: < div className ={ String . raw `before:content-['hello \_ world']` }> <!-- ... --> </ div > Resolving ambiguities Many utilities in Tailwind share a common namespace but map to different CSS properties. For example text-lg and text-black both share the text- namespace, but one is for font-size and the other is for color . When using arbitrary values, Tailwind can generally handle this ambiguity automatically based on the value you pass in: HTML <!-- Will generate a font-size utility --> < div class = " text-[22px] " > ... </ div > <!-- Will generate a color utility --> < div class = " text-[#bada55] " > ... </ div > Sometimes it really is ambiguous though, for example when using CSS variables: HTML < div class = " text-(--my-var) " > ... </ div > In these situations, you can "hint" the underlying type to Tailwind by adding a CSS data type before the value: HTML <!-- Will generate a font-size utility --> < div class = " text-(length:--my-var) " > ... </ div > <!-- Will generate a color utility --> < div class = " text-(color:--my-var) " > ... </ div > Using custom CSS While Tailwind is designed to handle the bulk of your styling needs, there is nothing stopping you from just writing plain CSS when you need to: CSS @import "tailwindcss" ; .my-custom-style { /* ... */ } Adding base styles If you just want to set some defaults for the page (like the text color, background color, or font family), the easiest option is just adding some classes to the html or body elements: HTML <! doctype html > < html lang = "en" class = " bg-gray-100 font-serif text-gray-900 " > <!-- ... --> </ html > This keeps your base styling decisions in your markup alongside all of your other styles, instead of hiding them in a separate file. If you want to add your own default base styles for specific HTML elements, use the @layer directive to add those styles to Tailwind's base layer: CSS @layer base { h1 { font-size : var ( --text-2xl ); } h2 { font-size : var ( --text-xl ); } } Adding component classes Use the components layer for any more complicated classes you want to add to your project that you'd still like to be able to override with utility classes. Traditionally these would be classes like card , btn , badge — that kind of thing. CSS @layer components { .card { background-color : var ( --color-white ); border-radius : var ( --radius-lg ); padding : --spacing(6 ); box-shadow : var ( --shadow-xl ); } } By defining component classes in the components layer, you can still use utility classes to override them when necessary: HTML <!-- Will look like a card, but with square corners --> < div class = " card rounded-none " > <!-- ... --> </ div > Using Tailwind you probably don't need these types of classes as often as you think. Read our guide on managing duplication for our recommendations. The components layer is also a good place to put custom styles for any third-party components you're using: CSS @layer components { .select2-dropdown { /* ... */ } } Using variants Use the @variant directive to apply a Tailwind variant within custom CSS: app.css .my-element { background : white ; @variant dark { background: black; } } Compiled CSS .my-element { background : white ; @ media ( prefers-color-scheme : dark ) { background : black ; } } If you need to apply multiple variants at the same time, use nesting: app.css .my-element { background : white ; @variant dark { @variant hover { background: black; } } } Compiled CSS .my-element { background : white ; @ media ( prefers-color-scheme : dark ) { &: hover { @media (hover: hover ) { background : black ; } } } } Adding custom utilities Simple utilities In addition to using the utilities that ship with Tailwind, you can also add your own custom utilities. This can be useful when there's a CSS feature you'd like to use in your project that Tailwind doesn't include utilities for out of the box. Use the @utility directive to add a custom utility to your project: CSS @utility content-auto { content-visibility: auto; } You can now use this utility in your HTML: HTML < div class = " content-auto " > <!-- ... --> </ div > It will also work with variants like hover , focus and lg : HTML < div class = " hover:content-auto " > <!-- ... --> </ div > Custom utilities are automatically inserted into the utilities layer along with all of the built-in utilities in the framework. Complex utilities If your custom utility is more complex than a single class name, use nesting to define the utility: CSS @utility scrollbar-hidden { &::-webkit-scrollbar { display : none ; } } Functional utilities In addition to registering simple utilities with the @utility directive, you can also register functional utilities that accept an argument: CSS @utility tab-* { tab-size: --value(--tab-size-*); } The special --value() function is used to resolve the utility value. Matching theme values Use the --value(--theme-key-*) syntax to resolve the utility value against a set of theme keys: CSS @theme { --tab-size-2 : 2 ; --tab-size-4 : 4 ; --tab-size-github : 8 ; } @utility tab-* { tab-size: --value(--tab-size-*); } This will match utilities like tab-2 , tab-4 , and tab-github . Bare values To resolve the value as a bare value, use the --value({type}) syntax, where {type} is the data type you want to validate the bare value as: CSS @utility tab-* { tab-size: --value(integer); } This will match utilities like tab-1 and tab-76 . Available bare value data types are: number , integer , ratio , and percentage . Literal values To support literal values, use the --value('literal') syntax (notice the quotes): CSS @utility tab-* { tab-size: --value("inherit", "initial", "unset"); } This will match utilities like tab-inherit , tab-initial , and tab-unset . Arbitrary values To support arbitrary values, use the --value([{type}]) syntax (notice the square brackets) to tell Tailwind which types are supported as an arbitrary value: CSS @utility tab-* { tab-size: --value([integer]); } This will match utilities like tab-[1] and tab-[76] . Available arbitrary value data types are: absolute-size , angle , bg-size , color , family-name , generic-name , image , integer , length , line-width , number , percentage , position , ratio , relative-size , url , vector , and * . Supporting theme, bare, and arbitrary values together All three forms of the --value() function can be used within a rule as multiple declarations, and any declarations that fail to resolve will be omitted in the output: CSS @theme { --tab-size-github : 8 ; } @utility tab-* { tab-size: --value([integer]); tab-size: --value(integer); tab-size: --value(--tab-size-*); } This makes it possible to treat the value differently in each case if necessary, for example translating a bare integer to a percentage: CSS @utility opacity-* { opacity: --value([percentage]); opacity: calc(--value(integer) * 1%); opacity: --value(--opacity-*); } The --value() function can also take multiple arguments and resolve them left to right if you don't need to treat the return value differently in different cases: CSS @theme { --tab-size-github : 8 ; } @utility tab-* { tab-size: --value(--tab-size-*, integer, [integer]); } @utility opacity-* { opacity: calc(--value(integer) * 1%); opacity: --value(--opacity-*, [percentage]); } Negative values To support negative values, register separate positive and negative utilities into separate declarations: CSS @utility inset-* { inset: --spacing(--value(integer)); inset: --value([percentage], [length]); } @utility -inset-* { inset: --spacing(--value(integer) * -1); inset: calc(--value([percentage], [length]) * -1); } Modifiers Modifiers are handled using the --modifier() function which works exactly like the --value() function but operates on a modifier if present: CSS @utility text-* { font-size: --value(--text-*, [length]); line-height: --modifier(--leading-*, [length], [*]); } If a modifier isn't present, any declaration depending on a modifier is just not included in the output. Fractions To handle fractions, we rely on the CSS ratio data type. If this is used with --value() , it's a signal to Tailwind to treat the value and modifier as a single value: CSS @utility aspect-* { aspect-ratio: --value(--aspect-ratio-*, ratio, [ratio] ); } This will match utilities like aspect-square , aspect-3/4 , and aspect-[7/9] . Adding custom variants In addition to using the variants that ship with Tailwind, you can also add your own custom variants using the @custom-variant directive: @custom-variant theme-midnight { & :where ( [ data-theme = " midnight " ] * ) { @ slot ; } } Now you can use the theme-midnight: <utility> variant in your HTML: < html data-theme = "midnight" > < button class = " theme-midnight:bg-black ..." ></ button > </ html > You can create variants using the shorthand syntax when nesting isn't required: @custom-variant theme-midnight (&:where([data-theme="midnight"] *)) ; When a custom variant has multiple rules, they can be nested within each other: @custom-variant any-hover { @media ( any-hover : hover ) { & :hover { @ slot ; } } }
Markdown
v4.2 `⌘K``Ctrl K` [Docs](https://tailwindcss.com/docs)[Blog](https://tailwindcss.com/blog)[Showcase](https://tailwindcss.com/showcase)[Sponsor](https://tailwindcss.com/sponsor) [Plus](https://tailwindcss.com/plus?ref=top) 1. Core concepts 2. Adding custom styles - [Documentation](https://tailwindcss.com/docs/installation) - [Components](https://tailwindcss.com/plus/ui-blocks?ref=sidebar) - [Templates](https://tailwindcss.com/plus/templates?ref=sidebar) - [UI Kit](https://tailwindcss.com/plus/ui-kit?ref=sidebar) - [Playground](https://play.tailwindcss.com/) - [CourseNew](https://tailwindcss.com/build-uis-that-dont-suck) - [Community](https://tailwindcss.com/insiders) ### Getting started - [Installation](https://tailwindcss.com/docs/installation) - [Editor setup](https://tailwindcss.com/docs/editor-setup) - [Compatibility](https://tailwindcss.com/docs/compatibility) - [Upgrade guide](https://tailwindcss.com/docs/upgrade-guide) ### Core concepts - [Styling with utility classes](https://tailwindcss.com/docs/styling-with-utility-classes) - [Hover, focus, and other states](https://tailwindcss.com/docs/hover-focus-and-other-states) - [Responsive design](https://tailwindcss.com/docs/responsive-design) - [Dark mode](https://tailwindcss.com/docs/dark-mode) - [Theme variables](https://tailwindcss.com/docs/theme) - [Colors](https://tailwindcss.com/docs/colors) - [Adding custom styles](https://tailwindcss.com/docs/adding-custom-styles) - [Detecting classes in source files](https://tailwindcss.com/docs/detecting-classes-in-source-files) - [Functions and directives](https://tailwindcss.com/docs/functions-and-directives) ### Base styles - [Preflight](https://tailwindcss.com/docs/preflight) ### Layout - [aspect-ratio](https://tailwindcss.com/docs/aspect-ratio) - [columns](https://tailwindcss.com/docs/columns) - [break-after](https://tailwindcss.com/docs/break-after) - [break-before](https://tailwindcss.com/docs/break-before) - [break-inside](https://tailwindcss.com/docs/break-inside) - [box-decoration-break](https://tailwindcss.com/docs/box-decoration-break) - [box-sizing](https://tailwindcss.com/docs/box-sizing) - [display](https://tailwindcss.com/docs/display) - [float](https://tailwindcss.com/docs/float) - [clear](https://tailwindcss.com/docs/clear) - [isolation](https://tailwindcss.com/docs/isolation) - [object-fit](https://tailwindcss.com/docs/object-fit) - [object-position](https://tailwindcss.com/docs/object-position) - [overflow](https://tailwindcss.com/docs/overflow) - [overscroll-behavior](https://tailwindcss.com/docs/overscroll-behavior) - [position](https://tailwindcss.com/docs/position) - [top / right / bottom / left](https://tailwindcss.com/docs/top-right-bottom-left) - [visibility](https://tailwindcss.com/docs/visibility) - [z-index](https://tailwindcss.com/docs/z-index) ### Flexbox & Grid - [flex-basis](https://tailwindcss.com/docs/flex-basis) - [flex-direction](https://tailwindcss.com/docs/flex-direction) - [flex-wrap](https://tailwindcss.com/docs/flex-wrap) - [flex](https://tailwindcss.com/docs/flex) - [flex-grow](https://tailwindcss.com/docs/flex-grow) - [flex-shrink](https://tailwindcss.com/docs/flex-shrink) - [order](https://tailwindcss.com/docs/order) - [grid-template-columns](https://tailwindcss.com/docs/grid-template-columns) - [grid-column](https://tailwindcss.com/docs/grid-column) - [grid-template-rows](https://tailwindcss.com/docs/grid-template-rows) - [grid-row](https://tailwindcss.com/docs/grid-row) - [grid-auto-flow](https://tailwindcss.com/docs/grid-auto-flow) - [grid-auto-columns](https://tailwindcss.com/docs/grid-auto-columns) - [grid-auto-rows](https://tailwindcss.com/docs/grid-auto-rows) - [gap](https://tailwindcss.com/docs/gap) - [justify-content](https://tailwindcss.com/docs/justify-content) - [justify-items](https://tailwindcss.com/docs/justify-items) - [justify-self](https://tailwindcss.com/docs/justify-self) - [align-content](https://tailwindcss.com/docs/align-content) - [align-items](https://tailwindcss.com/docs/align-items) - [align-self](https://tailwindcss.com/docs/align-self) - [place-content](https://tailwindcss.com/docs/place-content) - [place-items](https://tailwindcss.com/docs/place-items) - [place-self](https://tailwindcss.com/docs/place-self) ### Spacing - [padding](https://tailwindcss.com/docs/padding) - [margin](https://tailwindcss.com/docs/margin) ### Sizing - [width](https://tailwindcss.com/docs/width) - [min-width](https://tailwindcss.com/docs/min-width) - [max-width](https://tailwindcss.com/docs/max-width) - [height](https://tailwindcss.com/docs/height) - [min-height](https://tailwindcss.com/docs/min-height) - [max-height](https://tailwindcss.com/docs/max-height) - [inline-size](https://tailwindcss.com/docs/inline-size) - [min-inline-size](https://tailwindcss.com/docs/min-inline-size) - [max-inline-size](https://tailwindcss.com/docs/max-inline-size) - [block-size](https://tailwindcss.com/docs/block-size) - [min-block-size](https://tailwindcss.com/docs/min-block-size) - [max-block-size](https://tailwindcss.com/docs/max-block-size) ### Typography - [font-family](https://tailwindcss.com/docs/font-family) - [font-size](https://tailwindcss.com/docs/font-size) - [font-smoothing](https://tailwindcss.com/docs/font-smoothing) - [font-style](https://tailwindcss.com/docs/font-style) - [font-weight](https://tailwindcss.com/docs/font-weight) - [font-stretch](https://tailwindcss.com/docs/font-stretch) - [font-variant-numeric](https://tailwindcss.com/docs/font-variant-numeric) - [font-feature-settings](https://tailwindcss.com/docs/font-feature-settings) - [letter-spacing](https://tailwindcss.com/docs/letter-spacing) - [line-clamp](https://tailwindcss.com/docs/line-clamp) - [line-height](https://tailwindcss.com/docs/line-height) - [list-style-image](https://tailwindcss.com/docs/list-style-image) - [list-style-position](https://tailwindcss.com/docs/list-style-position) - [list-style-type](https://tailwindcss.com/docs/list-style-type) - [text-align](https://tailwindcss.com/docs/text-align) - [color](https://tailwindcss.com/docs/color) - [text-decoration-line](https://tailwindcss.com/docs/text-decoration-line) - [text-decoration-color](https://tailwindcss.com/docs/text-decoration-color) - [text-decoration-style](https://tailwindcss.com/docs/text-decoration-style) - [text-decoration-thickness](https://tailwindcss.com/docs/text-decoration-thickness) - [text-underline-offset](https://tailwindcss.com/docs/text-underline-offset) - [text-transform](https://tailwindcss.com/docs/text-transform) - [text-overflow](https://tailwindcss.com/docs/text-overflow) - [text-wrap](https://tailwindcss.com/docs/text-wrap) - [text-indent](https://tailwindcss.com/docs/text-indent) - [vertical-align](https://tailwindcss.com/docs/vertical-align) - [white-space](https://tailwindcss.com/docs/white-space) - [word-break](https://tailwindcss.com/docs/word-break) - [overflow-wrap](https://tailwindcss.com/docs/overflow-wrap) - [hyphens](https://tailwindcss.com/docs/hyphens) - [content](https://tailwindcss.com/docs/content) ### Backgrounds - [background-attachment](https://tailwindcss.com/docs/background-attachment) - [background-clip](https://tailwindcss.com/docs/background-clip) - [background-color](https://tailwindcss.com/docs/background-color) - [background-image](https://tailwindcss.com/docs/background-image) - [background-origin](https://tailwindcss.com/docs/background-origin) - [background-position](https://tailwindcss.com/docs/background-position) - [background-repeat](https://tailwindcss.com/docs/background-repeat) - [background-size](https://tailwindcss.com/docs/background-size) ### Borders - [border-radius](https://tailwindcss.com/docs/border-radius) - [border-width](https://tailwindcss.com/docs/border-width) - [border-color](https://tailwindcss.com/docs/border-color) - [border-style](https://tailwindcss.com/docs/border-style) - [outline-width](https://tailwindcss.com/docs/outline-width) - [outline-color](https://tailwindcss.com/docs/outline-color) - [outline-style](https://tailwindcss.com/docs/outline-style) - [outline-offset](https://tailwindcss.com/docs/outline-offset) ### Effects - [box-shadow](https://tailwindcss.com/docs/box-shadow) - [text-shadow](https://tailwindcss.com/docs/text-shadow) - [opacity](https://tailwindcss.com/docs/opacity) - [mix-blend-mode](https://tailwindcss.com/docs/mix-blend-mode) - [background-blend-mode](https://tailwindcss.com/docs/background-blend-mode) - [mask-clip](https://tailwindcss.com/docs/mask-clip) - [mask-composite](https://tailwindcss.com/docs/mask-composite) - [mask-image](https://tailwindcss.com/docs/mask-image) - [mask-mode](https://tailwindcss.com/docs/mask-mode) - [mask-origin](https://tailwindcss.com/docs/mask-origin) - [mask-position](https://tailwindcss.com/docs/mask-position) - [mask-repeat](https://tailwindcss.com/docs/mask-repeat) - [mask-size](https://tailwindcss.com/docs/mask-size) - [mask-type](https://tailwindcss.com/docs/mask-type) ### Filters - [filter](https://tailwindcss.com/docs/filter) - [blur](https://tailwindcss.com/docs/filter-blur) - [brightness](https://tailwindcss.com/docs/filter-brightness) - [contrast](https://tailwindcss.com/docs/filter-contrast) - [drop-shadow](https://tailwindcss.com/docs/filter-drop-shadow) - [grayscale](https://tailwindcss.com/docs/filter-grayscale) - [hue-rotate](https://tailwindcss.com/docs/filter-hue-rotate) - [invert](https://tailwindcss.com/docs/filter-invert) - [saturate](https://tailwindcss.com/docs/filter-saturate) - [sepia](https://tailwindcss.com/docs/filter-sepia) - [backdrop-filter](https://tailwindcss.com/docs/backdrop-filter) - [blur](https://tailwindcss.com/docs/backdrop-filter-blur) - [brightness](https://tailwindcss.com/docs/backdrop-filter-brightness) - [contrast](https://tailwindcss.com/docs/backdrop-filter-contrast) - [grayscale](https://tailwindcss.com/docs/backdrop-filter-grayscale) - [hue-rotate](https://tailwindcss.com/docs/backdrop-filter-hue-rotate) - [invert](https://tailwindcss.com/docs/backdrop-filter-invert) - [opacity](https://tailwindcss.com/docs/backdrop-filter-opacity) - [saturate](https://tailwindcss.com/docs/backdrop-filter-saturate) - [sepia](https://tailwindcss.com/docs/backdrop-filter-sepia) ### Tables - [border-collapse](https://tailwindcss.com/docs/border-collapse) - [border-spacing](https://tailwindcss.com/docs/border-spacing) - [table-layout](https://tailwindcss.com/docs/table-layout) - [caption-side](https://tailwindcss.com/docs/caption-side) ### Transitions & Animation - [transition-property](https://tailwindcss.com/docs/transition-property) - [transition-behavior](https://tailwindcss.com/docs/transition-behavior) - [transition-duration](https://tailwindcss.com/docs/transition-duration) - [transition-timing-function](https://tailwindcss.com/docs/transition-timing-function) - [transition-delay](https://tailwindcss.com/docs/transition-delay) - [animation](https://tailwindcss.com/docs/animation) ### Transforms - [backface-visibility](https://tailwindcss.com/docs/backface-visibility) - [perspective](https://tailwindcss.com/docs/perspective) - [perspective-origin](https://tailwindcss.com/docs/perspective-origin) - [rotate](https://tailwindcss.com/docs/rotate) - [scale](https://tailwindcss.com/docs/scale) - [skew](https://tailwindcss.com/docs/skew) - [transform](https://tailwindcss.com/docs/transform) - [transform-origin](https://tailwindcss.com/docs/transform-origin) - [transform-style](https://tailwindcss.com/docs/transform-style) - [translate](https://tailwindcss.com/docs/translate) ### Interactivity - [accent-color](https://tailwindcss.com/docs/accent-color) - [appearance](https://tailwindcss.com/docs/appearance) - [caret-color](https://tailwindcss.com/docs/caret-color) - [color-scheme](https://tailwindcss.com/docs/color-scheme) - [cursor](https://tailwindcss.com/docs/cursor) - [field-sizing](https://tailwindcss.com/docs/field-sizing) - [pointer-events](https://tailwindcss.com/docs/pointer-events) - [resize](https://tailwindcss.com/docs/resize) - [scroll-behavior](https://tailwindcss.com/docs/scroll-behavior) - [scroll-margin](https://tailwindcss.com/docs/scroll-margin) - [scroll-padding](https://tailwindcss.com/docs/scroll-padding) - [scroll-snap-align](https://tailwindcss.com/docs/scroll-snap-align) - [scroll-snap-stop](https://tailwindcss.com/docs/scroll-snap-stop) - [scroll-snap-type](https://tailwindcss.com/docs/scroll-snap-type) - [touch-action](https://tailwindcss.com/docs/touch-action) - [user-select](https://tailwindcss.com/docs/user-select) - [will-change](https://tailwindcss.com/docs/will-change) ### SVG - [fill](https://tailwindcss.com/docs/fill) - [stroke](https://tailwindcss.com/docs/stroke) - [stroke-width](https://tailwindcss.com/docs/stroke-width) ### Accessibility - [forced-color-adjust](https://tailwindcss.com/docs/forced-color-adjust) Core concepts # Adding custom styles Best practices for adding your own custom styles in Tailwind projects. Often the biggest challenge when working with a framework is figuring out what you’re supposed to do when there’s something you need that the framework doesn’t handle for you. Tailwind has been designed from the ground up to be extensible and customizable, so that no matter what you’re building you never feel like you’re fighting the framework. This guide covers topics like customizing your design tokens, how to break out of those constraints when necessary, adding your own custom CSS, and extending the framework with plugins. ## [Customizing your theme](https://tailwindcss.com/docs/adding-custom-styles#customizing-your-theme) If you want to change things like your color palette, spacing scale, typography scale, or breakpoints, add your customizations using the `@theme` directive in your CSS: CSS ``` @theme { --font-display: "Satoshi", "sans-serif"; --breakpoint-3xl: 120rem; --color-avocado-100: oklch(0.99 0 0); --color-avocado-200: oklch(0.98 0.04 113.22); --color-avocado-300: oklch(0.94 0.11 115.03); --color-avocado-400: oklch(0.92 0.19 114.08); --color-avocado-500: oklch(0.84 0.18 117.33); --color-avocado-600: oklch(0.53 0.12 118.34); --ease-fluid: cubic-bezier(0.3, 0, 0, 1); --ease-snappy: cubic-bezier(0.2, 0, 0, 1); /* ... */} ``` Learn more about customizing your theme in the [theme variables documentation](https://tailwindcss.com/docs/theme). ## [Using arbitrary values](https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values) While you can usually build the bulk of a well-crafted design using a constrained set of design tokens, once in a while you need to break out of those constraints to get things pixel-perfect. When you find yourself really needing something like `top: 117px` to get a background image in just the right spot, use Tailwind's square bracket notation to generate a class on the fly with any arbitrary value: HTML ``` <div class="top-[117px]"> <!-- ... --></div> ``` This is basically like inline styles, with the major benefit that you can combine it with interactive modifiers like `hover` and responsive modifiers like `lg`: HTML ``` <div class="top-[117px] lg:top-[344px]"> <!-- ... --></div> ``` This works for everything in the framework, including things like background colors, font sizes, pseudo-element content, and more: HTML ``` <div class="bg-[#bada55] text-[22px] before:content-['Festivus']"> <!-- ... --></div> ``` If you're referencing a CSS variable as an arbitrary value, you can use the custom property syntax: HTML ``` <div class="fill-(--my-brand-color) ..."> <!-- ... --></div> ``` This is just a shorthand for `fill-[var(--my-brand-color)]` that adds the `var()` function for you automatically. ### [Arbitrary properties](https://tailwindcss.com/docs/adding-custom-styles#arbitrary-properties) If you ever need to use a CSS property that Tailwind doesn't include a utility for out of the box, you can also use square bracket notation to write completely arbitrary CSS: HTML ``` <div class="[mask-type:luminance]"> <!-- ... --></div> ``` This is *really* like inline styles, but again with the benefit that you can use modifiers: HTML ``` <div class="[mask-type:luminance] hover:[mask-type:alpha]"> <!-- ... --></div> ``` This can be useful for things like CSS variables as well, especially when they need to change under different conditions: HTML ``` <div class="[--scroll-offset:56px] lg:[--scroll-offset:44px]"> <!-- ... --></div> ``` ### [Arbitrary variants](https://tailwindcss.com/docs/adding-custom-styles#arbitrary-variants) Arbitrary *variants* are like arbitrary values but for doing on-the-fly selector modification, like you can with built-in pseudo-class variants like `hover:{utility}` or responsive variants like `md:{utility}` but using square bracket notation directly in your HTML. HTML ``` <ul role="list"> {#each items as item} <li class="lg:[&:nth-child(-n+3)]:hover:underline">{item}</li> {/each}</ul> ``` Learn more in the [arbitrary variants](https://tailwindcss.com/docs/hover-focus-and-other-states#using-arbitrary-variants) documentation. ### [Handling whitespace](https://tailwindcss.com/docs/adding-custom-styles#handling-whitespace) When an arbitrary value needs to contain a space, use an underscore (`_`) instead and Tailwind will automatically convert it to a space at build-time: HTML ``` <div class="grid grid-cols-[1fr_500px_2fr]"> <!-- ... --></div> ``` In situations where underscores are common but spaces are invalid, Tailwind will preserve the underscore instead of converting it to a space, for example in URLs: HTML ``` <div class="bg-[url('/what_a_rush.png')]"> <!-- ... --></div> ``` In the rare case that you actually need to use an underscore but it's ambiguous because a space is valid as well, escape the underscore with a backslash and Tailwind won't convert it to a space: HTML ``` <div class="before:content-['hello\_world']"> <!-- ... --></div> ``` If you're using something like JSX where the backslash is stripped from the rendered HTML, use [String.raw()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw) so the backslash isn't treated as a JavaScript escape character: ``` <div className={String.raw`before:content-['hello\_world']`}> <!-- ... --></div> ``` ### [Resolving ambiguities](https://tailwindcss.com/docs/adding-custom-styles#resolving-ambiguities) Many utilities in Tailwind share a common namespace but map to different CSS properties. For example `text-lg` and `text-black` both share the `text-` namespace, but one is for `font-size` and the other is for `color`. When using arbitrary values, Tailwind can generally handle this ambiguity automatically based on the value you pass in: HTML ``` <!-- Will generate a font-size utility --><div class="text-[22px]">...</div><!-- Will generate a color utility --><div class="text-[#bada55]">...</div> ``` Sometimes it really is ambiguous though, for example when using CSS variables: HTML ``` <div class="text-(--my-var)">...</div> ``` In these situations, you can "hint" the underlying type to Tailwind by adding a [CSS data type](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Types) before the value: HTML ``` <!-- Will generate a font-size utility --><div class="text-(length:--my-var)">...</div><!-- Will generate a color utility --><div class="text-(color:--my-var)">...</div> ``` ## [Using custom CSS](https://tailwindcss.com/docs/adding-custom-styles#using-custom-css) While Tailwind is designed to handle the bulk of your styling needs, there is nothing stopping you from just writing plain CSS when you need to: CSS ``` @import "tailwindcss";.my-custom-style { /* ... */} ``` ### [Adding base styles](https://tailwindcss.com/docs/adding-custom-styles#adding-base-styles) If you just want to set some defaults for the page (like the text color, background color, or font family), the easiest option is just adding some classes to the `html` or `body` elements: HTML ``` <!doctype html><html lang="en" class="bg-gray-100 font-serif text-gray-900"> <!-- ... --></html> ``` This keeps your base styling decisions in your markup alongside all of your other styles, instead of hiding them in a separate file. If you want to add your own default base styles for specific HTML elements, use the `@layer` directive to add those styles to Tailwind's `base` layer: CSS ``` @layer base { h1 { font-size: var(--text-2xl); } h2 { font-size: var(--text-xl); }} ``` ### [Adding component classes](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes) Use the `components` layer for any more complicated classes you want to add to your project that you'd still like to be able to override with utility classes. Traditionally these would be classes like `card`, `btn`, `badge` — that kind of thing. CSS ``` @layer components { .card { background-color: var(--color-white); border-radius: var(--radius-lg); padding: --spacing(6); box-shadow: var(--shadow-xl); }} ``` By defining component classes in the `components` layer, you can still use utility classes to override them when necessary: HTML ``` <!-- Will look like a card, but with square corners --><div class="card rounded-none"> <!-- ... --></div> ``` Using Tailwind you probably don't need these types of classes as often as you think. Read our guide on [managing duplication](https://tailwindcss.com/docs/styling-with-utility-classes#managing-duplication) for our recommendations. The `components` layer is also a good place to put custom styles for any third-party components you're using: CSS ``` @layer components { .select2-dropdown { /* ... */ }} ``` ### [Using variants](https://tailwindcss.com/docs/adding-custom-styles#using-variants) Use the `@variant` directive to apply a Tailwind variant within custom CSS: app.css ``` .my-element { background: white; @variant dark { background: black; }} ``` Compiled CSS ``` .my-element { background: white; @media (prefers-color-scheme: dark) { background: black; }} ``` If you need to apply multiple variants at the same time, use nesting: app.css ``` .my-element { background: white; @variant dark { @variant hover { background: black; } }} ``` Compiled CSS ``` .my-element { background: white; @media (prefers-color-scheme: dark) { &:hover { @media (hover: hover) { background: black; } } }} ``` ## [Adding custom utilities](https://tailwindcss.com/docs/adding-custom-styles#adding-custom-utilities) ### [Simple utilities](https://tailwindcss.com/docs/adding-custom-styles#simple-utilities) In addition to using the utilities that ship with Tailwind, you can also add your own custom utilities. This can be useful when there's a CSS feature you'd like to use in your project that Tailwind doesn't include utilities for out of the box. Use the `@utility` directive to add a custom utility to your project: CSS ``` @utility content-auto { content-visibility: auto;} ``` You can now use this utility in your HTML: HTML ``` <div class="content-auto"> <!-- ... --></div> ``` It will also work with variants like `hover`, `focus` and `lg`: HTML ``` <div class="hover:content-auto"> <!-- ... --></div> ``` Custom utilities are automatically inserted into the `utilities` layer along with all of the built-in utilities in the framework. ### [Complex utilities](https://tailwindcss.com/docs/adding-custom-styles#complex-utilities) If your custom utility is more complex than a single class name, use nesting to define the utility: CSS ``` @utility scrollbar-hidden { &::-webkit-scrollbar { display: none; }} ``` ### [Functional utilities](https://tailwindcss.com/docs/adding-custom-styles#functional-utilities) In addition to registering simple utilities with the `@utility` directive, you can also register functional utilities that accept an argument: CSS ``` @utility tab-* { tab-size: --value(--tab-size-*);} ``` The special `--value()` function is used to resolve the utility value. #### [Matching theme values](https://tailwindcss.com/docs/adding-custom-styles#matching-theme-values) Use the `--value(--theme-key-*)` syntax to resolve the utility value against a set of theme keys: CSS ``` @theme { --tab-size-2: 2; --tab-size-4: 4; --tab-size-github: 8;}@utility tab-* { tab-size: --value(--tab-size-*);} ``` This will match utilities like `tab-2`, `tab-4`, and `tab-github`. #### [Bare values](https://tailwindcss.com/docs/adding-custom-styles#bare-values) To resolve the value as a bare value, use the `--value({type})` syntax, where `{type}` is the data type you want to validate the bare value as: CSS ``` @utility tab-* { tab-size: --value(integer);} ``` This will match utilities like `tab-1` and `tab-76`. Available bare value data types are: `number`, `integer`, `ratio`, and `percentage`. #### [Literal values](https://tailwindcss.com/docs/adding-custom-styles#literal-values) To support literal values, use the `--value('literal')` syntax (notice the quotes): CSS ``` @utility tab-* { tab-size: --value("inherit", "initial", "unset");} ``` This will match utilities like `tab-inherit`, `tab-initial`, and `tab-unset`. #### [Arbitrary values](https://tailwindcss.com/docs/adding-custom-styles#arbitrary-values) To support arbitrary values, use the `--value([{type}])` syntax (notice the square brackets) to tell Tailwind which types are supported as an arbitrary value: CSS ``` @utility tab-* { tab-size: --value([integer]);} ``` This will match utilities like `tab-[1]` and `tab-[76]`. Available arbitrary value data types are: `absolute-size`, `angle`, `bg-size`, `color`, `family-name`, `generic-name`, `image`, `integer`, `length`, `line-width`, `number`, `percentage`, `position`, `ratio`, `relative-size`, `url`, `vector`, and `*`. #### [Supporting theme, bare, and arbitrary values together](https://tailwindcss.com/docs/adding-custom-styles#supporting-theme-bare-and-arbitrary-values-together) All three forms of the `--value()` function can be used within a rule as multiple declarations, and any declarations that fail to resolve will be omitted in the output: CSS ``` @theme { --tab-size-github: 8;}@utility tab-* { tab-size: --value([integer]); tab-size: --value(integer); tab-size: --value(--tab-size-*);} ``` This makes it possible to treat the value differently in each case if necessary, for example translating a bare integer to a percentage: CSS ``` @utility opacity-* { opacity: --value([percentage]); opacity: calc(--value(integer) * 1%); opacity: --value(--opacity-*);} ``` The `--value()` function can also take multiple arguments and resolve them left to right if you don't need to treat the return value differently in different cases: CSS ``` @theme { --tab-size-github: 8;}@utility tab-* { tab-size: --value(--tab-size-*, integer, [integer]);}@utility opacity-* { opacity: calc(--value(integer) * 1%); opacity: --value(--opacity-*, [percentage]);} ``` #### [Negative values](https://tailwindcss.com/docs/adding-custom-styles#negative-values) To support negative values, register separate positive and negative utilities into separate declarations: CSS ``` @utility inset-* { inset: --spacing(--value(integer)); inset: --value([percentage], [length]);}@utility -inset-* { inset: --spacing(--value(integer) * -1); inset: calc(--value([percentage], [length]) * -1);} ``` #### [Modifiers](https://tailwindcss.com/docs/adding-custom-styles#modifiers) Modifiers are handled using the `--modifier()` function which works exactly like the `--value()` function but operates on a modifier if present: CSS ``` @utility text-* { font-size: --value(--text-*, [length]); line-height: --modifier(--leading-*, [length], [*]);} ``` If a modifier isn't present, any declaration depending on a modifier is just not included in the output. #### [Fractions](https://tailwindcss.com/docs/adding-custom-styles#fractions) To handle fractions, we rely on the CSS `ratio` data type. If this is used with `--value()`, it's a signal to Tailwind to treat the value and modifier as a single value: CSS ``` @utility aspect-* { aspect-ratio: --value(--aspect-ratio-*, ratio, [ratio]);} ``` This will match utilities like `aspect-square`, `aspect-3/4`, and `aspect-[7/9]`. ## [Adding custom variants](https://tailwindcss.com/docs/adding-custom-styles#adding-custom-variants) In addition to using the variants that ship with Tailwind, you can also add your own custom variants using the `@custom-variant` directive: ``` @custom-variant theme-midnight { &:where([data-theme="midnight"] *) { @slot; }} ``` Now you can use the `theme-midnight:<utility>` variant in your HTML: ``` <html data-theme="midnight"> <button class="theme-midnight:bg-black ..."></button></html> ``` You can create variants using the shorthand syntax when nesting isn't required: ``` @custom-variant theme-midnight (&:where([data-theme="midnight"] *)); ``` When a custom variant has multiple rules, they can be nested within each other: ``` @custom-variant any-hover { @media (any-hover: hover) { &:hover { @slot; } }} ``` [Colors](https://tailwindcss.com/docs/colors) [Detecting classes in source files](https://tailwindcss.com/docs/detecting-classes-in-source-files) ### On this page - [Customizing your theme](https://tailwindcss.com/docs/adding-custom-styles#customizing-your-theme) - [Using arbitrary values](https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values) - [Arbitrary properties](https://tailwindcss.com/docs/adding-custom-styles#arbitrary-properties) - [Arbitrary variants](https://tailwindcss.com/docs/adding-custom-styles#arbitrary-variants) - [Handling whitespace](https://tailwindcss.com/docs/adding-custom-styles#handling-whitespace) - [Resolving ambiguities](https://tailwindcss.com/docs/adding-custom-styles#resolving-ambiguities) - [Using custom CSS](https://tailwindcss.com/docs/adding-custom-styles#using-custom-css) - [Adding base styles](https://tailwindcss.com/docs/adding-custom-styles#adding-base-styles) - [Adding component classes](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes) - [Using variants](https://tailwindcss.com/docs/adding-custom-styles#using-variants) - [Adding custom utilities](https://tailwindcss.com/docs/adding-custom-styles#adding-custom-utilities) - [Simple utilities](https://tailwindcss.com/docs/adding-custom-styles#simple-utilities) - [Complex utilities](https://tailwindcss.com/docs/adding-custom-styles#complex-utilities) - [Functional utilities](https://tailwindcss.com/docs/adding-custom-styles#functional-utilities) - [Adding custom variants](https://tailwindcss.com/docs/adding-custom-styles#adding-custom-variants) ### Tailwind CSS - [Documentation](https://tailwindcss.com/docs) - [Playground](https://play.tailwindcss.com/) - [Blog](https://tailwindcss.com/blog) - [Showcase](https://tailwindcss.com/showcase) - [Sponsor](https://tailwindcss.com/sponsor) ### Resources - [Refactoring UI](https://www.refactoringui.com/) - [Headless UI](https://headlessui.com/) - [Heroicons](https://heroicons.com/) - [Hero Patterns](https://heropatterns.com/) ### [Tailwind Plus](https://tailwindcss.com/plus?ref=footer) - [UI Blocks](https://tailwindcss.com/plus/ui-blocks?ref=footer) - [Templates](https://tailwindcss.com/plus/templates?ref=footer) - [UI Kit](https://tailwindcss.com/plus/ui-kit?ref=footer) ### Community - [GitHub](https://github.com/tailwindlabs/tailwindcss) - [Discord](https://tailwindcss.com/sponsor#insiders) - [X](https://x.com/tailwindcss) ### Tailwind CSS - [Documentation](https://tailwindcss.com/docs) - [Playground](https://play.tailwindcss.com/) - [Blog](https://tailwindcss.com/blog) - [Showcase](https://tailwindcss.com/showcase) - [Sponsor](https://tailwindcss.com/sponsor) ### [Tailwind Plus](https://tailwindcss.com/plus?ref=footer) - [UI Blocks](https://tailwindcss.com/plus/ui-blocks?ref=footer) - [Templates](https://tailwindcss.com/plus/templates?ref=footer) - [UI Kit](https://tailwindcss.com/plus/ui-kit?ref=footer) ### Resources - [Refactoring UI](https://www.refactoringui.com/) - [Headless UI](https://headlessui.com/) - [Heroicons](https://heroicons.com/) - [Hero Patterns](https://heropatterns.com/) ### Community - [GitHub](https://github.com/tailwindlabs/tailwindcss) - [Discord](https://tailwindcss.com/sponsor#insiders) - [X](https://x.com/tailwindcss) Copyright © 2026 Tailwind Labs Inc.·[Trademark Policy](https://tailwindcss.com/brand)
Readable Markdown
Best practices for adding your own custom styles in Tailwind projects. Often the biggest challenge when working with a framework is figuring out what you’re supposed to do when there’s something you need that the framework doesn’t handle for you. Tailwind has been designed from the ground up to be extensible and customizable, so that no matter what you’re building you never feel like you’re fighting the framework. This guide covers topics like customizing your design tokens, how to break out of those constraints when necessary, adding your own custom CSS, and extending the framework with plugins. ## [Customizing your theme](https://tailwindcss.com/docs/adding-custom-styles#customizing-your-theme) If you want to change things like your color palette, spacing scale, typography scale, or breakpoints, add your customizations using the `@theme` directive in your CSS: CSS ``` @theme { --font-display: "Satoshi", "sans-serif"; --breakpoint-3xl: 120rem; --color-avocado-100: oklch(0.99 0 0); --color-avocado-200: oklch(0.98 0.04 113.22); --color-avocado-300: oklch(0.94 0.11 115.03); --color-avocado-400: oklch(0.92 0.19 114.08); --color-avocado-500: oklch(0.84 0.18 117.33); --color-avocado-600: oklch(0.53 0.12 118.34); --ease-fluid: cubic-bezier(0.3, 0, 0, 1); --ease-snappy: cubic-bezier(0.2, 0, 0, 1); /* ... */} ``` Learn more about customizing your theme in the [theme variables documentation](https://tailwindcss.com/docs/theme). ## [Using arbitrary values](https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values) While you can usually build the bulk of a well-crafted design using a constrained set of design tokens, once in a while you need to break out of those constraints to get things pixel-perfect. When you find yourself really needing something like `top: 117px` to get a background image in just the right spot, use Tailwind's square bracket notation to generate a class on the fly with any arbitrary value: HTML ``` <div class="top-[117px]"> <!-- ... --></div> ``` This is basically like inline styles, with the major benefit that you can combine it with interactive modifiers like `hover` and responsive modifiers like `lg`: HTML ``` <div class="top-[117px] lg:top-[344px]"> <!-- ... --></div> ``` This works for everything in the framework, including things like background colors, font sizes, pseudo-element content, and more: HTML ``` <div class="bg-[#bada55] text-[22px] before:content-['Festivus']"> <!-- ... --></div> ``` If you're referencing a CSS variable as an arbitrary value, you can use the custom property syntax: HTML ``` <div class="fill-(--my-brand-color) ..."> <!-- ... --></div> ``` This is just a shorthand for `fill-[var(--my-brand-color)]` that adds the `var()` function for you automatically. ### [Arbitrary properties](https://tailwindcss.com/docs/adding-custom-styles#arbitrary-properties) If you ever need to use a CSS property that Tailwind doesn't include a utility for out of the box, you can also use square bracket notation to write completely arbitrary CSS: HTML ``` <div class="[mask-type:luminance]"> <!-- ... --></div> ``` This is *really* like inline styles, but again with the benefit that you can use modifiers: HTML ``` <div class="[mask-type:luminance] hover:[mask-type:alpha]"> <!-- ... --></div> ``` This can be useful for things like CSS variables as well, especially when they need to change under different conditions: HTML ``` <div class="[--scroll-offset:56px] lg:[--scroll-offset:44px]"> <!-- ... --></div> ``` ### [Arbitrary variants](https://tailwindcss.com/docs/adding-custom-styles#arbitrary-variants) Arbitrary *variants* are like arbitrary values but for doing on-the-fly selector modification, like you can with built-in pseudo-class variants like `hover:{utility}` or responsive variants like `md:{utility}` but using square bracket notation directly in your HTML. HTML ``` <ul role="list"> {#each items as item} <li class="lg:[&:nth-child(-n+3)]:hover:underline">{item}</li> {/each}</ul> ``` Learn more in the [arbitrary variants](https://tailwindcss.com/docs/hover-focus-and-other-states#using-arbitrary-variants) documentation. ### [Handling whitespace](https://tailwindcss.com/docs/adding-custom-styles#handling-whitespace) When an arbitrary value needs to contain a space, use an underscore (`_`) instead and Tailwind will automatically convert it to a space at build-time: HTML ``` <div class="grid grid-cols-[1fr_500px_2fr]"> <!-- ... --></div> ``` In situations where underscores are common but spaces are invalid, Tailwind will preserve the underscore instead of converting it to a space, for example in URLs: HTML ``` <div class="bg-[url('/what_a_rush.png')]"> <!-- ... --></div> ``` In the rare case that you actually need to use an underscore but it's ambiguous because a space is valid as well, escape the underscore with a backslash and Tailwind won't convert it to a space: HTML ``` <div class="before:content-['hello\_world']"> <!-- ... --></div> ``` If you're using something like JSX where the backslash is stripped from the rendered HTML, use [String.raw()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw) so the backslash isn't treated as a JavaScript escape character: ``` <div className={String.raw`before:content-['hello\_world']`}> <!-- ... --></div> ``` ### [Resolving ambiguities](https://tailwindcss.com/docs/adding-custom-styles#resolving-ambiguities) Many utilities in Tailwind share a common namespace but map to different CSS properties. For example `text-lg` and `text-black` both share the `text-` namespace, but one is for `font-size` and the other is for `color`. When using arbitrary values, Tailwind can generally handle this ambiguity automatically based on the value you pass in: HTML ``` <!-- Will generate a font-size utility --><div class="text-[22px]">...</div><!-- Will generate a color utility --><div class="text-[#bada55]">...</div> ``` Sometimes it really is ambiguous though, for example when using CSS variables: HTML ``` <div class="text-(--my-var)">...</div> ``` In these situations, you can "hint" the underlying type to Tailwind by adding a [CSS data type](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Types) before the value: HTML ``` <!-- Will generate a font-size utility --><div class="text-(length:--my-var)">...</div><!-- Will generate a color utility --><div class="text-(color:--my-var)">...</div> ``` ## [Using custom CSS](https://tailwindcss.com/docs/adding-custom-styles#using-custom-css) While Tailwind is designed to handle the bulk of your styling needs, there is nothing stopping you from just writing plain CSS when you need to: CSS ``` @import "tailwindcss";.my-custom-style { /* ... */} ``` ### [Adding base styles](https://tailwindcss.com/docs/adding-custom-styles#adding-base-styles) If you just want to set some defaults for the page (like the text color, background color, or font family), the easiest option is just adding some classes to the `html` or `body` elements: HTML ``` <!doctype html><html lang="en" class="bg-gray-100 font-serif text-gray-900"> <!-- ... --></html> ``` This keeps your base styling decisions in your markup alongside all of your other styles, instead of hiding them in a separate file. If you want to add your own default base styles for specific HTML elements, use the `@layer` directive to add those styles to Tailwind's `base` layer: CSS ``` @layer base { h1 { font-size: var(--text-2xl); } h2 { font-size: var(--text-xl); }} ``` ### [Adding component classes](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes) Use the `components` layer for any more complicated classes you want to add to your project that you'd still like to be able to override with utility classes. Traditionally these would be classes like `card`, `btn`, `badge` — that kind of thing. CSS ``` @layer components { .card { background-color: var(--color-white); border-radius: var(--radius-lg); padding: --spacing(6); box-shadow: var(--shadow-xl); }} ``` By defining component classes in the `components` layer, you can still use utility classes to override them when necessary: HTML ``` <!-- Will look like a card, but with square corners --><div class="card rounded-none"> <!-- ... --></div> ``` Using Tailwind you probably don't need these types of classes as often as you think. Read our guide on [managing duplication](https://tailwindcss.com/docs/styling-with-utility-classes#managing-duplication) for our recommendations. The `components` layer is also a good place to put custom styles for any third-party components you're using: CSS ``` @layer components { .select2-dropdown { /* ... */ }} ``` ### [Using variants](https://tailwindcss.com/docs/adding-custom-styles#using-variants) Use the `@variant` directive to apply a Tailwind variant within custom CSS: app.css ``` .my-element { background: white; @variant dark { background: black; }} ``` Compiled CSS ``` .my-element { background: white; @media (prefers-color-scheme: dark) { background: black; }} ``` If you need to apply multiple variants at the same time, use nesting: app.css ``` .my-element { background: white; @variant dark { @variant hover { background: black; } }} ``` Compiled CSS ``` .my-element { background: white; @media (prefers-color-scheme: dark) { &:hover { @media (hover: hover) { background: black; } } }} ``` ## [Adding custom utilities](https://tailwindcss.com/docs/adding-custom-styles#adding-custom-utilities) ### [Simple utilities](https://tailwindcss.com/docs/adding-custom-styles#simple-utilities) In addition to using the utilities that ship with Tailwind, you can also add your own custom utilities. This can be useful when there's a CSS feature you'd like to use in your project that Tailwind doesn't include utilities for out of the box. Use the `@utility` directive to add a custom utility to your project: CSS ``` @utility content-auto { content-visibility: auto;} ``` You can now use this utility in your HTML: HTML ``` <div class="content-auto"> <!-- ... --></div> ``` It will also work with variants like `hover`, `focus` and `lg`: HTML ``` <div class="hover:content-auto"> <!-- ... --></div> ``` Custom utilities are automatically inserted into the `utilities` layer along with all of the built-in utilities in the framework. ### [Complex utilities](https://tailwindcss.com/docs/adding-custom-styles#complex-utilities) If your custom utility is more complex than a single class name, use nesting to define the utility: CSS ``` @utility scrollbar-hidden { &::-webkit-scrollbar { display: none; }} ``` ### [Functional utilities](https://tailwindcss.com/docs/adding-custom-styles#functional-utilities) In addition to registering simple utilities with the `@utility` directive, you can also register functional utilities that accept an argument: CSS ``` @utility tab-* { tab-size: --value(--tab-size-*);} ``` The special `--value()` function is used to resolve the utility value. #### [Matching theme values](https://tailwindcss.com/docs/adding-custom-styles#matching-theme-values) Use the `--value(--theme-key-*)` syntax to resolve the utility value against a set of theme keys: CSS ``` @theme { --tab-size-2: 2; --tab-size-4: 4; --tab-size-github: 8;}@utility tab-* { tab-size: --value(--tab-size-*);} ``` This will match utilities like `tab-2`, `tab-4`, and `tab-github`. #### [Bare values](https://tailwindcss.com/docs/adding-custom-styles#bare-values) To resolve the value as a bare value, use the `--value({type})` syntax, where `{type}` is the data type you want to validate the bare value as: CSS ``` @utility tab-* { tab-size: --value(integer);} ``` This will match utilities like `tab-1` and `tab-76`. Available bare value data types are: `number`, `integer`, `ratio`, and `percentage`. #### [Literal values](https://tailwindcss.com/docs/adding-custom-styles#literal-values) To support literal values, use the `--value('literal')` syntax (notice the quotes): CSS ``` @utility tab-* { tab-size: --value("inherit", "initial", "unset");} ``` This will match utilities like `tab-inherit`, `tab-initial`, and `tab-unset`. #### [Arbitrary values](https://tailwindcss.com/docs/adding-custom-styles#arbitrary-values) To support arbitrary values, use the `--value([{type}])` syntax (notice the square brackets) to tell Tailwind which types are supported as an arbitrary value: CSS ``` @utility tab-* { tab-size: --value([integer]);} ``` This will match utilities like `tab-[1]` and `tab-[76]`. Available arbitrary value data types are: `absolute-size`, `angle`, `bg-size`, `color`, `family-name`, `generic-name`, `image`, `integer`, `length`, `line-width`, `number`, `percentage`, `position`, `ratio`, `relative-size`, `url`, `vector`, and `*`. #### [Supporting theme, bare, and arbitrary values together](https://tailwindcss.com/docs/adding-custom-styles#supporting-theme-bare-and-arbitrary-values-together) All three forms of the `--value()` function can be used within a rule as multiple declarations, and any declarations that fail to resolve will be omitted in the output: CSS ``` @theme { --tab-size-github: 8;}@utility tab-* { tab-size: --value([integer]); tab-size: --value(integer); tab-size: --value(--tab-size-*);} ``` This makes it possible to treat the value differently in each case if necessary, for example translating a bare integer to a percentage: CSS ``` @utility opacity-* { opacity: --value([percentage]); opacity: calc(--value(integer) * 1%); opacity: --value(--opacity-*);} ``` The `--value()` function can also take multiple arguments and resolve them left to right if you don't need to treat the return value differently in different cases: CSS ``` @theme { --tab-size-github: 8;}@utility tab-* { tab-size: --value(--tab-size-*, integer, [integer]);}@utility opacity-* { opacity: calc(--value(integer) * 1%); opacity: --value(--opacity-*, [percentage]);} ``` #### [Negative values](https://tailwindcss.com/docs/adding-custom-styles#negative-values) To support negative values, register separate positive and negative utilities into separate declarations: CSS ``` @utility inset-* { inset: --spacing(--value(integer)); inset: --value([percentage], [length]);}@utility -inset-* { inset: --spacing(--value(integer) * -1); inset: calc(--value([percentage], [length]) * -1);} ``` #### [Modifiers](https://tailwindcss.com/docs/adding-custom-styles#modifiers) Modifiers are handled using the `--modifier()` function which works exactly like the `--value()` function but operates on a modifier if present: CSS ``` @utility text-* { font-size: --value(--text-*, [length]); line-height: --modifier(--leading-*, [length], [*]);} ``` If a modifier isn't present, any declaration depending on a modifier is just not included in the output. #### [Fractions](https://tailwindcss.com/docs/adding-custom-styles#fractions) To handle fractions, we rely on the CSS `ratio` data type. If this is used with `--value()`, it's a signal to Tailwind to treat the value and modifier as a single value: CSS ``` @utility aspect-* { aspect-ratio: --value(--aspect-ratio-*, ratio, [ratio]);} ``` This will match utilities like `aspect-square`, `aspect-3/4`, and `aspect-[7/9]`. ## [Adding custom variants](https://tailwindcss.com/docs/adding-custom-styles#adding-custom-variants) In addition to using the variants that ship with Tailwind, you can also add your own custom variants using the `@custom-variant` directive: ``` @custom-variant theme-midnight { &:where([data-theme="midnight"] *) { @slot; }} ``` Now you can use the `theme-midnight:<utility>` variant in your HTML: ``` <html data-theme="midnight"> <button class="theme-midnight:bg-black ..."></button></html> ``` You can create variants using the shorthand syntax when nesting isn't required: ``` @custom-variant theme-midnight (&:where([data-theme="midnight"] *)); ``` When a custom variant has multiple rules, they can be nested within each other: ``` @custom-variant any-hover { @media (any-hover: hover) { &:hover { @slot; } }} ```
Shard59 (laksa)
Root Hash16242711095379765659
Unparsed URLcom,tailwindcss!/docs/adding-custom-styles s443