Reflex Logo

Intro

Gallery

Hosting

Learn

Components

Recipes

API Reference

Onboarding

Styling

/

Responsive

Reflex apps can be made responsive to look good on mobile, tablet, and desktop.

You can pass a list of values to any style property to specify its value on different screen sizes.

Hello World

rx.text(
    "Hello World",
    color=["orange", "red", "purple", "blue", "green"],
)

The text will change color based on your screen size. If you are on desktop, try changing the size of your browser window to see the color change.

New in 0.5.6

Responsive values can also be specified using rx.breakpoints. Each size maps to a corresponding key, the value of which will be applied when the screen size is greater than or equal to the named breakpoint.

Hello World

rx.text(
    "Hello World",
    color=rx.breakpoints(
        initial="orange",
        sm="purple",
        lg="green",
    ),
)

Custom breakpoints in CSS units can be mapped to values per component using a dictionary instead of named parameters.

rx.text(
    "Hello World",
    color=rx.breakpoints(
        {
            "0px": "orange",
            "48em": "purple",
            "80em": "green",
        }
    ),
)

For the Radix UI components' fields that supports responsive value, you can also use rx.breakpoints (note that custom breakpoints and list syntax aren't supported).

rx.grid(
    rx.foreach(
        list(range(6)),
        lambda _: rx.box(
            bg_color="#a7db76",
            height="100px",
            width="100px",
        ),
    ),
    columns=rx.breakpoints(initial="2", sm="4", lg="6"),
    spacing="4",
)

The default breakpoints are shown below.

SizeWidth
initial0px
xs30em
sm48em
md62em
lg80em
xl96em

You can customize them using the style property.

app = rx.App(style={"breakpoints": ["520px", "768px", "1024px", "1280px", "1640px"]})

A common use case for responsive is to show different components based on the screen size.

Reflex provides useful helper components for this.

Desktop View

Tablet View

Mobile View

Visible on Mobile and Tablet

Visible on Desktop and Tablet

rx.vstack(
    rx.desktop_only(
        rx.text("Desktop View"),
    ),
    rx.tablet_only(
        rx.text("Tablet View"),
    ),
    rx.mobile_only(
        rx.text("Mobile View"),
    ),
    rx.mobile_and_tablet(
        rx.text("Visible on Mobile and Tablet"),
    ),
    rx.tablet_and_desktop(
        rx.text("Visible on Desktop and Tablet"),
    ),
)

You can specify the breakpoints to use for the responsive components by using the display style property.

Hello World

Hello World

Hello World

Hello World

Hello World

rx.vstack(
    rx.text(
        "Hello World",
        color="green",
        display=["none", "none", "none", "none", "flex"],
    ),
    rx.text(
        "Hello World",
        color="blue",
        display=["none", "none", "none", "flex", "flex"],
    ),
    rx.text(
        "Hello World",
        color="red",
        display=["none", "none", "flex", "flex", "flex"],
    ),
    rx.text(
        "Hello World",
        color="orange",
        display=["none", "flex", "flex", "flex", "flex"],
    ),
    rx.text(
        "Hello World",
        color="yellow",
        display=["flex", "flex", "flex", "flex", "flex"],
    ),
)
← ThemingCustom Stylesheets →

Did you find this useful?

HomeGalleryChangelogIntroductionHosting