Fix content wider than the viewport
Content that exceeds the viewport width forces horizontal scrolling and creates a poor mobile experience. Making sure all content fits within the viewport keeps every part of the page reachable on a phone.
Horizontal scrolling on mobile devices creates usability problems. Customers expect to scroll vertically, and horizontal content overflow breaks this expectation.
Content overflow causes specific issues:
- Hidden content: customers must scroll horizontally to see content that's off-screen.
- Cramped layouts: content that overflows in portrait orientation can reflow unpredictably when the device rotates.
- Accessibility barriers: screen readers struggle with overflowing content.
- Broken interactions: buttons and links pushed off-screen can be hard or impossible to reach.
Common causes of content overflow:
- Fixed-width elements, for example
width: 1200px. - Large images without
max-widthconstraints. - Tables with many columns.
- Pre-formatted text blocks.
- Absolute positioning with fixed coordinates.
- CSS transforms that extend beyond the viewport.
Overflow is easy to miss on a desktop browser at full width, so check every layout at mobile viewport sizes with Chrome DevTools device emulation.
Anchor to Set the proper viewport meta tagSet the proper viewport meta tag
Essential viewport configuration:
Without this tag, mobile browsers assume desktop width, typically 980 px, which causes all content to shrink and potentially overflow.
Anchor to Use relative widths instead of fixedUse relative widths instead of fixed
Replace fixed widths with percentages or max-width:
Anchor to Constrain images and mediaConstrain images and media
Prevent images from overflowing:
Shopify image sizing:
Anchor to Handle tables responsivelyHandle tables responsively
Horizontal scroll for tables:
Responsive table pattern, with a card layout on mobile:
Anchor to Fix pre-formatted textFix pre-formatted text
Wrap code blocks and pre elements:
Anchor to Handle long words and URLsHandle long words and URLs
Break long strings:
Anchor to Use CSS Grid and Flexbox safelyUse CSS Grid and Flexbox safely
Prevent grid and flex items from overflowing:
Anchor to ExamplesExamples
Anchor to Fixing an oversized product imageFixing an oversized product image
Before:
After:
Anchor to Responsive hero section with textResponsive hero section with text
Anchor to TestingTesting
-
Mobile viewport testing: use Chrome DevTools Device Mode. Test at 320 px, 375 px, and 414 px widths. Verify that there's no horizontal scrolling, and check that all content is visible.
-
Overflow detection: in the DevTools console, run
Array.from(document.querySelectorAll('*')).filter((element) => element.scrollWidth > document.documentElement.clientWidth)to list the elements that overflow, or scroll the page horizontally and inspect what moves. -
Real device testing: test on actual mobile devices. Try different screen sizes and orientations, and verify that there's no horizontal scroll on any page.
-
Lighthouse audit: run a mobile Lighthouse audit and verify that there are no viewport meta tag issues.
-
Scroll testing: scroll through the entire page vertically. Verify that no horizontal scroll appears, and check that all interactive elements are accessible.
Anchor to ReferencesReferences
- MDN: viewport meta tag
- MDN: responsive images
- Chrome DevTools: simulate mobile devices with device mode
- Build responsive layouts that perform well