HTML marginwidth Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
iframe & Legacy Layout

Introduction

The marginwidth attribute on <iframe> (and legacy <frame>) elements set the left and right internal margin in pixels between the iframe’s border and the embedded document content. It is deprecated in HTML5 and should not be used in new projects. Modern browsers often ignore it. Use CSS horizontal padding on a wrapper element or style the embedded page instead. Do not confuse it with CSS margin, which controls spacing around the iframe in the parent layout—nor with width, which sets the iframe’s outer dimensions.

What You’ll Learn

01

<iframe> Only

Legacy frame too.

02

Left + Right

Horizontal internal space.

03

Pixels

Non-negative integer.

04

Deprecated

Use CSS instead.

05

marginheight

Vertical partner attr.

06

Not CSS margin

Internal vs external.

Purpose of marginwidth Attribute

The original purpose of marginwidth was to add horizontal breathing room inside an iframe—space between the iframe’s inner left and right edges and where the embedded page content begins. In early HTML layouts, authors used it alongside marginheight to prevent embedded content from touching the iframe border on all sides.

HTML5 made presentation attributes like marginwidth obsolete in favor of CSS. You may still see the attribute in legacy code. Understanding it helps when maintaining older sites, but new iframe layouts should rely on CSS padding, borders, and styling inside the embedded document.

⚠️
Not margins on “all sides”

The old reference incorrectly suggested marginwidth="20" applied to every side. In fact, marginwidth affects left and right only. Top and bottom spacing was controlled by the separate marginheight attribute.

📝 Syntax

Legacy syntax on an iframe (shown for recognition only—do not use in new projects):

marginwidth-legacy.html
<iframe
  src="example.html"
  width="300"
  height="200"
  marginwidth="20"
  marginheight="10"
  title="Embedded page"></iframe>

Modern CSS equivalent:

iframe-css.html
<div class="iframe-wrap">
  <iframe src="example.html" title="Embedded page"></iframe>
</div>

<style>
  .iframe-wrap {
    padding: 10px 20px; /* vertical horizontal — replaces marginheight + marginwidth */
    border: 1px solid #cbd5e1;
  }
  .iframe-wrap iframe {
    width: 300px;
    height: 200px;
    border: 0;
  }
</style>

Syntax Rules

  • Historically valid on <iframe> and obsolete <frame> elements.
  • Value is a non-negative integer in pixels (no units in the attribute value).
  • Applies the same value to both left and right internal margins.
  • Often paired with marginheight for top and bottom internal margins.
  • Deprecated in HTML5—use CSS padding or embedded-document styles instead.
  • Always include a descriptive title on iframes for accessibility.

💎 Values

The marginwidth attribute accepts a non-negative integer representing pixels:

  • marginwidth="0" — No extra left/right internal space (default in many browsers).
  • marginwidth="20" — 20 pixels of space on the left and right inside the iframe.
  • marginwidth="8" — Small horizontal gap for compact embeds.
  • Negative values are invalid—use zero or a positive integer only.
marginwidth-js.html
// Legacy IDL property (camelCase) — avoid in new code
var iframe = document.getElementById("myIframe");
iframe.marginWidth = 20;
iframe.setAttribute("marginwidth", "20");

// Modern replacement
iframe.parentElement.style.padding = "0 20px";

⚡ Quick Reference

ConceptLegacy (avoid)Modern replacement
Horizontal internal spacemarginwidth="20"Wrapper padding: 0 20px
Vertical internal spacemarginheight="10"Wrapper padding: 10px 0
Space around iframe in pageN/A (wrong attribute)CSS margin on iframe
Embedded page body spacingmarginwidth (unreliable)CSS in embedded document
JavaScript updateiframe.marginWidth = 20wrap.style.padding = "0 20px"
Valid elementsiframe, frameAny element via CSS

Applicable Elements

ElementSupported?Notes
<iframe>ObsoletePrimary historical use; attribute deprecated
<frame>ObsoleteLegacy frameset; frames removed from modern HTML
<embed>, <object>NoUse CSS for spacing on these elements
<div>, <img>, etc.Nomarginwidth is iframe/frame-specific

marginwidth vs CSS margin vs CSS padding

FeaturemarginwidthCSS margin on iframeCSS padding on wrapper
What it affectsInside iframe (left/right)Outside iframe in parent layoutSpace around iframe inside wrapper
StatusDeprecatedStandard CSSStandard CSS (recommended)
UnitsPixels only (integer)px, rem, %, etc.px, rem, %, etc.
Browser support todayLargely ignoredFull supportFull support
Vertical partnermarginheightmargin-top/bottompadding-top/bottom

marginwidth vs marginheight on <iframe>

AttributeExampleInternal spacing
marginwidthmarginwidth="20"Left and right (horizontal)
marginheightmarginheight="10"Top and bottom (vertical)
CSS replacementpadding: 10px 20pxVertical then horizontal (shorthand)

Examples Gallery

Legacy iframe markup, modern CSS horizontal padding replacement, and dynamic spacing with JavaScript.

👀 Live Preview

Modern CSS wrapper with horizontal padding (replaces marginwidth):

The gray bands on the left and right come from wrapper padding: 0 20px—the reliable way to add horizontal space today.

Example — Legacy iframe with marginwidth

Historical pattern setting 20 pixels of left and right internal margin:

index.html
<iframe
  src="example.html"
  width="300"
  height="200"
  marginwidth="20"
  title="Embedded page"></iframe>
Try It Yourself

How It Works

When supported, the browser inset the embedded document’s viewport from the left and right by the specified pixel value. Because the attribute is obsolete, behavior is inconsistent—migrate to CSS for predictable results.

Example — Modern CSS Replacement

Wrap the iframe and use horizontal padding for left/right spacing:

iframe-wrap.html
<div class="iframe-wrap">
  <iframe src="example.html" title="Embedded page"></iframe>
</div>

<style>
  .iframe-wrap {
    padding: 0 20px;
    border: 1px solid #cbd5e1;
  }
  .iframe-wrap iframe { border: 0; width: 100%; height: 200px; }
</style>
Try It Yourself

How It Works

The wrapper’s padding: 0 20px adds space on the left and right of the iframe inside the parent document. Combine with vertical padding to replace both marginwidth and marginheight.

Dynamic Values with JavaScript

Legacy code used iframe.marginWidth; prefer updating CSS padding instead:

dynamic-marginwidth.html
<iframe id="myIframe" src="example.html" title="Embedded"></iframe>

<script>
  var iframe = document.getElementById("myIframe");
  iframe.marginWidth = 20; // legacy — deprecated
  iframe.parentElement.style.padding = "0 20px"; // modern
</script>
Try It Yourself

How It Works

The IDL property marginWidth (note camelCase) mirrored the content attribute in legacy browsers. Updating wrapper style.padding gives the same visual effect with full modern browser support.

♿ Accessibility

  • Always set title on iframes — Screen readers use it to describe embedded content when no accessible name is otherwise available.
  • Horizontal spacing improves readability — Adequate padding around embedded widgets helps all users distinguish iframe content from surrounding page content.
  • Use CSS, not marginwidth — Reliable spacing ensures consistent visual separation for low-vision users across browsers.
  • Embedded page accessibility — Spacing inside the iframe depends on the embedded document’s own HTML and CSS; ensure that page meets WCAG guidelines too.
  • Avoid framesets — Legacy <frame> layouts with marginwidth were difficult for assistive technology; modern single-page layouts with semantic HTML are preferred.

🧠 How marginwidth Worked (Historical)

1

Author sets marginwidth

Pixel value on iframe.

Markup
2

Browser insets viewport

Left and right internal gap.

Render
3

Embedded content laid out

Content avoids side edges.

Display
=

Spaced iframe content

Today: CSS padding on wrapper.

Browser Support

The marginwidth attribute is deprecated in HTML5. Modern browsers largely ignore it or treat it as a legacy hint. CSS horizontal padding is the reliable cross-browser approach for iframe spacing.

⚠️ Deprecated · Use CSS

Do not rely on marginwidth

Validators flag the attribute. CSS wrapper padding works everywhere.

Legacy Obsolete attribute
Google Chrome Deprecated / ignored
Obsolete
Mozilla Firefox Deprecated / ignored
Obsolete
Apple Safari Deprecated / ignored
Obsolete
Microsoft Edge Deprecated / ignored
Obsolete
marginwidth on <iframe> Use CSS instead

Bottom line: Never use marginwidth in new code. CSS padding: 0 20px on a wrapper is supported and predictable.

💡 Best Practices

✅ Do

  • Use CSS horizontal padding on a wrapper around iframes
  • Set title on every iframe for accessibility
  • Combine vertical and horizontal padding to replace both legacy attrs
  • Use CSS margin for spacing the iframe in the page layout
  • Remove legacy marginwidth when refactoring old markup

❌ Don’t

  • Add marginwidth to new iframe elements
  • Assume it affects all four sides (that was a common mistake)
  • Confuse marginwidth with CSS margin or width
  • Rely on iframe.marginWidth for layout
  • Use framesets and <frame> in new projects

Conclusion

The marginwidth attribute was a legacy way to add left and right internal spacing inside <iframe> elements in pixels. HTML5 deprecated it in favor of CSS. Modern browsers do not depend on it, and validators recommend removing it from markup.

For new projects, wrap iframes in a container with horizontal padding, use CSS margin for external page layout, and style embedded documents directly when possible. Together with marginheight, these legacy attributes are fully replaced by a single padding declaration on a wrapper.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about marginwidth

Bookmark these before embedding iframes.

5
Core concepts
↔️ 02

Left + right

Internal horizontal px.

Meaning
📝 03

Not all sides

Not marginheight.

Compare
⚠️ 04

Deprecated

Use CSS padding.

Status
↕️ 05

marginheight

Vertical partner.

Related

❓ Frequently Asked Questions

It set the left and right internal margin in pixels between an iframe’s border and its embedded content. The attribute is deprecated and largely ignored in modern browsers.
No. marginwidth affects left and right only. Top and bottom spacing was controlled by the separate marginheight attribute. This is a common misconception in older tutorials.
Historically <iframe> and obsolete <frame> elements. It is not valid on other HTML elements.
No. marginwidth controlled space inside the iframe viewport on the left and right. CSS margin on an iframe affects spacing around the iframe in the parent page layout.
Legacy code used iframe.marginWidth = 20 (camelCase) or setAttribute("marginwidth", "20"). Prefer iframe.parentElement.style.padding = "0 20px" instead.
No. Use CSS horizontal padding on a wrapper element or style the embedded document. Remove marginwidth when updating legacy iframe markup.

Space iframe content horizontally the modern way

Explore legacy marginwidth markup and CSS padding replacements in the Try It editor.

Try CSS replacement example →

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.

5 people found this page helpful