HTML marginwidth Attribute

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

Definition and Usage

The marginwidth attribute on an <iframe> set the space, in pixels, between the frame’s content and its left and right 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 marginheight for top/bottom space.

Deprecated / obsolete.

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

HTML Syntax

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

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

Attribute Values

Historical values (do not use in new work):

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

Live Preview

Compare obsolete marginwidth 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 marginwidth 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 horizontal internal space with marginwidth. Do not copy this into new work.

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

How It Works

  1. Intent. 24 meant 24px between content and the left/right 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 horizontal spacing with CSS on body.

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

How It Works

  1. No marginwidth. Spacing lives in the embedded stylesheet.
  2. body margin. margin: 8px 24px adds left/right 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 horizontal padding on the embedded page with a simple control — not iframe.marginWidth.

HTML
<label for="space">Left/right 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:8px 16px;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:8px " +
      px + "px;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 marginWidth. The legacy IDL property mirrors the deprecated attribute.
  3. Visible feedback. The label shows the current pixel value.
  4. Same idea for height. Use vertical CSS instead of marginheight.

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. marginwidth 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

marginheight is also deprecated

Top/bottom internal space used marginheight. 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 marginwidth “for older browsers.” Use CSS and delete the obsolete attribute.

Browser Support

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

✓ Deprecated · Prefer CSS

marginwidth

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*
marginwidth Deprecated for authors

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

Key Takeaways

  • Role: old left/right 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: marginheight, frameborder, scrolling — use CSS.

One line: drop marginwidth — put left/right space in the framed page’s CSS.

Frequently Asked Questions

On an iframe, marginwidth set the space in pixels between the frame’s content and its left and right borders — an internal horizontal 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. marginwidth was meant to space content inside the frame, next to its left and right edges.
The companion deprecated attribute for top and bottom internal space. Use CSS vertical 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-left / margin-right } or padding. With srcdoc, put that CSS inside the embedded HTML.

Next: max

Upper limits on number, date, range, meter, and progress.

max 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