Layouting
We write layouts in UI everyday. What is the best way to do it?
Mantine Layout Components
If possible, use Mantine layout components instead of writing your own. They are very handy and easy to use, also consistent and customizable.
Example:
Result:
Tailwind CSS
Tailwind CSS is also a good choice for layouting. But you need to be careful about the class names. It's easy to write a lot of classes and make the code hard to read (too many div
s and long className
s).
Example:
Result:
However, it has a huge benefit: minimal bundle size! You don't need to import any layout components, just use the class names.
This is the generated HTML of the above Mantine code for comparison:
Which One to Choose?
Simply put:
- if you care about scalability, easy-to-use and more familiar with Mantine, use Mantine.
- if you care about bundle size and more familiar with Tailwind, use Tailwind CSS;
They are both good for layouting. They don't conflict with each other. You can also use both in the same project.
Tips
Here are some tips to make your layout more readable and maintainable. But they are not required! You can still write in your own way.
-
Use
xs
,sm
,md
,lg
,xl
for layouting instead ofpx
orrem
.For example:
h="md"
instead ofh="16px"
orh="1rem"
.padding="lg"
instead ofpadding="16px"
orpadding="1rem"
.className="mt-lg"
instead ofclassName="mt-[16px]"
orclassName="mt-[1rem]"
.className="gap-md"
instead ofclassName="gap-[8px]"
orclassName="gap-[0.5rem]"
.
-
Use
Space
component to add space between custom components instead of adding margin inside the component.For example:
instead of:
-
Use
rem
andem
instead ofpx
for any size.Specifically:
- Use REMs for sizes and spacing.
- Use EMs for media queries.
See:
- Why you shouldn't use px.
- rem in Mantine.