HTML marginheight Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The marginheight attribute on an <iframe> set the space, in pixels, between the frame’s content and its top and bottom borders.

MDN lists it under deprecated iframe attributes: do not use it in new content. Prefer CSS on the embedded document (for example body margin) or padding you control via srcdoc. Pair historically with marginwidth for left/right space.

Deprecated / obsolete.

Authors must not rely on marginheight. Learn it to recognize legacy markup; space embedded content with CSS instead.

HTML Syntax

syntax.html
<iframe marginheight=" pixels">…</iframe>

Modern equivalent: style the framed page’s body (or use srcdoc with CSS) for top/bottom spacing — not presentational attributes on the iframe.

Attribute Values

Historical values (do not use in new work):

ValueDescriptionTry it
pixelsNon-negative integer for top and bottom internal spaceTry it
With marginwidthCompanion for left/right internal space (also deprecated)Try it
Modern: body CSSSet margin/padding on the embedded documentTry it
Modern: adjustable CSSChange spacing with CSS instead of marginHeightTry it

Live Preview

Compare obsolete marginheight with CSS spacing inside srcdoc.

pattern

Prefer CSS on the embedded page. Always give the iframe an accessible title.

Demo

Obsolete attribute on the iframe — unreliable; do not ship.

Showing legacy marginheight markup (do not use).

HTML Examples

Short study snippets. Use Try It Yourself for a live editor. Ship only the modern CSS pattern.

Example 1 — Recognize Legacy Markup

Old pages set vertical internal space with marginheight. Do not copy this into new work.

HTML
<!-- Obsolete — study only -->
<iframe
  srcdoc="<p>Embedded content</p>"
  width="320"
  height="100"
  marginheight="24"
  title="Legacy spacing demo">
</iframe>
Try It Yourself

How It Works

  1. Intent. 24 meant 24px between content and the top/bottom frame edges.
  2. Inside the frame. Not the same as CSS margin on the iframe in the parent page.
  3. Spec today. Deprecated — remove from new templates.
  4. Accessibility. Keep a meaningful title on every iframe.

Example 2 — CSS Inside srcdoc

When you control the framed HTML, set vertical spacing with CSS on body.

HTML
<iframe
  width="320"
  height="120"
  title="CSS spacing demo"
  srcdoc="<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      margin: 24px 8px;
      font-family: system-ui, sans-serif;
      background: #dbeafe;
    }
  </style>
</head>
<body>Embedded content</body>
</html>">
</iframe>
Try It Yourself

How It Works

  1. No marginheight. Spacing lives in the embedded stylesheet.
  2. body margin. margin: 24px 8px adds top/bottom space like the old attribute.
  3. srcdoc. Handy for demos when you own the framed markup.
  4. Cross-origin pages. You cannot restyle another site’s body from the parent; the framed author must set CSS.

Example 3 — Adjust Spacing with CSS

Change vertical padding on the embedded page with a simple control — not iframe.marginHeight.

HTML
<label for="space">Top/bottom space: <span id="val">16</span>px</label>
<input type="range" id="space" min="0" max="40" value="16">

<iframe
  id="frame"
  width="320"
  height="110"
  title="Adjustable spacing"
  srcdoc="<!DOCTYPE html><html><head>
<style>body{margin:16px 8px;font-family:system-ui;background:#dbeafe;}</style>
</head><body>Embedded content</body></html>">
</iframe>

<script>
  var range = document.getElementById("space");
  var val = document.getElementById("val");
  var frame = document.getElementById("frame");
  range.addEventListener("input", function () {
    var px = range.value;
    val.textContent = px;
    frame.srcdoc =
      "<!DOCTYPE html><html><head><style>body{margin:" +
      px + "px 8px;font-family:system-ui;background:#dbeafe;}</style></head>" +
      "<body>Embedded content</body></html>";
  });
</script>
Try It Yourself

How It Works

  1. Control CSS, not the attribute. The slider updates body margin in srcdoc.
  2. Avoid marginHeight. The legacy IDL property mirrors the deprecated attribute.
  3. Visible feedback. The label shows the current pixel value.
  4. Same idea for width. Use horizontal CSS instead of marginwidth.

Edge Cases & Pitfalls

Most confusion comes from mixing internal frame spacing with CSS margin on the iframe itself.

inside vs outside

Not iframe CSS margin

iframe { margin: … } offsets the frame in the parent layout. marginheight targeted space inside the frame around its content.

origin

Cross-origin limits

You cannot restyle another origin’s document from the parent page. Spacing must come from that page’s own CSS (or from content you embed with srcdoc).

pair

marginwidth is also deprecated

Left/right internal space used marginwidth. Replace both with CSS; do not keep one attribute alone.

a11y

Always set title

Spacing attributes never replaced an accessible name. Give every iframe a clear title.

migrate

Remove, don’t stack

Do not keep marginheight “for older browsers.” Use CSS and delete the obsolete attribute.

Browser Support

marginheight is a deprecated HTML attribute on iframe. Do not use it for new layouts.

✓ Deprecated · Prefer CSS

marginheight

MDN: deprecated on iframe. Engines may still recognize it for legacy pages, but authors must use CSS on the embedded content instead.

Legacy Not for new work
Google Chrome Legacy parse*
Deprecated*
Mozilla Firefox Legacy parse*
Deprecated*
Apple Safari Legacy parse*
Deprecated*
Microsoft Edge Legacy parse*
Deprecated*
Opera Legacy parse*
Deprecated*
Samsung Internet Legacy parse*
Deprecated*
Internet Explorer Legacy era
Legacy*
marginheight Deprecated for authors

Bottom line: *Do not ship marginheight. Space framed content with CSS (body margin/padding or srcdoc styles).

Key Takeaways

  • Role: old top/bottom space inside an <iframe> — now deprecated.
  • Replace with: CSS on the embedded body (or controlled srcdoc).
  • Not outer margin: iframe CSS margin is a different layout idea.
  • Also drop: marginwidth, frameborder, scrolling — use CSS.

One line: drop marginheight — put top/bottom space in the framed page’s CSS.

Frequently Asked Questions

On an iframe, marginheight set the space in pixels between the frame’s content and its top and bottom borders — an internal vertical margin for the embedded document.
No. MDN and HTML mark it deprecated/obsolete. Prefer CSS on the embedded page (body margin) or spacing you control via srcdoc / the framed document.
No. CSS margin on the iframe moves the frame in the parent page. marginheight was meant to space content inside the frame, next to its top and bottom edges.
The companion deprecated attribute for left and right internal space. Use CSS horizontal margin or padding on the embedded content instead.
Historically iframe (and legacy frame). Do not use it on new pages.
If you control the framed document, set body { margin-top / margin-bottom } or padding. With srcdoc, put that CSS inside the embedded HTML.

Next: marginwidth

Deprecated left/right spacing inside an iframe.

marginwidth tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful