Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Tailwind's container class sticks to the left and has no padding on small screens

Errorcontainer is flush left on the page

Short answer

Tailwind's container sets width 100% and a max-width equal to the current breakpoint, from sm upward. It does not center itself and adds no horizontal padding. Add mx-auto and a px utility yourself, and expect full width below the sm breakpoint.

  • Applies to Tailwind CSS 4, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Inspect the element in browser dev tools and read the computed width, max-width and margin. They tell you which rule applies at the current screen width.

    grep -rn "className=\"container" src
  2. 2Match what you see, then jump to its fix

    1. 1The content hugs the left edge once the screen is wider than the sm breakpointcontainer does not center itselfFix
    2. 2Text touches the screen edge on phonescontainer has no built-in horizontal paddingFix
    3. 3Width changes in steps as the window grows; sm: classes only apply from the breakpoint upThe max-width follows breakpoints, so it jumps at each oneFix

What does the error look like?

  • container is flush left on the page
  • container touches the screen edge on a phone

Cause 1 of 3

container does not center itself

1Check

The docs state that Tailwind's container does not center itself automatically, unlike containers in other frameworks.

grep -rn "container" src --include=*.jsx --include=*.tsx

2Fix

Add mx-auto to center it.

<div className="container mx-auto">
  ...
</div>

3Verify

Widen the window past 40rem; the block should sit in the middle.

# resize the browser and re-inspect margin

Confirmed by tailwindcss.com

Did this fix it?

Cause 2 of 3

container has no built-in horizontal padding

1Check

The docs say container has no built-in horizontal padding. Below the first breakpoint it is simply width: 100%.

# dev tools: computed padding-left is 0px

2Fix

Add padding utilities next to mx-auto.

<div className="container mx-auto px-4">
  ...
</div>

3Verify

Check a narrow viewport: there should be a gap on both sides.

# dev tools device mode, width 375px

Confirmed by tailwindcss.com

Did this fix it?

Cause 3 of 3

The max-width follows breakpoints, so it jumps at each one

1Check

container sets a max-width equal to the min-width of the current breakpoint. A breakpoint prefix such as md: applies at that size and above.

# dev tools: note max-width at 640px, 768px, 1024px

2Fix

Write the mobile style with plain utilities and use a prefix only for the breakpoint and above; sm: does not mean small screens only. For a different fixed width, use a max-w utility instead of container.

<div className="mx-auto w-full max-w-sm px-4 md:max-w-lg">
  ...
</div>

3Verify

Resize between phone and desktop widths and watch the width.

# resize the browser

Confirmed by tailwindcss.com

Did this fix it?

How do I stop it coming back?

  • Pair container with mx-auto and px-* every time, or wrap it in one reusable component.
  • Write the mobile layout first with unprefixed classes, then add breakpoint prefixes.
  • Check phone width in dev tools device mode before shipping.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Tailwind is installed but the utility classes do nothing

Tailwind classes work in some components but not in others

When classes have no effect, the stylesheet either lacks the v4 import line, is never loaded by the page, or the class name never appears as plain text in a scanned file.

5 causes, each with a fix

Web apps

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Web apps

shadcn/ui setup fails in a Vite React project using Tailwind v4

Imports from @/components/ui do not resolve in the editor or the build

shadcn init needs three things in a Vite project: Tailwind v4 installed with the @tailwindcss/vite plugin and a single @import "tailwindcss" line in your CSS, the @/* path alias in both tsconfig.json and tsconfig.app.json, and the same alias in vite.config.ts, with @types/node installed so path resolves.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.