核心概念
使用工具程式在懸停、焦點等狀態下設定元素樣式。
Tailwind 中的每個工具類別都可以透過在類別名稱的開頭新增一個變體來 _有條件地_ 應用,該變體描述您要鎖定的條件。
例如,若要在懸停時套用 bg-sky-700
類別,請使用 hover:bg-sky-700
類別
將滑鼠懸停在此按鈕上以查看背景顏色變更
<button class="bg-sky-500 hover:bg-sky-700 ...">Save changes</button>
以傳統方式撰寫 CSS 時,單一類別名稱會根據目前狀態執行不同的操作
傳統上,相同的類別名稱會在懸停時套用不同的樣式
.btn-primary { background-color: #0ea5e9;}.btn-primary:hover { background-color: #0369a1;}
在 Tailwind 中,您不是將懸停狀態的樣式新增到現有的類別,而是將另一個類別新增到元素,該類別 _僅在_ 懸停時執行某些操作
在 Tailwind 中,預設狀態和懸停狀態使用不同的類別
.bg-sky-500 { background-color: #0ea5e9;}.hover\:bg-sky-700:hover { background-color: #0369a1;}
請注意 hover:bg-sky-700
如何 _僅_ 定義 :hover
狀態的樣式?預設情況下它不會執行任何操作,但只要您將滑鼠懸停在具有該類別的元素上,背景顏色就會變更為 sky-700
。
這就是我們所說的工具類別可以 _有條件地_ 套用的意思 — 透過使用變體,您可以控制您的設計在不同狀態下的行為方式,而無需離開 HTML。
Tailwind 包含了您可能需要的所有變體,包括
:hover
、:focus
、:first-child
和 :required
::before
、::after
、::placeholder
和 ::selection
prefers-reduced-motion
[dir="rtl"]
和 [open]
& > *
和 & *
這些變體甚至可以堆疊起來,以鎖定更具體的情況,例如在深色模式下,在中間斷點處,懸停時變更背景顏色
<button class="dark:md:hover:bg-fuchsia-600 ...">Save changes</button>
在本指南中,您將瞭解框架中可用的每個變體、如何將它們與自己的自訂類別搭配使用,甚至是建立自己的變體。
使用 hover
、focus
和 active
變體在懸停、焦點和作用中設定元素樣式
嘗試與此按鈕互動以查看懸停、焦點和作用中狀態
<button class="bg-violet-500 hover:bg-violet-600 focus:outline-2 focus:outline-offset-2 focus:outline-violet-500 active:bg-violet-700 ..."> Save changes</button>
Tailwind 還包含其他互動式狀態的變體,例如 :visited
、:focus-within
、:focus-visible
等。
請參閱虛擬類別參考,以取得可用虛擬類別變體的完整清單。
當元素是第一個子系或最後一個子系時,使用 first
和 last
變體設定其樣式
Kristen Ramos
kristen.ramos@example.com
Floyd Miles
floyd.miles@example.com
Courtney Henry
courtney.henry@example.com
Ted Fox
ted.fox@example.com
<ul role="list"> {#each people as person} <!-- Remove top/bottom padding when first/last child --> <li class="flex py-4 first:pt-0 last:pb-0"> <img class="h-10 w-10 rounded-full" src={person.imageUrl} alt="" /> <div class="ml-3 overflow-hidden"> <p class="text-sm font-medium text-gray-900 dark:text-white">{person.name}</p> <p class="truncate text-sm text-gray-500 dark:text-gray-400">{person.email}</p> </div> </li> {/each}</ul>
您也可以使用 odd
和 even
變體在元素是奇數或偶數子系時設定其樣式
名稱 | 職稱 | 電子郵件 |
---|---|---|
Jane Cooper | 區域範例技術員 | jane.cooper@example.com |
Cody Fisher | 產品指令負責人 | cody.fisher@example.com |
Leonard Krasner | 資深設計師 | leonard.krasner@example.com |
Emily Selman | 硬體工程副總裁 | emily.selman@example.com |
Anna Roberts | 首席策略長 | anna.roberts@example.com |
<table> <!-- ... --> <tbody> {#each people as person} <!-- Use different background colors for odd and even rows --> <tr class="odd:bg-white even:bg-gray-50 dark:odd:bg-gray-900/50 dark:even:bg-gray-950"> <td>{person.name}</td> <td>{person.title}</td> <td>{person.email}</td> </tr> {/each} </tbody></table>
使用 nth-*
和 nth-last-*
變體,根據子元素在列表中的位置來設定樣式
<div class="nth-3:underline"> <!-- ... --></div><div class="nth-last-5:underline"> <!-- ... --></div><div class="nth-of-type-4:underline"> <!-- ... --></div><div class="nth-last-of-type-6:underline"> <!-- ... --></div>
預設情況下,你可以傳入任何數字給這些變體,並使用任意值來表示更複雜的表達式,例如 nth-[2n+1_of_li]
。
Tailwind 還包含其他結構虛擬類別的變體,例如 :only-child
、:first-of-type
、:empty
等。
請參閱虛擬類別參考,以取得可用虛擬類別變體的完整清單。
使用 required
、invalid
和 disabled
等變體,為不同狀態的表單元素設定樣式
試著使電子郵件地址有效,以查看樣式的變化
<input type="text" value="tbone" disabled class="invalid:border-pink-500 invalid:text-pink-600 focus:border-sky-500 focus:outline focus:outline-sky-500 focus:invalid:border-pink-500 focus:invalid:outline-pink-500 disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500 disabled:shadow-none dark:disabled:border-gray-700 dark:disabled:bg-gray-800/20 ..."/>
使用變體來處理這類事情,可以減少範本中的條件邏輯,讓你無論輸入處於何種狀態,都可以使用同一組類別,並讓瀏覽器為你套用正確的樣式。
Tailwind 還包含其他表單狀態的變體,例如 :read-only
、:indeterminate
、:checked
等。
請參閱虛擬類別參考,以取得可用虛擬類別變體的完整清單。
使用 has-*
變體,根據元素的後代狀態或內容來設定元素的樣式
<label class="has-checked:bg-indigo-50 has-checked:text-indigo-900 has-checked:ring-indigo-200 dark:has-checked:bg-indigo-950 dark:has-checked:text-indigo-200 dark:has-checked:ring-indigo-900 ..."> <svg fill="currentColor"> <!-- ... --> </svg> Google Pay <input type="radio" class="checked:border-indigo-500 ..." /></label>
你可以將 has-*
與虛擬類別一起使用,例如 has-[:focus]
,根據其後代的狀態來設定元素的樣式。你也可以使用元素選擇器,例如 has-[img]
或 has-[a]
,根據其後代的內容來設定元素的樣式。
如果需要根據父元素的後代來設定元素的樣式,可以使用 group
類別標記父元素,並使用 group-has-*
變體來設定目標元素的樣式
很高興來到這裡。
推動像素。拋擲 div。
<div class="group ..."> <img src="..." /> <h4>Spencer Sharp</h4> <svg class="hidden group-has-[a]:block ..."><!-- ... --></svg> <p>Product Designer at <a href="...">planeteria.tech</a></p></div>
如果需要根據同層元素的後代來設定元素的樣式,可以使用 peer
類別標記同層元素,並使用 peer-has-*
變體來設定目標元素的樣式
<div> <label class="peer ..."> <input type="checkbox" name="todo[1]" checked /> Create a to do list </label> <svg class="peer-has-checked:hidden ..."><!-- ... --></svg></div>
當條件不成立時,使用 not-
變體來設定元素的樣式。
當與其他虛擬類別變體結合使用時,它特別強大,例如將 not-focus:
與 hover:
結合使用,僅在元素未被聚焦時才套用懸停樣式
試著聚焦按鈕,然後將滑鼠懸停在其上
<button class="bg-indigo-600 hover:not-focus:bg-indigo-700"> <!-- ... --></button>
你也可以將 not-
變體與媒體查詢變體結合使用,例如 forced-colors
或 supports
,僅當使用者的環境中某些情況不成立時才設定元素的樣式
<div class="not-supports-[display:grid]:flex"> <!-- ... --></div>
當你需要根據某些*父元素*的狀態來設定元素的樣式時,可以使用 group
類別標記父元素,並使用 group-*
變體(例如 group-hover
)來設定目標元素的樣式
將滑鼠懸停在卡片上,查看兩個文字元素的顏色變化
<a href="#" class="group ..."> <div> <svg class="stroke-sky-500 group-hover:stroke-white ..." fill="none" viewBox="0 0 24 24"> <!-- ... --> </svg> <h3 class="text-gray-900 group-hover:text-white ...">New project</h3> </div> <p class="text-gray-500 group-hover:text-white ...">Create a new project from a variety of starting templates.</p></a>
此模式適用於每個虛擬類別變體,例如 group-focus
、group-active
,甚至 group-odd
。
當巢狀群組時,你可以根據*特定*父群組的狀態來設定樣式,方法是使用 group/{name}
類別為該父群組提供唯一的群組名稱,並在變體中使用類別(例如 group-hover/{name}
)來包含該名稱
<ul role="list"> {#each people as person} <li class="group/item ..."> <!-- ... --> <a class="group/edit invisible group-hover/item:visible ..." href="tel:{person.phone}"> <span class="group-hover/edit:text-gray-700 ...">Call</span> <svg class="group-hover/edit:translate-x-0.5 group-hover/edit:text-gray-500 ..."><!-- ... --></svg> </a> </li> {/each}</ul>
群組可以隨意命名,不需要以任何方式設定 — 只需直接在標記中命名你的群組,Tailwind 就會自動生成必要的 CSS。
你可以透過在方括號之間提供你自己的選擇器作為任意值,來即時建立一次性的 group-*
變體
<div class="group is-published"> <div class="hidden group-[.is-published]:block"> Published </div></div>
為了獲得更多控制,你可以使用 &
字元來標記 .group
在最終選擇器中相對於你傳入的選擇器應該出現的位置
<div class="group"> <div class="group-[:nth-of-type(3)_&]:block"> <!-- ... --> </div></div>
in-*
變體的工作方式與 group
類似,但你不需要將 group
新增至父元素
<div tabindex="0" class="group"> <div class="opacity-50 group-focus:opacity-100"> <div tabindex="0"> <div class="opacity-50 in-focus:opacity-100"> <!-- ... --> </div> </div> </div></div>
in-*
變體會回應任何父元素中的狀態變更,因此如果你想要更精細的控制,則需要改用 group
。
當你需要根據*同層*元素的狀態來設定元素的樣式時,可以使用 peer
類別標記同層元素,並使用 peer-*
變體(例如 peer-invalid
)來設定目標元素的樣式
試著使電子郵件地址有效,以查看警告消失
<form> <label class="block"> <span class="...">Email</span> <input type="email" class="peer ..." /> <p class="invisible peer-invalid:visible ...">Please provide a valid email address.</p> </label></form>
這使得可以執行各種巧妙的技巧,例如 浮動標籤,而無需任何 JS。
此模式適用於每個虛擬類別變體,例如 peer-focus
、peer-required
和 peer-disabled
。
請務必注意,由於 CSS 中後續同層組合符號的工作方式,peer
標記只能用於*先前*的同層元素
無法運作,只有先前的同層元素才能標記為 peers
<label> <span class="peer-invalid:text-red-500 ...">Email</span> <input type="email" class="peer ..." /></label>
當使用多個 peers 時,你可以透過使用 peer/{name}
類別為該 peer 提供唯一的名稱,並在變體中使用類別(例如 peer-checked/{name}
)來包含該名稱,以根據*特定* peer 的狀態來設定某些內容的樣式
<fieldset> <legend>Published status</legend> <input id="draft" class="peer/draft" type="radio" name="status" checked /> <label for="draft" class="peer-checked/draft:text-sky-500">Draft</label> <input id="published" class="peer/published" type="radio" name="status" /> <label for="published" class="peer-checked/published:text-sky-500">Published</label> <div class="hidden peer-checked/draft:block">Drafts are only visible to administrators.</div> <div class="hidden peer-checked/published:block">Your post will be publicly visible on your site.</div></fieldset>
Peers 可以隨意命名,不需要以任何方式設定 — 只需直接在標記中命名你的 peers,Tailwind 就會自動生成必要的 CSS。
你可以透過在方括號之間提供你自己的選擇器作為任意值,來即時建立一次性的 peer-*
變體
<form> <label for="email">Email:</label> <input id="email" name="email" type="email" class="is-dirty peer" required /> <div class="peer-[.is-dirty]:peer-required:block hidden">This field is required.</div> <!-- ... --></form>
為了獲得更多控制,你可以使用 &
字元來標記 .peer
在最終選擇器中相對於你傳入的選擇器應該出現的位置
<div> <input type="text" class="peer" /> <div class="hidden peer-[:nth-of-type(3)_&]:block"> <!-- ... --> </div></div>
使用 before
和 after
變體來設定 ::before
和 ::after
虛擬元素的樣式
<label> <span class="text-gray-700 after:ml-0.5 after:text-red-500 after:content-['*'] ...">Email</span> <input type="email" name="email" class="..." placeholder="you@example.com" /></label>
使用這些變體時,Tailwind 預設會自動新增 content: ''
,因此除非你想要不同的值,否則你無需指定它
當你看起來 不高興 總是,人們會認為你很忙。
<blockquote class="text-center text-2xl font-semibold text-gray-900 italic dark:text-white"> When you look <span class="relative inline-block before:absolute before:-inset-1 before:block before:-skew-y-3 before:bg-pink-500"> <span class="relative text-white dark:text-gray-950">annoyed</span> </span> all the time, people think that you're busy.</blockquote>
值得注意的是,在 Tailwind 專案中,你實際上並不需要 ::before
和 ::after
虛擬元素來處理大多數事情 — 通常,使用真正的 HTML 元素會更簡單。
例如,這是與上述相同的設計,但使用 <span>
而不是 ::before
虛擬元素,這樣更容易閱讀,而且實際上程式碼更少
<blockquote class="text-center text-2xl font-semibold text-gray-900 italic"> When you look <span class="relative"> <span class="absolute -inset-1 block -skew-y-3 bg-pink-500" aria-hidden="true"></span> <span class="relative text-white">annoyed</span> </span> all the time, people think that you're busy.</blockquote>
將 before
和 after
保留給虛擬元素的內容實際上不在 DOM 中且無法讓使用者選取的重要情況。
使用 placeholder
變體來設定任何輸入框或文字區域的預留位置文字樣式
<input class="placeholder:text-gray-500 placeholder:italic ..." placeholder="Search for anything..." type="text" name="search"/>
使用 file
變體來設定檔案輸入按鈕的樣式
<input type="file" class="file:mr-4 file:rounded-full file:border-0 file:bg-violet-50 file:px-4 file:py-2 file:text-sm file:font-semibold file:text-violet-700 hover:file:bg-violet-100 dark:file:bg-violet-600 dark:file:text-violet-100 ..."/>
使用 marker
變體來設定清單中的計數器或項目符號樣式
<ul role="list" class="list-disc marker:text-sky-400 ..."> <li>5 cups chopped Porcini mushrooms</li> <li>1/2 cup of olive oil</li> <li>3lb of celery</li></ul>
我們將 marker
變體設計為可繼承的,因此儘管您可以直接在 <li>
元素上使用它,您也可以在父元素上使用它,以避免重複自己。
使用 selection
變體設定作用中文字選取的樣式
嘗試用滑鼠選取一些這段文字
所以我開始走進水裡。我不會對你們這些傢伙說謊,我當時嚇壞了。但我還是繼續前進,當我越過浪濤時,一種奇怪的平靜降臨到我身上。我不知道這是神明的旨意,還是所有生物之間的親密關係,但我告訴你,傑瑞,在那一刻,我就是一位海洋生物學家。
<div class="selection:bg-fuchsia-300 selection:text-fuchsia-900"> <p> So I started to walk into the water. I won't lie to you boys, I was terrified. But I pressed on, and as I made my way past the breakers a strange calm came over me. I don't know if it was divine intervention or the kinship of all living things but I tell you Jerry at that moment, I <em>was</em> a marine biologist. </p></div>
我們將 selection
變體設計為可繼承的,因此您可以將其新增到樹狀結構中的任何位置,它將會應用於所有後代元素。
這使得在整個網站上設定與您品牌相符的選取顏色變得容易
<html> <head> <!-- ... --> </head> <body class="selection:bg-pink-300"> <!-- ... --> </body></html>
使用 first-line
變體設定內容區塊中第一行的樣式,並使用 first-letter
變體設定第一個字母的樣式
好吧,讓我告訴你一些事情,滑稽的傢伙。你知道那個小印章,上面寫著「紐約公共圖書館」嗎?這對你來說可能不算什麼,但對我來說意義重大。意義非常重大。
當然,你想笑就笑吧。我以前見過你這種人:浮誇、愛出風頭、炫耀傳統。是啊,我知道你在想什麼。這個傢伙為什麼要為舊圖書館的書大驚小怪?好吧,讓我給你一個提示,年輕人。
<div class="text-gray-700"> <p class="first-letter:float-left first-letter:mr-3 first-letter:text-7xl first-letter:font-bold first-letter:text-gray-900 first-line:tracking-widest first-line:uppercase" > Well, let me tell you something, funny boy. Y'know that little stamp, the one that says "New York Public Library"? </p> <p class="mt-6">Well that may not mean anything to you, but that means a lot to me. One whole hell of a lot.</p></div>
使用 backdrop
變體設定 原生 <dialog>
元素的背景樣式
<dialog class="backdrop:bg-gray-50"> <form method="dialog"> <!-- ... --> </form></dialog>
如果您在專案中使用原生 <dialog>
元素,您可能也想閱讀有關使用 open
變體來設定開啟/關閉狀態樣式的內容。
若要在特定斷點設定元素的樣式,請使用 md
和 lg
等響應式變體。
例如,這會在行動裝置上呈現 3 欄格線,在中等寬度的螢幕上呈現 4 欄格線,在大型螢幕上呈現 6 欄格線
<div class="grid grid-cols-3 md:grid-cols-4 lg:grid-cols-6"> <!-- ... --></div>
若要根據父元素的寬度而非視窗設定元素的樣式,請使用 @md
和 @lg
等變體
<div class="@container"> <div class="flex flex-col @md:flex-row"> <!-- ... --> </div></div>
請參閱響應式設計文件,深入瞭解這些功能如何運作。
prefers-color-scheme
媒體查詢會告訴您使用者是否偏好淺色主題或深色主題,且通常會在作業系統層級進行設定。
使用沒有變體的公用程式來指定淺色模式,並使用 dark
變體來提供深色模式的覆寫
淺色模式
零重力筆可以用於任何方向書寫,包括倒著寫。它甚至可以在外太空工作。
深色模式
零重力筆可以用於任何方向書寫,包括倒著寫。它甚至可以在外太空工作。
<div class="bg-white dark:bg-gray-900 ..."> <!-- ... --> <h3 class="text-gray-900 dark:text-white ...">Writes upside-down</h3> <p class="text-gray-500 dark:text-gray-400 ..."> The Zero Gravity Pen can be used to write in any orientation, including upside-down. It even works in outer space. </p></div>
請參閱深色模式文件,深入瞭解此功能如何運作。
prefers-reduced-motion
媒體查詢會告訴您使用者是否要求您將非必要的動作降至最低。
當使用者要求減少動作時,請使用 motion-reduce
變體來有條件地新增樣式
嘗試在開發人員工具中模擬 `prefers-reduced-motion: reduce` 以隱藏旋轉器
<button type="button" class="bg-indigo-500 ..." disabled> <svg class="animate-spin motion-reduce:hidden ..." viewBox="0 0 24 24"><!-- ... --></svg> Processing...</button>
Tailwind 也包含 motion-safe
變體,只有在使用者未要求減少動作時才會新增樣式。當使用 motion-reduce
協助程式會導致必須「復原」大量樣式時,這會很有用
<!-- Using `motion-reduce` can mean lots of "undoing" styles --><button class="transition hover:-translate-y-0.5 motion-reduce:transition-none motion-reduce:hover:translate-y-0 ..."> Save changes</button><!-- Using `motion-safe` is less code in these situations --><button class="motion-safe:transition motion-safe:hover:-translate-x-0.5 ...">Save changes</button>
prefers-contrast
媒體查詢會告訴您使用者是否要求較多或較少的對比。
當使用者要求更多對比時,請使用 contrast-more
變體來有條件地新增樣式
嘗試在開發人員工具中模擬 `prefers-contrast: more` 以查看變更
<label class="block"> <span class="block text-sm font-medium text-gray-700">Social Security Number</span> <input class="border-gray-200 placeholder-gray-400 contrast-more:border-gray-400 contrast-more:placeholder-gray-500 ..." /> <p class="text-gray-600 opacity-10 contrast-more:opacity-100 ...">We need this to steal your identity.</p></label>
Tailwind 也包含 contrast-less
變體,您可以使用它在使用者要求較少對比時有條件地新增樣式。
forced-colors
媒體查詢會指出使用者是否正在使用強制色彩模式。這些模式會覆寫您網站的色彩,並使用使用者定義的文字、背景、連結和按鈕調色盤。
當使用者啟用強制色彩模式時,請使用 forced-colors
變體來有條件地新增樣式
嘗試在開發人員工具中模擬 `forced-colors: active` 以查看變更
<label> <input type="radio" class="appearance-none forced-colors:appearance-auto" /> <p class="hidden forced-colors:block">Cyan</p> <div class="bg-cyan-200 forced-colors:hidden ..."></div> <div class="bg-cyan-500 forced-colors:hidden ..."></div></label>
當使用者未使用強制色彩模式時,請使用 not-forced-colors
變體來套用樣式
<div class="not-forced-colors:appearance-none ..."> <!-- ... --></div>
Tailwind 也包含 強制色彩調整公用程式,可用於選擇加入和退出強制色彩。
當視窗處於特定方向時,請使用 portrait
和 landscape
變體來有條件地新增樣式
<div> <div class="portrait:hidden"> <!-- ... --> </div> <div class="landscape:hidden"> <p>This experience is designed to be viewed in landscape. Please rotate your device to view the site.</p> </div></div>
請使用 print
變體來有條件地新增僅在列印文件時才套用的樣式
<div> <article class="print:hidden"> <h1>My Secret Pizza Recipe</h1> <p>This recipe is a secret, and must not be shared with anyone</p> <!-- ... --> </article> <div class="hidden print:block">Are you seriously trying to print this? It's secret!</div></div>
請使用 supports-[...]
變體根據使用者的瀏覽器是否支援特定功能來設定樣式
<div class="flex supports-[display:grid]:grid ..."> <!-- ... --></div>
在幕後,supports-[...]
變體會產生 @supports 規則
,並將您與 @supports (...)
一起使用的任何內容置於方括號之間,例如屬性/值配對,甚至使用 and
和 or
的運算式。
為了簡潔起見,如果您只需要檢查是否支援屬性 (而不是特定值),您可以只指定屬性名稱
<div class="bg-black/75 supports-backdrop-filter:bg-black/25 supports-backdrop-filter:backdrop-blur ..."> <!-- ... --></div>
請使用 not-supports-[...]
變體,根據使用者的瀏覽器是否不支援特定功能來設定樣式
<div class="not-supports-[display:grid]:flex"> <!-- ... --></div>
您可以在 supports-*
命名空間中建立新變體,以設定專案中正在使用的常見 @supports
規則的快捷方式
@variant supports-grid { @supports (display: grid) { @slot; }}
然後您可以在專案中使用這些自訂的 supports-*
變體
<div class="supports-grid:grid"> <!-- ... --></div>
當元素首次在 DOM 中呈現時,或從 display: none
轉換為可見時,請使用 starting
變體來設定元素的外觀
<div> <button popovertarget="my-popover">Check for updates</button> <div popover id="my-popover" class="opacity-0 starting:open:opacity-0 ..."> <!-- ... --> </div></div>
請使用 aria-*
變體根據 ARIA 屬性來有條件地設定樣式。
例如,若要在 aria-checked
屬性設定為 true
時套用 bg-sky-700
類別,請使用 aria-checked:bg-sky-700
類別
<div aria-checked="true" class="bg-gray-600 aria-checked:bg-sky-700"> <!-- ... --></div>
依預設,我們已包含最常見的布林值 ARIA 屬性的變體
變體 | CSS |
---|---|
aria-busy | &[aria-busy="true"] |
aria-checked | &[aria-checked="true"] |
aria-disabled | &[aria-disabled="true"] |
aria-expanded | &[aria-expanded="true"] |
aria-hidden | &[aria-hidden="true"] |
aria-pressed | &[aria-pressed="true"] |
aria-readonly | &[aria-readonly="true"] |
aria-required | &[aria-required="true"] |
aria-selected | &[aria-selected="true"] |
您可以透過建立新的變體來自訂可用的 aria-*
變體。
@variant aria-asc (&[aria-sort="ascending"]);@variant aria-desc (&[aria-sort="descending"]);
如果您需要使用一次性的 aria
變體,該變體不適合包含在您的專案中,或者對於需要特定值的更複雜的 ARIA 屬性,請使用方括號來動態產生具有任何任意值的屬性。
發票編號 | 客戶 | 金額 |
---|---|---|
#100 | Pendant Publishing | $2,000.00 |
#101 | Kruger Industrial Smoothing | $545.00 |
#102 | J. Peterman | $10,000.25 |
<table> <thead> <tr> <th aria-sort="ascending" class="aria-[sort=ascending]:bg-[url('/img/down-arrow.svg')] aria-[sort=descending]:bg-[url('/img/up-arrow.svg')]" > Invoice # </th> <!-- ... --> </tr> </thead> <!-- ... --></table>
ARIA 狀態變體也可以使用 group-aria-*
和 peer-aria-*
變體來定位父元素和兄弟元素。
<table> <thead> <tr> <th aria-sort="ascending" class="group"> Invoice # <svg class="group-aria-[sort=ascending]:rotate-0 group-aria-[sort=descending]:rotate-180"><!-- ... --></svg> </th> <!-- ... --> </tr> </thead> <!-- ... --></table>
使用 data-*
變體根據 data 屬性 有條件地套用樣式。
要檢查 data 屬性是否存在(而不是特定值),您可以僅指定屬性名稱。
<!-- Will apply --><div data-active class="border border-gray-300 data-active:border-purple-500"> <!-- ... --></div><!-- Will not apply --><div class="border border-gray-300 data-active:border-purple-500"> <!-- ... --></div>
如果您需要檢查特定值,則可以使用任意值。
<!-- Will apply --><div data-size="large" class="data-[size=large]:p-8"> <!-- ... --></div><!-- Will not apply --><div data-size="medium" class="data-[size=large]:p-8"> <!-- ... --></div>
或者,您可以透過在 data-*
命名空間中建立新的變體,來設定專案中使用的常見 data 屬性的快捷方式。
@import "tailwindcss";@variant data-checked (&[data-ui~="checked"]);
然後您可以在專案中使用這些自訂的 data-*
變體。
<div data-ui="checked active" class="data-checked:underline"> <!-- ... --></div>
在建構多方向佈局時,分別使用 rtl
和 ltr
變體,以在從右到左和從左到右模式下有條件地新增樣式。
從左到右
Tom Cook
營運總監
從右到左
تامر كرم
執行長
<div class="group flex items-center"> <img class="h-12 w-12 shrink-0 rounded-full" src="..." alt="" /> <div class="ltr:ml-3 rtl:mr-3"> <p class="text-gray-700 group-hover:text-gray-900 ...">...</p> <p class="text-gray-500 group-hover:text-gray-700 ...">...</p> </div></div>
請記住,這些變體僅在您建構需要同時支援從左到右和從右到左佈局的網站時才有用。如果您建構的網站僅需支援單一方向,則不需要這些變體,只需套用對您的內容有意義的樣式即可。
當 <details>
或 <dialog>
元素處於開啟狀態時,使用 open
變體來有條件地新增樣式。
嘗試切換揭露內容以查看樣式變化
馬克杯是圓的。罐子是圓的。他們應該稱之為 Roundtine。
<details class="border border-transparent open:border-black/10 open:bg-gray-100 ..." open> <summary class="text-sm leading-6 font-semibold text-gray-900 select-none">Why do they call it Ovaltine?</summary> <div class="mt-3 text-sm leading-6 text-gray-600"> <p>The mug is round. The jar is round. They should call it Roundtine.</p> </div></details>
此變體也針對 popover 的 :popover-open
偽類。
<div> <button popovertarget="my-popover">Open Popover</button> <div popover id="my-popover" class="opacity-0 open:opacity-100 ..."> <!-- ... --> </div></div>
inert
變體可讓您樣式化標記有 inert
屬性的元素。
<form> <legend>Notification preferences</legend> <fieldset> <input type="radio" /> <label> Custom </label> <fieldset inert class="inert:opacity-50"> <!-- ... --> </fieldset> <input type="radio" /> <label> Everything </label> </fieldset></form>
這對於新增視覺提示很有用,這些提示可清楚表明內容的某些部分不是互動式的。
雖然通常最好將實用類別直接放在子元素上,但您可以在需要樣式化您無法控制的直接子項的情況下使用 *
變體。
<div> <h2>Categories<h2> <ul class="*:rounded-full *:border *:border-sky-100 *:bg-sky-50 *:px-2 *:py-0.5 dark:text-sky-300 dark:*:border-sky-500/15 dark:*:bg-sky-500/10 ..."> <li>Sales</li> <li>Marketing</li> <li>SEO</li> <!-- ... --> </ul></div>
請務必注意,由於產生的子選擇器的特殊性,使用子項本身上的實用程式直接覆寫樣式將無法運作。
無法運作,子項無法覆寫自己的樣式。
<ul class="*:bg-sky-50 ..."> <li class="bg-red-50 ...">Sales</li> <li>Marketing</li> <li>SEO</li> <!-- ... --></ul>
與 *
類似,**
變體可用於樣式化元素的子項。主要區別在於 **
會將樣式套用到所有後代,而不僅僅是直接子項。當您將其與另一個變體結合以縮小您所選取的內容時,這特別有用。
<ul class="**:data-avatar:size-12 **:data-avatar:rounded-full ..."> {#each items as item} <li> <img src={item.src} data-avatar /> <p>{item.name}</p> </li> {/each}</ul>
就像 任意值 讓您將自訂值與實用類別一起使用一樣,任意變體可讓您直接在 HTML 中編寫自訂選擇器變體。
任意變體只是代表選擇器的格式字串,用方括號括起來。例如,當元素具有 is-dragging
類別時,此任意變體會將游標變更為 grabbing
。
<ul role="list"> {#each items as item} <li class="[&.is-dragging]:cursor-grabbing">{item}</li> {/each}</ul>
任意變體可以與內建變體或彼此堆疊,就像 Tailwind 中的其他變體一樣。
<ul role="list"> {#each items as item} <li class="[&.is-dragging]:active:cursor-grabbing">{item}</li> {/each}</ul>
如果您的選擇器中需要空格,可以使用底線。例如,此任意變體會選取您新增類別的元素內的所有 p
元素。
<div class="[&_p]:mt-4"> <p>Lorem ipsum...</p> <ul> <li> <p>Lorem ipsum...</p> </li> <!-- ... --> </ul></div>
您還可以在任意變體中使用 @media
或 @supports
之類的 at-rule。
<div class="flex [@supports(display:grid)]:grid"> <!-- ... --></div>
使用 at-rule 自訂變體時,&
預留位置不是必要的,就像使用預處理器進行巢狀結構一樣。
如果您發現自己在專案中多次使用相同的任意變體,則可能值得建立自訂變體。
@variant pointer-coarse { @media (pointer: coarse) { @slot; }}
現在您可以在 HTML 中使用 pointer-coarse:<utility>
變體。
<button class="pointer-coarse:size-12 ..."></button>
當不需要巢狀結構時,您可以使用簡寫語法建立變體。
@variant pointer-coarse (@media (pointer: coarse));
當自訂變體有多個規則時,它們可以彼此巢狀。
@variant any-hover { @media (any-hover: hover) { &:hover { @slot; } }}
預設包含在 Tailwind 中的每個變體的快速參考表。
變體 | CSS |
---|---|
hover | @media (hover: hover){ &:hover } |
focus | &:focus |
focus-within | &:focus-within |
focus-visible | &:focus-visible |
active | &:active |
visited | &:visited |
target | &:target |
* | :is(& > *) |
** | :is(& *) |
has-[...] | &:has(...) |
group-[...] | &:is(:where(.group)... *) |
peer-[...] | &:is(:where(.peer)... ~ *) |
in-[...] | :where(...) & |
not-[...] | &:not(...) |
inert | &:is([inert], [inert] *) |
first | &:first-child |
last | &:last-child |
only | &:only-child |
odd | &:nth-child(odd) |
even | &:nth-child(even) |
first-of-type | &:first-of-type |
last-of-type | &:last-of-type |
only-of-type | &:only-of-type |
nth-[...] | &:nth-child(...) |
nth-last-[...] | &:nth-last-child(...) |
nth-of-type-[...] | &:nth-of-type(...) |
nth-last-of-type-[...] | &:nth-last-of-type(...) |
empty | &:empty |
disabled | &:disabled |
enabled | &:enabled |
checked | &:checked |
indeterminate | &:indeterminate |
default | &:default |
optional | &:optional |
required | &:required |
valid | &:valid |
invalid | &:invalid |
in-range | &:in-range |
out-of-range | &:out-of-range |
placeholder-shown | &:placeholder-shown |
autofill | &:autofill |
read-only | &:read-only |
before | &::before |
after | &::after |
first-letter | &::first-letter |
first-line | &::first-line |
marker | &::marker, & *::marker |
selection | &::selection |
file | &::file-selector-button |
backdrop | &::backdrop |
placeholder | &::placeholder |
sm | @media (width>=40rem) |
md | @media (width>=48rem) |
lg | @media (width>=64rem) |
xl | @media (width>=80rem) |
2xl | @media (width>=96rem) |
min-[...] | @media (width>= ...) |
max-sm | @media (width<40rem) |
max-md | @media (width<48rem) |
max-lg | @media (width<64rem) |
max-xl | @media (width<80rem) |
max-2xl | @media (width<96rem) |
max-[...] | @media (width< ...) |
@3xs | @container (width>=16rem) |
@2xs | @container (width>=18rem) |
@xs | @container (width>=20rem) |
@sm | @container (width>=24rem) |
@md | @container (width>=28rem) |
@lg | @container (width>=32rem) |
@xl | @container (width>=36rem) |
@2xl | @container (width>=42rem) |
@3xl | @container (width>=48rem) |
@4xl | @container (width>=56rem) |
@5xl | @container (width>=64rem) |
@6xl | @container (width>=72rem) |
@7xl | @container (width>=80rem) |
@min-[...] | @container (width>= ...) |
@max-3xs | @container (width<16rem) |
@max-2xs | @container (width<18rem) |
@max-xs | @container (width<20rem) |
@max-sm | @container (width<24rem) |
@max-md | @container (width<28rem) |
@max-lg | @container (width<32rem) |
@max-xl | @container (width<36rem) |
@max-2xl | @container (width<42rem) |
@max-3xl | @container (width<48rem) |
@max-4xl | @container (width<56rem) |
@max-5xl | @container (width<64rem) |
@max-6xl | @container (width<72rem) |
@max-7xl | @container (width<80rem) |
@max-[...] | @container (width< ...) |
dark | @media (prefers-color-scheme: dark) |
portrait | @media (orientation: portrait) |
landscape | @media (orientation: landscape) |
motion-safe | @media (prefers-reduced-motion: no-preference) |
motion-reduce | @media (prefers-reduced-motion: reduce) |
contrast-more | @media (prefers-contrast: more) |
contrast-less | @media (prefers-contrast: less) |
@media print | |
supports-[…] | @supports (…) |
aria-busy | &[aria-busy="true"] |
aria-checked | &[aria-checked="true"] |
aria-disabled | &[aria-disabled="true"] |
aria-expanded | &[aria-expanded="true"] |
aria-hidden | &[aria-hidden="true"] |
aria-pressed | &[aria-pressed="true"] |
aria-readonly | &[aria-readonly="true"] |
aria-required | &[aria-required="true"] |
aria-selected | &[aria-selected="true"] |
aria-[…] | &[aria-…] |
data-[...] | &[data-...] |
rtl | [dir="rtl"] & |
ltr | [dir="ltr"] & |
open | &:is([open], :popover-open) |
forced-colors | @media (forced-colors: active) |
starting | @starting-style |
這是一個包含 Tailwind 中所有偽類別變體範例的完整清單,作為本指南開頭的偽類別文件的補充。
當使用者使用滑鼠游標懸停在元素上時,使用 hover
變體來設定元素樣式
<div class="bg-black hover:bg-white ..."> <!-- ... --></div>
當元素獲得焦點時,使用 focus
變體來設定元素樣式
<input class="border-gray-300 focus:border-blue-400 ..." />
當元素或其後代之一獲得焦點時,使用 focus-within
變體來設定元素樣式
<div class="focus-within:shadow-lg ..."> <input type="text" /></div>
當元素使用鍵盤獲得焦點時,使用 focus-visible
變體來設定元素樣式
<button class="focus-visible:outline-2 ...">Submit</button>
當元素被按下時,使用 active
變體來設定元素樣式
<button class="bg-blue-500 active:bg-blue-600 ...">Submit</button>
當連結已經被瀏覽過時,使用 visited
變體來設定連結樣式
<a href="https://seinfeldquotes.com" class="text-blue-600 visited:text-purple-600 ..."> Inspiration </a>
如果元素的 ID 與目前的 URL 片段匹配,則使用 target
變體來設定元素樣式
<div id="about" class="target:shadow-lg ..."> <!-- ... --></div>
如果元素是第一個子元素,則使用 first
變體來設定元素樣式
<ul> {#each people as person} <li class="py-4 first:pt-0 ..."> <!-- ... --> </li> {/each}</ul>
如果元素是最後一個子元素,則使用 last
變體來設定元素樣式
<ul> {#each people as person} <li class="py-4 last:pb-0 ..."> <!-- ... --> </li> {/each}</ul>
如果元素是唯一的子元素,則使用 only
變體來設定元素樣式
<ul> {#each people as person} <li class="py-4 only:py-0 ..."> <!-- ... --> </li> {/each}</ul>
如果元素是奇數編號的子元素,則使用 odd
變體來設定元素樣式
<table> {#each people as person} <tr class="bg-white odd:bg-gray-100 ..."> <!-- ... --> </tr> {/each}</table>
如果元素是偶數編號的子元素,則使用 even
變體來設定元素樣式
<table> {#each people as person} <tr class="bg-white even:bg-gray-100 ..."> <!-- ... --> </tr> {/each}</table>
如果元素是其類型中的第一個子元素,則使用 first-of-type
變體來設定元素樣式
<nav> <img src="/logo.svg" alt="Vandelay Industries" /> {#each links as link} <a href="#" class="ml-2 first-of-type:ml-6 ..."> <!-- ... --> </a> {/each}</nav>
如果元素是其類型中的最後一個子元素,則使用 last-of-type
變體來設定元素樣式
<nav> <img src="/logo.svg" alt="Vandelay Industries" /> {#each links as link} <a href="#" class="mr-2 last-of-type:mr-6 ..."> <!-- ... --> </a> {/each} <button>More</button></nav>
如果元素是其類型中唯一的子元素,則使用 only-of-type
變體來設定元素樣式
<nav> <img src="/logo.svg" alt="Vandelay Industries" /> {#each links as link} <a href="#" class="mx-2 only-of-type:mx-6 ..."> <!-- ... --> </a> {/each} <button>More</button></nav>
使用 nth
變體來設定特定位置的元素樣式
<nav> <img src="/logo.svg" alt="Vandelay Industries" /> {#each links as link} <a href="#" class="mx-2 nth-3:mx-6 nth-[3n+1]:mx-7 ..."> <!-- ... --> </a> {/each} <button>More</button></nav>
使用 nth-last
變體來設定從最後一個位置開始的特定位置的元素樣式
<nav> <img src="/logo.svg" alt="Vandelay Industries" /> {#each links as link} <a href="#" class="mx-2 nth-last-3:mx-6 nth-last-[3n+1]:mx-7 ..."> <!-- ... --> </a> {/each} <button>More</button></nav>
使用 nth-of-type
變體來設定相同類型中特定位置的元素樣式
<nav> <img src="/logo.svg" alt="Vandelay Industries" /> {#each links as link} <a href="#" class="mx-2 nth-of-type-3:mx-6 nth-of-type-[3n+1]:mx-7 ..."> <!-- ... --> </a> {/each} <button>More</button></nav>
使用 nth-last-of-type
變體來設定從最後一個位置開始的相同類型中特定位置的元素樣式
<nav> <img src="/logo.svg" alt="Vandelay Industries" /> {#each links as link} <a href="#" class="mx-2 nth-last-of-type-3:mx-6 nth-last-of-type-[3n+1]:mx-7 ..."> <!-- ... --> </a> {/each} <button>More</button></nav>
如果元素沒有內容,則使用 empty
變體來設定元素樣式
<ul> {#each people as person} <li class="empty:hidden ...">{person.hobby}</li> {/each}</ul>
當輸入被禁用時,使用 disabled
變體來設定輸入的樣式
<input class="disabled:opacity-75 ..." />
當輸入啟用時,使用 enabled
變體來設定輸入的樣式,當您只想在元素未被禁用時應用其他樣式時,這最有用
<input class="enabled:hover:border-gray-400 disabled:opacity-75 ..." />
當選中複選框或單選按鈕時,使用 checked
變體來設定樣式
<input type="checkbox" class="appearance-none checked:bg-blue-500 ..." />
使用 indeterminate
變體來設定處於不確定狀態的複選框或單選按鈕的樣式
<input type="checkbox" class="appearance-none indeterminate:bg-gray-300 ..." />
使用 default
變體來設定選項、複選框或單選按鈕在頁面初始載入時的預設值樣式
<input type="checkbox" class="default:outline-2 ..." />
當輸入為選填時,使用 optional
變體來設定輸入的樣式
<input class="optional:border-red-500 ..." />
當輸入為必填時,使用 required
變體來設定輸入的樣式
<input class="required:border-red-500 ..." />
當輸入有效時,使用 valid
變體來設定輸入的樣式
<input class="valid:border-green-500 ..." />
當輸入無效時,使用 invalid
變體來設定輸入的樣式
<input class="invalid:border-red-500 ..." />
當輸入的值在指定的範圍限制內時,使用 in-range
變體來設定輸入的樣式
<input min="1" max="5" class="in-range:border-green-500 ..." />
使用 out-of-range
變體,在輸入值超出指定的範圍限制時,設定其樣式
<input min="1" max="5" class="out-of-range:border-red-500 ..." />
使用 placeholder-shown
變體,在顯示 placeholder 時,設定輸入框的樣式
<input class="placeholder-shown:border-gray-500 ..." placeholder="you@example.com" />
使用 autofill
變體,在輸入框被瀏覽器自動填入內容時,設定其樣式
<input class="autofill:bg-yellow-200 ..." />
使用 read-only
變體,在輸入框為唯讀時,設定其樣式
<input class="read-only:bg-gray-100 ..." />