首頁 > web前端 > js教程 > 主體

如何將上下文變數與 Tailwind CSS 類別名稱一起使用?

Barbara Streisand
發布: 2024-11-14 16:27:02
原創
846 人瀏覽過

How to Use Context Variables with Tailwind CSS Class Names?

Dynamic Class Names with Tailwind CSS

When utilizing context variables in React, it's common to want to pass these values as class names to Tailwind CSS. However, this approach may encounter limitations.

Tailwind CSS Naming Conventions

According to Tailwind CSS documentation, it only interprets complete unbroken strings as valid class names. Dynamic class names constructed through interpolation or concatenation will not be recognized.

Solution:

  • Use Complete Class Names: Define full class names in your ThemeContext, ensuring they exist as a single string.
  • Example:

    const colors = {
    // ...
    secondary: darkTheme ? "bg-[#FFFFFF]" : "bg-[#FFFFFF]",
    // ...
    };
    登入後複製
  • Use Inline Style Attribute:

    <p className="text-text-white">
    登入後複製

    By adhering to these solutions, you can effectively incorporate context variables into your Tailwind CSS class names.

以上是如何將上下文變數與 Tailwind CSS 類別名稱一起使用?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板