I have Figma design for mobile application and for desktop application for my web page. I don’t know how to apply both mobile and desktop versions on my code so when I open the app in my phone only the mobile version will appear and the same for desktop version.
Should I use @media
query for that? Or there is another option for doing that? Assume mobile screen width is less then 390 px and desktop is more then 390px. I try using @media
but both screens appear when I’m in my computer screen.
2
Answers
If you are using TailwindCSS, they have class prefixes to designate for mobile and desktop screen sizes. Visit the documentation to learn more.
From the documentation:
First, make sure you’ve added the viewport meta tag to the of your document:
Then to add a utility but only have it take effect at a certain breakpoint, all you need to do is prefix the utility with the breakpoint name, followed by the
:
character:There are five breakpoints by default, inspired by common device resolutions:
sm
@media (min-width: 640px) { ... }
md
@media (min-width: 768px) { ... }
lg
@media (min-width: 1024px) { ... }
xl
@media (min-width: 1280px) { ... }
2xl
@media (min-width: 1536px) { ... }
This works for every utility class in the framework, which means you can change literally anything at a given breakpoint — even things like letter spacing or cursor styles.
In Tailwind, there are a series of predefined media query breakpoints. For example,
sm
means: it doesn’t apply when the window width is less than640px
. It applies when the width is equal to or greater than640px
. Based on this, the effect you need can be achieved with code like the following:(👆Resize the screen width down to
640px
to see mobile screen.)Tailwind has another set of breakpoints with the opposite effect, starting with
max-*
. For example,max-sm
has the exact opposite condition ofsm
. So you can also achieve the same effect in the following way:If you need to break exactly at
390px
, you can add a custom breakpoint totailwind.config.ts
:Usage:
<div class="mb:hidden"></div>
or<div class="max-mb:hidden"></div>