Mobile-first means designing websites starting with small screens first. This helps make sure the site works well on phones before adding styles for bigger screens.
Mobile-first philosophy in Tailwind
/* Tailwind uses small screen styles by default. Use prefixes like 'md:', 'lg:' to add styles for bigger screens. */ <div class="p-4 bg-blue-500 md:bg-green-500 lg:bg-red-500"> Content here </div>
Tailwind applies styles without prefixes to all screen sizes starting from mobile.
Use prefixes like md: or lg: to change styles on medium or large screens.
<div class="text-sm md:text-lg">
Text size changes from small on mobile to large on medium screens.
</div><button class="bg-blue-500 hover:bg-blue-700 md:bg-green-500">
Click me
</button><div class="p-2 md:p-6 lg:p-10">
Padding grows as screen size increases.
</div>This page shows a colored box that changes color and text size as the screen gets bigger. On a phone, the box is blue with smaller text. On a tablet, it turns green with medium text. On a desktop, it becomes red with larger text.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Mobile-first Tailwind Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="bg-gray-100"> <main class="max-w-md mx-auto p-4"> <h1 class="text-xl font-bold text-center mb-4">Mobile-first Design</h1> <div class="bg-blue-400 text-white p-4 rounded md:bg-green-500 lg:bg-red-600"> <p class="text-base md:text-lg lg:text-xl"> This box is blue on small screens, green on medium, and red on large screens. </p> </div> </main> </body> </html>
Always include the viewport meta tag for proper mobile scaling.
Start styling without prefixes for mobile, then add prefixes for bigger screens.
Test your design by resizing the browser or using device simulation in browser DevTools.
Mobile-first means design for small screens first, then add styles for bigger screens.
Tailwind applies styles mobile-first by default, using prefixes like md: for bigger screens.
This approach helps make websites fast and easy to use on phones.