> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# 样式


Lynx 支持通过 Rsbuild 接入 Web 生态常见的各类样式工具。你可以直接使用 Rsbuild 配置，包括：

- 
  [**CSS
  Modules**](https://rsbuild.rs/zh/guide/styling/css-modules)：创建具有局部作用域的
  CSS 类，以避免命名冲突并提高可维护性。
- 
  [**Global CSS**](https://rsbuild.rs/zh/guide/styling/css-usage)：使用全局
  CSS，沿用传统 CSS 的编写方式；随着应用增长，需要注意样式体积和全局规则的管理。
- 
  [**CSS
  预处理器**](https://rsbuild.rs/zh/guide/styling/css-usage#css-预处理器)：使用
  Sass、Less 等预处理器，通过变量、嵌套规则等特性扩展 CSS。
- 
  [**PostCSS**](https://rsbuild.rs/zh/guide/styling/css-usage#postcss)：通过插件转换
  CSS。
- [**Tailwind CSS**](#tailwind-css)：通过组合工具类（utility classes）快速进行自定义设计。

这些工具处理样式的编写和转换，最终可使用的 CSS 能力仍由 Lynx 决定，见[应用样式](/next/zh/guide/ui/styling.md)。CSS 会被解析并[编码进 Bundle](/next/zh/guide/build/index.md#what-is-inside-a-bundle)。组件作用域、继承和样式重新匹配分别由 [`enableRemoveCSSScope`](/next/zh/api/packages/react-rsbuild-plugin/interfaces/PluginReactLynxOptions.md#enableremovecssscope)、[`enableCSSInheritance`](/next/zh/api/packages/react-rsbuild-plugin/interfaces/PluginReactLynxOptions.md#enablecssinheritance) 和 [`enableCSSInvalidation`](/next/zh/api/packages/react-rsbuild-plugin/interfaces/PluginReactLynxOptions.md#enablecssinvalidation) 控制。

## 在 Lynx 中使用 Tailwind CSS \{#tailwind-css}

使用 Tailwind CSS 时，需要 [`@lynx-js/tailwind-preset`](https://www.npmjs.com/package/@lynx-js/tailwind-preset) 过滤 Lynx 不支持的工具类，并适配需要不同实现的部分。其余用法沿用 [Rsbuild 的 Tailwind CSS 指南](https://rsbuild.rs/zh/guide/styling/tailwindcss-v3)。

:::tip 使用 Tailwind CSS v3
`@lynx-js/tailwind-preset` 需要 **Tailwind CSS v3**，以 PostCSS 插件的形式使用。Tailwind v4 的 `@tailwindcss/postcss`、`@tailwindcss/vite` 和 `@tailwindcss/cli` 不适用于这个 preset。
:::

安装 Tailwind CSS 和 Lynx preset：


```sh [npm]
npm install -D tailwindcss@^3 @lynx-js/tailwind-preset
```

```sh [yarn]
yarn add -D tailwindcss@^3 @lynx-js/tailwind-preset
```

```sh [pnpm]
pnpm add -D tailwindcss@^3 @lynx-js/tailwind-preset
```

```sh [bun]
bun add -D tailwindcss@^3 @lynx-js/tailwind-preset
```

```sh [deno]
deno add -D npm:tailwindcss@^3 npm:@lynx-js/tailwind-preset
```

在项目根目录注册 PostCSS 插件：

```js title="postcss.config.js"
export default {
  plugins: {
    tailwindcss: {},
  },
};
```

在 Tailwind 配置中添加 Lynx preset：

```js title="tailwind.config.js"
import lynxPreset from '@lynx-js/tailwind-preset';

export default {
  content: ['./src/**/*.{js,ts,jsx,tsx}'],
  presets: [lynxPreset],
};
```

:::warning
`@lynx-js/tailwind-preset` 仍不稳定。如果发现应当支持的工具类无法使用，请向 [lynx-stack](https://github.com/lynx-family/lynx-stack/issues) 反馈。
:::


下面的示例展示了 Tailwind CSS 在 Lynx 中的用法：

**下面是一个示例: tailwindcss**

**Entry:** `src`
**Bundle:** `dist/main.lynx.bundle` | Web: `dist/main.web.bundle`

```tsx
import { useCallback, useState } from "@lynx-js/react";
import "./App.css";

type ThemeName = "dark" | "light" | "ocean";
const themeNames: ThemeName[] = ["dark", "light", "ocean"];

export function App() {
  const [currentTheme, setCurrentTheme] = useState<ThemeName>("dark");
  const [pushEnabled, setPushEnabled] = useState(true);
  const [emailEnabled, setEmailEnabled] = useState(false);
  const [plan, setPlan] = useState<"free" | "pro">("pro");

  const name = "Hux";
  const email = "hux@example.com";

  const togglePush = useCallback(() => {
    "background-only";
    setPushEnabled((v) => !v);
  }, []);

  const toggleEmail = useCallback(() => {
    "background-only";
    setEmailEnabled((v) => !v);
  }, []);

  const upgradePlan = useCallback(() => {
    "background-only";
    setPlan((v) => (v === "free" ? "pro" : "free"));
  }, []);

  const planBadgeClass = plan === "pro"
    ? "bg-primary text-primary-foreground"
    : "bg-secondary text-secondary-foreground";

  return (
    <scroll-view
      className={`w-full h-full bg-background theme-${currentTheme}`}
      scroll-orientation="vertical"
    >
      <view className="p-6 flex flex-col gap-6">
        {/* Header */}
        <view className="flex flex-col gap-1">
          <text className="text-foreground text-2xl font-bold">Settings</text>
          <text className="text-muted-foreground text-sm">
            Manage your account and preferences.
          </text>
        </view>

        {/* Card: Theme Switcher */}
        <view className="bg-card rounded-lg border border-border flex flex-col">
          <view className="p-4 pb-0 flex flex-col gap-1">
            <text className="text-card-foreground text-lg font-semibold">
              Theme
            </text>
            <text className="text-muted-foreground text-sm">
              Switch design tokens at runtime via CSS variables.
            </text>
          </view>

          <view className="p-4 flex flex-row gap-2">
            {themeNames.map((t) => (
              <view
                key={t}
                className={`flex-1 rounded-md py-2 items-center justify-center border ${
                  currentTheme === t
                    ? "bg-primary border-primary"
                    : "bg-secondary border-border"
                }`}
                bindtap={() => setCurrentTheme(t)}
              >
                <text
                  className={`text-sm font-medium ${
                    currentTheme === t
                      ? "text-primary-foreground"
                      : "text-secondary-foreground"
                  }`}
                >
                  {t[0].toUpperCase() + t.slice(1)}
                </text>
              </view>
            ))}
          </view>
        </view>

        {/* Card: Profile */}
        <view className="bg-card rounded-lg border border-border flex flex-col">
          <view className="p-4 pb-0 flex flex-col gap-1">
            <text className="text-card-foreground text-lg font-semibold">
              Profile
            </text>
            <text className="text-muted-foreground text-sm">
              Your public profile information.
            </text>
          </view>

          <view className="p-4 flex flex-col gap-4">
            {/* Avatar + Name row */}
            <view className="flex flex-row items-center gap-3">
              <view className="w-10 h-10 rounded-full bg-primary items-center justify-center">
                <text className="text-primary-foreground text-lg font-semibold">
                  H
                </text>
              </view>
              <view className="flex flex-col">
                <text className="text-card-foreground text-base font-medium">
                  {name}
                </text>
                <text className="text-muted-foreground text-sm">{email}</text>
              </view>
              <view className="ml-auto">
                <view
                  className={`rounded-full px-2.5 py-0.5 ${planBadgeClass}`}
                >
                  <text className="text-xs font-medium">
                    {plan.toUpperCase()}
                  </text>
                </view>
              </view>
            </view>

            <view className="h-px bg-border" />

            {/* Input: Name */}
            <view className="flex flex-col gap-1.5">
              <text className="text-card-foreground text-sm font-medium">
                Display Name
              </text>
              <view className="bg-secondary rounded-md border border-border px-3 py-2">
                <text className="text-secondary-foreground text-sm">
                  {name}
                </text>
              </view>
            </view>

            {/* Input: Email */}
            <view className="flex flex-col gap-1.5">
              <text className="text-card-foreground text-sm font-medium">
                Email
              </text>
              <view className="bg-secondary rounded-md border border-border px-3 py-2">
                <text className="text-secondary-foreground text-sm">
                  {email}
                </text>
              </view>
            </view>
          </view>

          <view className="p-4 pt-0 flex flex-row gap-2">
            <view
              className="bg-primary rounded-md py-2 px-4 items-center justify-center"
              bindtap={upgradePlan}
            >
              <text className="text-primary-foreground text-sm font-medium">
                {plan === "pro" ? "Downgrade" : "Upgrade to Pro"}
              </text>
            </view>
          </view>
        </view>

        {/* Card: Notifications */}
        <view className="bg-card rounded-lg border border-border flex flex-col">
          <view className="p-4 pb-0 flex flex-col gap-1">
            <text className="text-card-foreground text-lg font-semibold">
              Notifications
            </text>
            <text className="text-muted-foreground text-sm">
              Choose how you want to be notified.
            </text>
          </view>

          <view className="p-4 flex flex-col gap-3">
            {/* Switch row: Push */}
            <view
              className="flex flex-row items-center justify-between"
              bindtap={togglePush}
            >
              <view className="flex flex-col">
                <text className="text-card-foreground text-sm font-medium">
                  Push Notifications
                </text>
                <text className="text-muted-foreground text-xs">
                  Receive push notifications on your device.
                </text>
              </view>
              <view
                className={`w-10 h-6 rounded-full p-0.5 ${pushEnabled ? "bg-primary" : "bg-secondary"}`}
              >
                <view
                  className={`w-5 h-5 rounded-full bg-white ${pushEnabled ? "ml-4" : "ml-0"}`}
                />
              </view>
            </view>

            <view className="h-px bg-border" />

            {/* Switch row: Email */}
            <view
              className="flex flex-row items-center justify-between"
              bindtap={toggleEmail}
            >
              <view className="flex flex-col">
                <text className="text-card-foreground text-sm font-medium">
                  Email Notifications
                </text>
                <text className="text-muted-foreground text-xs">
                  Receive email digests and updates.
                </text>
              </view>
              <view
                className={`w-10 h-6 rounded-full p-0.5 ${emailEnabled ? "bg-primary" : "bg-secondary"}`}
              >
                <view
                  className={`w-5 h-5 rounded-full bg-white ${emailEnabled ? "ml-4" : "ml-0"}`}
                />
              </view>
            </view>
          </view>
        </view>

        {/* Card: Danger Zone */}
        <view className="bg-card rounded-lg border border-destructive flex flex-col">
          <view className="p-4 pb-0 flex flex-col gap-1">
            <text className="text-destructive text-lg font-semibold">
              Danger Zone
            </text>
            <text className="text-muted-foreground text-sm">
              Irreversible and destructive actions.
            </text>
          </view>

          <view className="p-4 flex flex-row gap-2">
            <view className="bg-destructive rounded-md py-2 px-4 items-center justify-center">
              <text className="text-destructive-foreground text-sm font-medium">
                Delete Account
              </text>
            </view>
            <view className="bg-secondary rounded-md py-2 px-4 items-center justify-center border border-border">
              <text className="text-secondary-foreground text-sm font-medium">
                Cancel
              </text>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
  );
}

```

v0.6.1


