Skip to content

Responsive Design

Tailwind ใช้ mobile-first approach:

PrefixMin-widthTarget
(none)0pxMobile
sm:640pxLarge phones
md:768pxTablets
lg:1024pxLaptops
xl:1280pxDesktops
2xl:1536pxLarge screens

เขียน styles สำหรับ mobile ก่อน แล้วเพิ่ม breakpoints:

<!-- เริ่มจาก mobile: 1 column -->
<!-- md ขึ้นไป: 2 columns -->
<!-- lg ขึ้นไป: 4 columns -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">...</div>

ตัวอย่างการใช้งาน

Section titled “ตัวอย่างการใช้งาน”
<h1 class="text-2xl md:text-4xl lg:text-6xl font-bold">Responsive Heading</h1>
<div class="p-4 md:p-8 lg:p-12">Padding increases on larger screens</div>
<div class="flex flex-col md:flex-row gap-4">
<aside class="w-full md:w-64">Sidebar</aside>
<main class="flex-1">Content</main>
</div>
<!-- ซ่อนบน mobile, แสดงบน md ขึ้นไป -->
<nav class="hidden md:flex">Desktop nav</nav>
<!-- แสดงบน mobile, ซ่อนบน md ขึ้นไป -->
<button class="md:hidden">☰ Mobile menu</button>

ใช้ max-* สำหรับ desktop-first (ไม่แนะนำ):

<div class="max-md:hidden">Hidden on screens smaller than md (768px)</div>

<div class="container mx-auto px-4">
Centered container with responsive max-width
</div>
ScreenMax-width
sm640px
md768px
lg1024px
xl1280px
2xl1536px

@theme {
/* เพิ่ม breakpoint ใหม่ */
--breakpoint-xs: 480px;
--breakpoint-3xl: 1920px;
}

ใช้งาน:

<div class="xs:text-sm 3xl:text-2xl">Custom breakpoints</div>

Do ✅Don’t ❌
Design mobile-firstDesign desktop-first แล้วซ่อน
ใช้ relative units (rem)ใช้ fixed pixels ทุกที่
Test บนหลาย devicesTest แค่บน desktop
ใช้ containerกำหนด max-width เอง