Common Layout Problems
- Elements overflow on mobile (horizontal scrolling).
- Columns stack incorrectly on small screens.
- Images or text get cut off.
How to Fix:
1. Use Mobile-First Media Queries
Start with mobile styles and add breakpoints for larger screens:
/* Default (mobile) styles */
.container {
padding: 20px;
}
/* Tablet breakpoint */
@media (min-width: 768px) {
.container {
padding: 40px;
display: grid;
grid-template-columns: 1fr 1fr;
}
}
/* Desktop breakpoint */
@media (min-width: 1024px) {
.container {
grid-template-columns: 1fr 1fr 1fr;
}
}
2. Fix Overflow Issues
Prevent horizontal scrolling with:
body {
overflow-x: hidden; /* Emergency fix */
}
img, video {
max-width: 100%; /* Scale media to container */
height: auto;
}
3. Use Flexbox/Grid for Responsive Layouts
Example of a responsive grid:
.grid {
display: grid;
gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}
4. Debug with Browser Tools
Use Chrome DevTools to inspect elements:
- Right-click the element → Inspect.
- Toggle device emulation (
Ctrl + Shift + M
). - Test responsiveness at different screen sizes.
Prevention Tips
- Use a CSS reset (e.g., Normalize.css).
- Validate CSS with W3C Validator.
- Test layouts on real devices (not just emulators).
Note: Avoid fixed widths (e.g.,
width: 960px;
). Use max-width
and relative units like %
or vw
.