TailwindCSS's responsive design is based on a Mobile-First strategy, implementing style adaptation for different screen sizes through breakpoint prefixes.
Default Breakpoint System
TailwindCSS provides five default breakpoints:
javascript// tailwind.config.js module.exports = { theme: { screens: { 'sm': '640px', // Small screen devices 'md': '768px', // Tablet devices 'lg': '1024px', // Laptops 'xl': '1280px', // Large screen devices '2xl': '1536px', // Extra large screen devices }, }, }
Usage
1. Basic Responsive Classes
html<!-- Default styles (mobile) --> <div class="w-full md:w-1/2 lg:w-1/3"> Content area </div>
2. Responsive Show/Hide
html<!-- Show on mobile, hide on desktop --> <div class="block md:hidden"> Mobile menu </div> <!-- Hide on mobile, show on desktop --> <div class="hidden md:block"> Desktop navigation </div>
3. Responsive Layout
html<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
Custom Breakpoints
You can customize breakpoints in tailwind.config.js:
javascriptmodule.exports = { theme: { extend: { screens: { 'xs': '475px', '3xl': '1600px', // Use min-width ranges 'tablet': {'min': '640px', 'max': '1023px'}, }, }, }, }
Responsive Modifiers
TailwindCSS supports adding responsive prefixes to any utility class:
html<!-- Responsive font size --> <h1 class="text-2xl md:text-4xl lg:text-6xl"> Responsive heading </h1> <!-- Responsive spacing --> <div class="p-4 md:p-8 lg:p-12"> Responsive padding </div> <!-- Responsive flex layout --> <div class="flex flex-col md:flex-row"> <div>Left</div> <div>Right</div> </div>
Best Practices
- Mobile-First Design: Write mobile styles first, then gradually add breakpoints
- Reasonable Use of Breakpoints: Avoid over-segmenting breakpoints, keep code concise
- Test Multiple Devices: Ensure good display effects on different screen sizes
- Performance Optimization: Use
@mediaqueries to merge styles for similar breakpoints