When running my current Astro.build project locally using astro dev
, styling works as expected.
After building and deploying the site though – either with astro build
and astro preview
or live on cloudflare pages – the lg
breakpoint does not apply.
As you can see, on the live site, the md breakpoint (min-width: 768px) is applied, even though the screen is wide enough for the lg breakpoint condition to the fulfilled. The lg breakpoint calls for min-width: 1024px
. Running document.body.clientWidth
yields 1920
. As such, the lg breakpoint, instead of the md breakpoint should be applied.
This behavior is the same on Firefox and Chrome. Disabling compression does not fix the issue. Deployment works with Cloudflare pages CI/CD. This issue appears on all built commits since replacing the static width with a responsive one.
I don't know whether this issue stems from Astro, Tailwind, Vite, Cloudflare pages or some other part of my application.
Edit: seems like this is not a Tailwind/CSS issue. Reproducing the elements on tailwind play does work as expected (eg. as during live testing). I suppose the error comes from a build stage. https://play.tailwindcss.com/wnVcYAD5B2
Edit:
I found the root cause of the issue. I do not yet know how to fix it.
On the dev server, media queries and their subsequent classes are defined in descending order.
@media (min-width: 640px) {
/* sm breakpoint */
/* ... */
}
@media (min-width: 768px) {
/* md breakpoint */
/* ... */
}
@media (min-width: 1024px) {
/* lg breakpoint */
/* ... */
}
/* And so on */
The built CSS has the media queries in a random (perhaps order of first use?) order.
@media (min-width: 1024px) {
/* lg breakpoint */
/* ... */
}
@media (min-width: 640px) {
/* sm breakpoint */
/* ... */
}
@media (min-width: 768px) {
/* md breakpoint */
/* ... */
}
If my CSS knowledge is correct, the md breakpoint is used in the built project since they are the bottom most, active classes in the CSS.