We’ve talked a lot about responsive variants in this course so far, and I would just like to dive a bit deeper into them so it becomes easier for you to build responsive websites.
The trick here is to include custom responsive variants. They should have these qualities:
- They make it easy for you to know what’s going on
- They are consistent across any website you build.
Tailwind’s built-in variants are insufficient
Tailwind comes with these built-in responsive variants.
| Variant | Width | CSS |
|---|---|---|
sm |
640px |
@media (width >= 40rem) { … } |
md |
768px |
@media (width >= 48rem) { … } |
lg |
1024px |
@media (width >= 64rem) { … } |
xl |
1280px |
@media (width >= 80rem) { … } |
2xl |
1536px |
@media (width >= 96rem) { … } |
They are insufficient because each of these variants is tied to a device.
In web development, it is a good practice to not use device breakpoints but to use content-based breakpoints. When the content starts to look ugly, you will change your breakpoints.
Better media breakpoint variants
So instead of relying on Tailwind’s built-in variants, I created a set of breakpoint variants across all of my projects.
And there’s a very simple rule here:
- Every 1 unit is
100px - And every half a unit is
50px
I made variants from 100px all the way up to 2000px to cover any device that I may need.
| Variant | Width | CSS |
|---|---|---|
bp1 |
100px |
@media (width >= 100px) { … } |
bp1.5 |
150px |
@media (width >= 150px) { … } |
bp2 |
200px |
@media (width >= 200px) { … } |
... |
... |
... |
bp20 |
2000px |
@media (width >= 2000px) { … } |
This lets me output any breakpoint at will:
<div class="bp8:p-4">...</div>And the above simply says: “At the 800px breakpoint, use p-4 utility”.
With this system, I no longer need to remember what md or lg refers to.
Better container breakpoint variants
Besides media queries, Tailwind lets you use container queries as well.
To use it, you add @container to the parent element, then use their container variants to create container queries.
Here’s an example:
<div class="@container"> <div class="@md:p-4">...</div></div>This means: “At @md variant, use p-4 utility”.
But here’s the problem: md represents different numbers in Tailwind’s media query and the container variants. I don’t know why they do this because it’s really confusing.
Here’s a table that shows the difference:
| Variant | Media query | Container query |
|---|---|---|
@3xs |
— | 256px |
@2xs |
— | 288px |
@xs |
— | 320px |
sm / @sm |
640px |
384px |
md / @md |
768px |
448px |
lg / @lg |
1024px |
512px |
xl / @xl |
1280px |
576px |
2xl / @2xl |
1536px |
672px |
@3xl |
— | 768px |
@4xl |
— | 896px |
@5xl |
— | 1024px |
@6xl |
— | 1152px |
@7xl |
— | 1280px |
Anyway, I applied the same rules for my breakpoint variants onto container variants so you can enjoy it as well:
| Variant | Width | CSS |
|---|---|---|
@bp1 |
100px |
@container (width >= 100px) { … } |
@bp1.5 |
150px |
@container (width >= 150px) { … } |
@bp2 |
200px |
@container (width >= 200px) { … } |
... |
... |
... |
@bp20 |
2000px |
@container (width >= 2000px) { … } |
With my system, @bp8: means @container (width >= 800px) { … }. I think that’s way easier to remember and use compared to Tailwind’s default values.
Using these via Splendid Layouts
These breakpoint values are added by default to Splendid Layouts so when you use Splendid Layouts, you automatically have them. There is no need to configure anything else.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson