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

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.
Legacy frame too.
Horizontal internal space.
Non-negative integer.
Use CSS instead.
Vertical partner attr.
Internal vs external.
marginwidth AttributeThe 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.
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.
Legacy syntax on an iframe (shown for recognition only—do not use in new projects):
<iframe
src="example.html"
width="300"
height="200"
marginwidth="20"
marginheight="10"
title="Embedded page"></iframe>Modern CSS equivalent:
<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><iframe> and obsolete <frame> elements.marginheight for top and bottom internal margins.title on iframes for accessibility.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.// 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";| Concept | Legacy (avoid) | Modern replacement |
|---|---|---|
| Horizontal internal space | marginwidth="20" | Wrapper padding: 0 20px |
| Vertical internal space | marginheight="10" | Wrapper padding: 10px 0 |
| Space around iframe in page | N/A (wrong attribute) | CSS margin on iframe |
| Embedded page body spacing | marginwidth (unreliable) | CSS in embedded document |
| JavaScript update | iframe.marginWidth = 20 | wrap.style.padding = "0 20px" |
| Valid elements | iframe, frame | Any element via CSS |
| Element | Supported? | Notes |
|---|---|---|
<iframe> | Obsolete | Primary historical use; attribute deprecated |
<frame> | Obsolete | Legacy frameset; frames removed from modern HTML |
<embed>, <object> | No | Use CSS for spacing on these elements |
<div>, <img>, etc. | No | marginwidth is iframe/frame-specific |
marginwidth vs CSS margin vs CSS padding| Feature | marginwidth | CSS margin on iframe | CSS padding on wrapper |
|---|---|---|---|
| What it affects | Inside iframe (left/right) | Outside iframe in parent layout | Space around iframe inside wrapper |
| Status | Deprecated | Standard CSS | Standard CSS (recommended) |
| Units | Pixels only (integer) | px, rem, %, etc. | px, rem, %, etc. |
| Browser support today | Largely ignored | Full support | Full support |
| Vertical partner | marginheight | margin-top/bottom | padding-top/bottom |
marginwidth vs marginheight on <iframe>| Attribute | Example | Internal spacing |
|---|---|---|
marginwidth | marginwidth="20" | Left and right (horizontal) |
marginheight | marginheight="10" | Top and bottom (vertical) |
| CSS replacement | padding: 10px 20px | Vertical then horizontal (shorthand) |
Legacy iframe markup, modern CSS horizontal padding replacement, and dynamic spacing with JavaScript.
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.
Historical pattern setting 20 pixels of left and right internal margin:
<iframe
src="example.html"
width="300"
height="200"
marginwidth="20"
title="Embedded page"></iframe>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.
Wrap the iframe and use horizontal padding for left/right spacing:
<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>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.
Legacy code used iframe.marginWidth; prefer updating CSS padding instead:
<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>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.
title on iframes — Screen readers use it to describe embedded content when no accessible name is otherwise available.<frame> layouts with marginwidth were difficult for assistive technology; modern single-page layouts with semantic HTML are preferred.Pixel value on iframe.
Left and right internal gap.
Content avoids side edges.
Today: CSS padding on wrapper.
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.
Validators flag the attribute. CSS wrapper padding works everywhere.
<iframe> Use CSS insteadBottom line: Never use marginwidth in new code. CSS padding: 0 20px on a wrapper is supported and predictable.
title on every iframe for accessibilitymargin for spacing the iframe in the page layoutmarginwidth when refactoring old markupmarginwidth to new iframe elementsmargin or widthiframe.marginWidth for layout<frame> in new projectsThe 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.
marginwidthBookmark these before embedding iframes.
Legacy elements only.
ScopeInternal horizontal px.
MeaningNot marginheight.
CompareUse CSS padding.
StatusVertical partner.
Relatedmarginwidth affects left and right only. Top and bottom spacing was controlled by the separate marginheight attribute. This is a common misconception in older tutorials.<iframe> and obsolete <frame> elements. It is not valid on other HTML elements.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.iframe.marginWidth = 20 (camelCase) or setAttribute("marginwidth", "20"). Prefer iframe.parentElement.style.padding = "0 20px" instead.marginwidth when updating legacy iframe markup.Explore legacy marginwidth markup and CSS padding replacements in the Try It editor.
5 people found this page helpful