Styling Options
Mantine recommends using CSS Modules to style components but you can also use any styling methods you want. Please refer to the mantine documentations Styles Overview.
Here are some examples and tradeoffs of different styling methods.
To recommend for our teams, CSS Modules and Tailwind are both the best options.
Before You Style
The @rss3/mantine-theme
package aims to provide a consistent theme for all RSS3 official websites. You may only want to change a few styles of the components. If you make significant changes, please consider whether the customized styles are suitable and reusable for all RSS3 official websites. If they are, please consider contributing to the @rss3/mantine-theme
package.
In most cases, you may only need to change the "layouting" styles (e.g. width
, margin
, padding
, border
, flex
, etc.).
CSS Modules ⭐︎⭐︎⭐︎
Code:
Result:
Tailwind ⭐︎⭐︎⭐︎
The benefits of using Tailwind is that:
- You don't need to write another CSS file.
- You can use the same class names across the entire project(s). This makes it easier to remember and use.
- The performance is good as well.
Basic Usage
Code:
Result:
If you followed the "Setup Tailwind" section in the Getting Started page, you can use the Tailwind classes directly in the className
prop.
Tailwind Theme vs. Mantine Theme
Bonus point: the Tailwind theme are coordinated with the Mantine theme, which means the following codes are identical in real effect:
Note that in mantine, the primary shade is 6
, while in Tailwind, the primary shade is 600
. So the color="yellow"
or color="yellow.6"
prop is equivalent to bg-yellow
or bg-yellow-600
in Tailwind.
For a complete example of the color red
:
color="red"
is equivalent tobg-red
orbg-red-600
color="red.0"
is equivalent tobg-red-50
color="red.1"
is equivalent tobg-red-100
color="red.2"
is equivalent tobg-red-200
color="red.3"
is equivalent tobg-red-300
color="red.4"
is equivalent tobg-red-400
color="red.5"
is equivalent tobg-red-500
color="red.6"
is equivalent tobg-red-600
color="red.7"
is equivalent tobg-red-700
color="red.8"
is equivalent tobg-red-800
color="red.9"
is equivalent tobg-red-900
Use classNames Prop
You can also exploit the classNames
prop to use Tailwind classes for more accurate styling:
You can find the classNames
styles API of each component in the "Styles API" section. For example, the TextInput#styles-api.
Style Props ⭐︎⭐︎
One of the above examples using color
and radius
props (color="blue" c="yellow" radius="xl"
) is using the Style Props feature of Mantine. It is a very powerful feature that allows you to style components with props.
The benefits:
- You don't need to write another CSS file.
- Very very easy to use.
- Type-safe.
The tradeoffs:
- The performance is not as good as CSS Modules or Tailwind. Because the style props are converted to
style
attributes which increases the bundle size. - It is not easy to override. For example, if you want to override the
color
prop, you need to use!important
or other inline styles to override it.
It is recommended to only use Style Props when you need to change a few (like 1-3) styles of a component.
For another example:
The identical code using Tailwind classes:
If you could use Tailwind classes, it is recommended to use Tailwind classes instead of Style Props.
Inline Styles ⭐︎
The last option is to use inline styles. It is not recommended to use inline styles because:
- It is not easy to maintain.
- It is not easy to read.
- It is not easy to override.
But if you just want to make a quick demo or make life easier, you can use inline styles anyway. 😊
Example:
Result:
There is also a styles
prop that just like the classNames
prop:
Result: