Maaz Zindani

Loading the index…

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

mt-4 adds no space above an element in Tailwind

Errormt-4 on a span or a link has no visible effect

Short answer

The class is usually generated correctly, but CSS ignores the margin. Vertical margin has no effect on a non-replaced inline element like a span. A top margin can also collapse into its parent's. Make the element block or inline-block, or stop the collapse with a padding, a border or flow-root.

  • 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 dev tools. If margin-top appears in the Styles panel but the box does not move, the class works and CSS rules are ignoring the margin.

    <span className="mt-4">Label</span>
  2. 2Match what you see, then jump to its fix

    1. 1Dev tools shows margin-top on a span, a, em or code, and nothing moves.The element is inlineFix
    2. 2The margin on the first child seems to move the whole parent box.The margin collapses with the parent's marginFix
    3. 3mt-[10%] gives a much smaller or larger space than expected.The class is not what you think it is (a percentage or a wrong value)Fix

What does the error look like?

  • mt-4 on a span or a link has no visible effect
  • The space above a child appears on its parent instead

Cause 1 of 3

The element is inline

1Check

MDN states that margin-top has no effect on non-replaced inline elements such as span or code. Check the element's computed display.

getComputedStyle($0).display

2Fix

Change the display type: block, or inline-block when it must stay in a line of text.

<span className="mt-4 inline-block">Label</span>

3Verify

The space above the label appears.

npm run dev

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

The margin collapses with the parent's margin

1Check

A parent's margin-top can collapse with its first child's when no border, padding, inline content or new formatting context separates them.

getComputedStyle($0.parentElement).display

2Fix

Create a block formatting context on the parent with flow-root, or give the parent a padding or border.

<section className="flow-root">
  <h2 className="mt-8">Title</h2>
</section>

3Verify

The space now appears inside the section, above the heading.

npm run dev

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

The class is not what you think it is (a percentage or a wrong value)

1Check

A percentage margin is relative to the containing block's width, not its height, so on a wide, short box it looks too large. Tailwind generates margin-top: <value> for the arbitrary form.

getComputedStyle($0).marginTop

2Fix

Use the spacing scale (mt-4 is margin-top: calc(var(--spacing) * 4)) or a fixed length, and use gap on flex or grid parents.

<div className="mt-4">...</div>
<div className="flex flex-col gap-4">...</div>

3Verify

The computed margin-top matches the scale value you chose.

getComputedStyle($0).marginTop

Confirmed by tailwindcss.com

Did this fix it?

How do I stop it coming back?

  • Prefer gap on flex and grid parents over margins on each child.
  • Check the element's display type before blaming the class.

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.