Introducing a new way to review your PRs.

Read the announcement

reflex-dev / reflex

🕸️ Web apps in pure Python 🐍

View on GitHub

30 documents · Updated Nov 23, 2026

docs / styling-and-theming

Styling and theming

Reflex styling gives component declarations one path from Python style choices to browser CSS. It combines CSS properties, reusable style maps, responsive rules, state-based values, color tokens, color modes, and optional Tailwind utilities so an app can keep its visual rules close to its component tree while still producing frontend CSS. The relationship between reactive values and style declarations is covered in Reactive values and styling, and themed controls are part of the component package model described in Component packages.

Style declarations

A style declaration is the set of visual rules applied to a component. Reflex accepts CSS-like props for common rules and a style map for reusable or less common rules. Property names may use Python snake_case; Reflex normalizes them for frontend CSS and supports a small set of layout shorthands such as padding_x, margin_y, and bg_color (see packages/…/style.py• L84-93, #L224-L237).

Themed components can also expose visual props that participate in the same frontend styling path. For example, a text field can accept a responsive size plus theme color and radius overrides, so component-level appearance can follow both viewport and theme context (packages/…/text_field.py• L62-74). Radix avatars and menu surfaces use the same model for responsive size, color scheme, contrast, and radius choices. These props let shared theme context shape component appearance without replacing the component structure (packages/…/avatar.py• L18-35, packages/…/context_menu.py• L66-85, packages/…/dropdown_menu.py• L77-96).

Style sources

Styles can come from the app, a component type, or one component instance. The system combines these sources so broad defaults provide a base and local declarations can refine them.

The precedence order is:

  1. Component defaults.
  2. Styles from App.style.
  3. Styles from the component's style prop.
  4. CSS props passed to that component instance.

The same style map can be shared by several components. A later map in a style list overrides an earlier map when both set the same property. Child components inherit normal CSS properties from their parent unless they set a different value (see packages/…/component.py• L1411-1461, docs/…/overview.md• L21-71, #L79-L103).

References: packages/…/component.py• L1411-1461 docs/…/overview.md• L21-71 docs/…/overview.md• L79-103

References: packages/…/component.py• L1411-1461 docs/…/overview.md• L21-71 docs/…/overview.md• L79-103

Conditional selectors

Conditional selectors attach rules to browser states instead of changing the component tree. Keys such as _hover, :focus, and ::selection become CSS selectors, so interaction styling stays in the generated stylesheet and can be combined with responsive values (see packages/…/style.py• L320-339, #L354-L381, docs/…/overview.md• L189-199). Radix text fields use the same selector path to hide a wrapper when its inner input is hidden, while an explicit display style remains authoritative (packages/…/text_field.py• L27-55, #L148-L155).

References: packages/…/style.py• L320-339 #L354-L381 docs/…/overview.md• L189-199 packages/…/text_field.py• L27-55 #L148-L155

References: packages/…/style.py• L320-339 packages/…/style.py• L354-381 docs/…/overview.md• L189-199

Responsive styling

Responsive styling assigns values to screen-width ranges. A list uses the app's ordered breakpoint positions, while rx.breakpoints names those positions or supplies custom CSS thresholds. The same rule applies to a CSS prop, a nested style map, or a supported component prop.

The default named thresholds are:

NameMinimum width
initial0px
xs30em
sm48em
md62em
lg80em
xl96em

An app can replace the five named thresholds through its style configuration. Responsive display helpers use the same breakpoint model to show mobile, tablet, or desktop content (see packages/…/breakpoints.py• L7-19, #L25-L87, docs/…/responsive.md• L17-69, #L71-L237). Radix avatars and menu content use this model for responsive size, so their appearance can change at configured widths without separate component trees (packages/…/avatar.py• L23-25, packages/…/context_menu.py• L66-73, packages/…/dropdown_menu.py• L77-84).

Responsive CSS output

Responsive values are converted to minimum-width media rules. Nested responsive values remain under their parent selector, which lets a hover rule or another conditional selector vary by screen size without a separate component tree (see packages/…/style.py• L341-386, tests/…/test_style.py• L145-313).

References: packages/…/style.py• L341-386 tests/…/test_style.py• L145-313

References: packages/…/style.py• L341-386 tests/…/test_style.py• L145-313

Reactive and conditional styling

A reactive value is a Var: a value whose frontend expression can change when state changes. A style property can use a Var, a string containing a reactive value, or a conditional expression such as rx.cond or rx.match; Reflex keeps the expression and its required imports or hooks with the style declaration (see Reactive variables, packages/…/style.py• L108-221, tests/…/test_style.py• L333-459).

References: packages/…/style.py• L108-221 tests/…/test_style.py• L333-459

Conditional styling changes a property without replacing the component. This is useful for state such as active, selected, disabled, or validation status. Use rx.color_mode_cond when the condition is the resolved light or dark mode, and use component-level conditional rendering when the entire component or layout must change (see packages/…/cond.py• L221-243).

References: packages/…/cond.py• L221-243

Color and theme utilities

Theme utilities provide shared visual tokens instead of fixed color values. The Radix-based palette supports named colors, shades from 1 through 12, and alpha variants; rx.color turns these choices into CSS variables that follow the active light or dark palette (see packages/…/colors.py• L15-53, packages/…/colors.py• L11-50, #L55-L99).

Theme settings

The Theme component defines the visual context for Radix-based components. App-level settings can set the accent color, gray palette, panel background, radius, scale, and appearance. A nested Theme can override those settings for its child subtree. The theme context also supplies global CSS values used by the themed frontend (see packages/…/base.py• L166-247). Radix avatars and menu content can override selected theme values at the component level while still using the active theme context (packages/…/avatar.py• L23-35, packages/…/context_menu.py• L75-85, packages/…/dropdown_menu.py• L86-96).

References: packages/…/base.py• L166-247

The active Radix theme supplies its stylesheet and wraps the app when the Radix theme system is enabled. App(theme=...) remains a compatibility path, but new app-level theme setup belongs in the Radix themes plugin configuration (see packages/…/plugin.py• L20-54, packages/…/plugin.py• L84-112).

References: packages/…/plugin.py• L20-54 packages/…/plugin.py• L84-112

Color modes

Color mode is the app's selected appearance: system, light, or dark. The frontend keeps both the selected mode and its resolved mode. System mode follows the browser preference, while light and dark mode set the root class and CSS color scheme. color_mode, resolved_color_mode, toggle_color_mode, and set_color_mode expose this context to component declarations (see packages/…/config.py• L262-263, packages/…/style.py• L51-80, packages/…/react-theme.js• L13-18, #L38-L106).

References: packages/…/config.py• L262-263 packages/…/style.py• L51-80 packages/…/react-theme.js• L13-18 packages/…/react-theme.js• L38-106

Tailwind support

Tailwind support adds a utility-class build to the frontend. Add one Tailwind plugin to the app configuration and put utility classes in a component's class_name; the plugin supplies the Tailwind package, scans generated frontend files, and connects Tailwind output to the app stylesheet.

Reflex supports two plugin generations:

PluginUse
TailwindV4PluginPreferred for new apps.
TailwindV3PluginCompatibility for apps that need Tailwind 3 behavior or configuration.

Both plugins accept Tailwind configuration such as theme extensions, content paths, dark-mode settings, presets, and plugins. Tailwind is not active when no Tailwind plugin is configured (see packages/…/shared_tailwind.py• L68-83, #L173-L236, packages/…/tailwind_v3.py• L155-190, packages/…/tailwind_v4.py• L161-197, docs/…/tailwind.md• L7-106).

Tailwind classes must be present as static strings for the Tailwind scan to find them. Use a conditional expression over complete, statically present class strings rather than building a class name from a runtime state value. Lower-level rx.el components are the safer choice when Tailwind should control all visual rules because themed core components already have Radix styles that can compete with utilities (see docs/…/tailwind.md• L196-257).

Frontend style conversion

Reflex converts Python declarations into frontend output during component rendering. The conversion preserves reactive dependencies, normalizes property names, expands shorthands, turns responsive values into media rules, and turns pseudo-style keys into CSS selectors. The resulting CSS-in-JS object is passed to the frontend renderer as the component's CSS value (see packages/…/style.py• L164-221, #L341-L401, packages/…/component.py• L1463-1476).

The style path connects declaration, merge, and output stages as follows:

flowchart LR
    A[Python props and style maps] --> B[Merge app and component styles]
    B --> C[Normalize CSS keys and reactive values]
    C --> D[Expand selectors and media rules]
    D --> E[Frontend CSS-in-JS output]
    E --> F[Browser styles]

Style dependencies are collected with the component's other reactive values. This allows a state change to update a CSS value without treating the style as a backend event or rebuilding unrelated component content (see packages/…/component.py• L1696-1729). The broader page and asset compilation path is described in Frontend compilation.

References: packages/…/component.py• L1696-1729 packages/…/style.py• L164-221 packages/…/component.py• L1463-1476

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to breakpoint names or order must stay aligned with responsive lists, rx.breakpoints, display helpers, and generated media queries.
  • Changes to style key normalization or shorthand expansion must preserve both CSS props and nested selector rules.
  • Changes to VarData carried by styles must preserve imports and hooks for reactive style values.
  • Changes to Radix theme activation must keep the theme wrapper, theme stylesheet, color-mode provider, and color token output aligned.
  • Changes to Radix field wrapper rules must keep hidden-input behavior aligned with explicit display styles and responsive visual props.
  • Changes to Tailwind plugin generation must keep its config, content scan, PostCSS setup, root stylesheet, and frontend package versions compatible.
  • Changes to Tailwind class handling must keep runtime conditional choices statically visible to the Tailwind scan.

Important failure modes

TriggerConsequenceGuard
A Tailwind class is built only from a runtime state value.Tailwind does not emit the needed utility rule.Keep each possible class as a static string and select between complete class strings.
Tailwind utilities are applied to a pre-styled Radix core component.Radix rules and utility rules can produce unexpected visual results.Use rx.el for utility-controlled markup or account for the component's built-in theme styles.
A responsive list has values in the wrong order.Styles appear at the wrong screen widths.Match list positions to the configured initial, xs, sm, md, lg, and xl order.
A color or shade is outside the supported palette.Color construction fails before valid CSS is produced.Use a supported color name and a shade from 1 through 12.
A component or event handler is used as a style value.Style conversion raises an error because the value is not CSS data.Use a literal CSS value or a reactive Var.
A hidden text-field input has a wrapper display rule that conflicts with the intended layout.The wrapper can remain visible or override the requested display behavior.Set the desired display value explicitly when the wrapper must remain visible or use a custom layout.

Key files

FileWhy to read it
packages/…/style.pyDefines style normalization, shorthand expansion, selector handling, responsive conversion, color-mode access, and frontend CSS formatting.
packages/…/component.pyShows style precedence, recursive application, CSS output attachment, and reactive dependency collection.
packages/…/breakpoints.pyDefines the shared responsive breakpoint model and custom threshold support.
packages/…/colors.pyDefines the public color utility and its validation boundary.
packages/…/base.pyDefines the theme context and the settings that shape Radix component appearance.
packages/…/avatar.pyIllustrates responsive sizing and per-component theme controls for a themed visual component.
packages/…/context_menu.pyIllustrates responsive menu surfaces and per-component color controls in a themed component family.
packages/…/dropdown_menu.pyIllustrates responsive menu surfaces and per-component color controls in a themed component family.
packages/…/text_field.pyShows how a themed input exposes responsive and theme visual props and keeps hidden input wrappers from affecting layout.
packages/…/react-theme.jsShows how selected and resolved color modes reach the browser.
packages/…/shared_tailwind.pyDefines the shared Tailwind configuration contract and frontend setup.
packages/…/tailwind_v3.pyShows the Tailwind 3 build integration.
packages/…/tailwind_v4.pyShows the Tailwind 4 build integration.
Component authoringExplains how component declarations expose style props and participate in the component tree.
Reactive variablesExplains the reactive values that can drive style declarations.
Frontend compilationExplains the wider conversion from Reflex declarations to browser output.