DEV Community

Nguyen Long
Nguyen Long

Posted on

5 Common Mobile Layout Bugs in CSS (and How to Fix Them)

Your site looks perfect on your laptop. Then you open it on a phone and suddenly there's a horizontal scrollbar, text spilling off the screen, and buttons stacked on top of each other.

Most of these problems come from a handful of repeat offenders. Here are five of the most common ones, with the fix for each.

1. The mystery horizontal scroll

Symptom: The page scrolls sideways on mobile, and there's empty white space on the right.

Cause: Something is wider than the viewport—usually a fixed pixel width, an image without a max width, or an element using 100vw.

Fix:

img, video, iframe {
  max-width: 100%;
  height: auto;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}
Enter fullscreen mode Exit fullscreen mode

To find the culprit quickly, paste this into DevTools temporarily:

* { outline: 1px solid red; }
Enter fullscreen mode Exit fullscreen mode

2. Forgetting the viewport meta tag

Symptom: The whole page looks zoomed out and tiny on phones, and your media queries never trigger.

Fix: Add this inside <head>:

<meta name="viewport" content="width=device-width, initial-scale=1">
Enter fullscreen mode Exit fullscreen mode

3. Rigid grids that don't wrap

Symptom: Three cards side by side look great on desktop but get crushed into thin columns on mobile.

Fix: Let CSS Grid decide how many columns fit:

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1.5rem;
}
Enter fullscreen mode Exit fullscreen mode

4. Long words and URLs breaking the layout

Symptom: A long email address or URL pushes its container wider than the screen.

Fix:

.content {
  overflow-wrap: anywhere;
}
Enter fullscreen mode Exit fullscreen mode

5. Flex items refusing to shrink

Cause: Flex items have min-width: auto by default, so they won't shrink below their content size.

Fix:

.flex-child {
  min-width: 0;
}
Enter fullscreen mode Exit fullscreen mode

If you're stuck on a stubborn layout bug you want fixed fast, I also help fix HTML, CSS, and JavaScript issues on Fiverr. Happy to take a look!

Top comments (0)