📱 CSS Mobile Responsiveness: A Practical Guide
Over 50% of web traffic comes from mobile devices. If your site isn’t mobile-responsive, users will bounce. Here's exactly how to make your layout work across all screen sizes.
✅ 1. Always Include the Viewport Meta Tag
<meta name="viewport" content="width=device-width, initial-scale=1.0">
This tells the browser to scale your layout to the device’s width. Without it, your CSS won't behave responsively on mobile.
✅ 2. Use Flexible Layouts with Flexbox or Grid
Avoid fixed positioning. Use flex
or grid
to create adaptable layouts.
.container {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
✅ 3. Set Widths with Relative Units
Avoid px
for widths and spacing. Use %
, em
, rem
, vw
, vh
.
.card {
width: 100%;
padding: 1rem;
}
✅ 4. Use Media Queries to Adjust Layout
Target screen sizes using max-width
or min-width
.
@media (max-width: 768px) {
.sidebar {
display: none;
}
}
Common breakpoints (customize based on layout needs):
@media (max-width: 480px) {} /* Small phones */
@media (max-width: 768px) {} /* Tablets */
@media (min-width: 1024px) {} /* Desktops */
✅ 5. Make Images and Videos Responsive
Prevent overflow and scaling issues.
img, video {
max-width: 100%;
height: auto;
display: block;
}
✅ 6. Mobile-First Approach (Recommended)
Write base styles for mobile, then scale up using min-width
.
/* Base: Mobile */
.card {
width: 100%;
}
/* Tablet */
@media (min-width: 768px) {
.card {
width: 48%;
}
}
/* Desktop */
@media (min-width: 1024px) {
.card {
width: 30%;
}
}
✅ 7. Don’t Forget Responsive Typography
Use em
, rem
, or clamp-based fluid sizing.
body {
font-size: clamp(1rem, 2.5vw, 1.25rem);
}
✅ 8. Test Responsiveness
Use:
- Chrome DevTools (Toggle device toolbar)
- Real phones/tablets
- Responsively App
- BrowserStack
⚠️ Common Mistakes to Avoid
- Using only
px
for spacing or font sizes - Forgetting the
viewport
tag - Not testing on real devices
- Designing only for desktop
🔁 TL;DR Cheatsheet
What | How |
---|---|
Viewport | <meta name="viewport"...> |
Layout | Flexbox / Grid |
Units | %, em, rem, vw, vh |
Media Queries | @media (max-width: 768px) {} |
Images | max-width: 100%; height: auto; |
Typography |
rem or clamp()
|
Testing | DevTools, Responsively, Real Devices |
🎯 Conclusion
Mobile responsiveness is not optional. These 8 techniques cover 95% of what you need. Build mobile-first, test early, and avoid hardcoded layouts.
Top comments (0)