Skip to content

Commit 8ac3b77

Browse files
authored
issue #3713 (#1257)
Co-authored-by: pourhakimi <[email protected]>
1 parent d90a0ff commit 8ac3b77

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

docs/styling/custom-stylesheets.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,111 @@ Without a leading slash the path is considered relative to the current page rout
3333
not work for routes containing more than one path component, like `/blog/my-cool-post`.
3434
```
3535

36+
37+
## Styling with CSS
38+
39+
You can use CSS variables directly in your Reflex app by passing them alongside the appropriae props. Create a `style.css` file inside the `assets` folder with the following lines:
40+
41+
```css
42+
:root {
43+
--primary-color: blue;
44+
--accent-color: green;
45+
}
46+
```
47+
48+
Then, after referencing the CSS file within the `stylesheets` props of `rx.App`, you can access the CSS props directly like this
49+
50+
```python
51+
app = rx.App(
52+
theme=rx.theme(appearance="light"),
53+
stylesheets=["/style.css"],
54+
)
55+
app.add_page(
56+
rx.center(
57+
rx.text("CSS Variables!"),
58+
width="100%",
59+
height="100vh",
60+
bg="var(--primary-color)",
61+
),
62+
"/",
63+
)
64+
```
65+
66+
## Styling with Tailwind
67+
68+
You can integrate custom Tailwind themes within your Reflex app as well. The setup process is similar to the CSS Styling method mentioned above, with only a few minor variations.
69+
70+
Begin by creating a CSS file inside your `assets` folder. Inside the CSS file, include the following Tailwind directives:
71+
72+
```css
73+
@tailwind base;
74+
@tailwind components;
75+
@tailwind utilities;
76+
77+
:root {
78+
--background: blue;
79+
--foreground: green;
80+
}
81+
82+
.dark {
83+
--background: darkblue;
84+
--foreground: lightgreen;
85+
}
86+
```
87+
88+
We define a couple of custom CSS variables (--background and --foreground) that will be used throughout your app for styling. These variables can be dynamically updated based on the theme.
89+
90+
Tailwind defaults to light mode, but to handle dark mode, you can define a separate set of CSS variables under the .dark class.
91+
92+
Tailwind Directives (@tailwind base, @tailwind components, @tailwind utilities): These are essential Tailwind CSS imports that enable the default base styles, components, and utility classes.
93+
94+
Next, you'll need to configure Tailwind in your `rxconfig.py` file to ensure that the Reflex app uses your custom Tailwind setup.
95+
96+
```python
97+
import reflex as rx
98+
99+
tailwind_config = {
100+
"plugins": ["@tailwindcss/typography"],
101+
"theme": {
102+
"extend": {
103+
"colors": {
104+
"background": "var(--background)",
105+
"foreground": "var(--foreground)"
106+
},
107+
}
108+
},
109+
}
110+
111+
config = rx.Config(
112+
app_name="app",
113+
tailwind=tailwind_config,
114+
)
115+
```
116+
117+
In the theme section, we're extending the default Tailwind theme to include custom colors. Specifically, we're referencing the CSS variables (--background and --foreground) that were defined earlier in your CSS file.
118+
119+
The rx.Config object is used to initialize and configure your Reflex app. Here, we're passing the tailwind_config dictionary to ensure Tailwind's custom setup is applied to the app.
120+
121+
Finally, to apply your custom styles and Tailwind configuration, you need to reference the CSS file you created in your `assets` folder inside the `rx.App` setup. This will allow you to use the custom properties (variables) directly within your Tailwind classes.
122+
123+
In your `app.py` (or main application file), make the following changes:
124+
125+
```python
126+
app = rx.App(
127+
theme=rx.theme(appearance="light"),
128+
stylesheets=["/style.css"],
129+
)
130+
app.add_page(
131+
rx.center(
132+
rx.text("Tailwind & Reflex!"),
133+
class_name="bg-background w-full h-[100vh]",
134+
),
135+
"/",
136+
)
137+
```
138+
139+
The bg-background class uses the --background variable (defined in the CSS file), which will be applied as the background color.
140+
36141
## Fonts
37142

38143
You can take advantage of Reflex's support for custom stylesheets to add custom fonts to your app.

0 commit comments

Comments
 (0)