👀 Live Preview
Modern CSS wrapper with vertical padding (replaces marginheight):
The gray band above and below the iframe comes from wrapper padding: 20px 0—the reliable way to add vertical space today.

The marginheight attribute on <iframe> (and legacy <frame>) elements set the top and bottom 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 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.
Legacy frame too.
Vertical internal space.
Non-negative integer.
Use CSS instead.
Horizontal partner attr.
Internal vs external.
marginheight AttributeThe original purpose of marginheight was to add vertical breathing room inside an iframe—space between the iframe’s inner edge and where the embedded page content begins at the top and ends at the bottom. In early HTML layouts, authors used it alongside marginwidth to prevent embedded content from touching the iframe border.
HTML5 made presentation attributes like marginheight obsolete in favor of CSS. You may still see the attribute in legacy code or old tutorials. Understanding it helps when maintaining older sites, but new iframe layouts should rely on CSS padding, borders, and styling inside the embedded document.
margin on an iframemarginheight affected spacing inside the iframe viewport (between border and embedded content). CSS margin on an <iframe> element pushes the iframe box away from surrounding elements in the parent page. They solve different layout problems.
Legacy syntax on an iframe (shown for recognition only—do not use in new projects):
<iframe
src="example.html"
width="300"
height="200"
marginheight="20"
marginwidth="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: 20px 10px; /* vertical horizontal — replaces marginheight + marginwidth */
border: 1px solid #cbd5e1;
}
.iframe-wrap iframe {
width: 300px;
height: 200px;
border: 0;
}
</style><iframe> and obsolete <frame> elements.marginwidth for left and right internal margins.title on iframes for accessibility.The marginheight attribute accepts a non-negative integer representing pixels:
marginheight="0" — No extra top/bottom internal space (default in many browsers).marginheight="20" — 20 pixels of space at top and bottom inside the iframe.marginheight="8" — Small padding-like gap for compact embeds.// Legacy IDL property (camelCase) — avoid in new code
var iframe = document.getElementById("myIframe");
iframe.marginHeight = 30;
iframe.setAttribute("marginheight", "30");
// Modern replacement
iframe.parentElement.style.padding = "30px 0";| Concept | Legacy (avoid) | Modern replacement |
|---|---|---|
| Vertical internal space | marginheight="20" | Wrapper padding: 20px 0 |
| Horizontal internal space | marginwidth="10" | Wrapper padding: 0 10px |
| Space around iframe in page | N/A (wrong attribute) | CSS margin on iframe |
| Embedded page body spacing | marginheight (unreliable) | CSS in embedded document |
| JavaScript update | iframe.marginHeight = 30 | wrap.style.padding = "30px 0" |
| 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 | marginheight is iframe/frame-specific |
marginheight vs CSS margin vs CSS padding| Feature | marginheight | CSS margin on iframe | CSS padding on wrapper |
|---|---|---|---|
| What it affects | Inside iframe (top/bottom) | 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 |
| Horizontal partner | marginwidth | margin-left/right | padding-left/right |
Legacy iframe markup, modern CSS padding replacement, and dynamic spacing with JavaScript.
Modern CSS wrapper with vertical padding (replaces marginheight):
The gray band above and below the iframe comes from wrapper padding: 20px 0—the reliable way to add vertical space today.
Historical pattern setting 20 pixels of top and bottom internal margin:
<iframe
src="example.html"
width="300"
height="200"
marginheight="20"
title="Embedded page"></iframe>When supported, the browser inset the embedded document’s viewport from the top and bottom by the specified pixel value. Because the attribute is obsolete, behavior is inconsistent—migrate to CSS for predictable results.
Wrap the iframe and use padding for vertical spacing:
<div class="iframe-wrap">
<iframe src="example.html" title="Embedded page"></iframe>
</div>
<style>
.iframe-wrap {
padding: 20px 0;
border: 1px solid #cbd5e1;
}
.iframe-wrap iframe { border: 0; width: 100%; height: 200px; }
</style>The wrapper’s padding adds space around the iframe element inside the parent document. You can also set body { margin: 0; padding: … } inside the embedded page when you control its HTML.
Legacy code used iframe.marginHeight; prefer updating CSS padding instead:
<iframe id="myIframe" src="example.html" title="Embedded"></iframe>
<script>
var iframe = document.getElementById("myIframe");
iframe.marginHeight = 30; // legacy — deprecated
iframe.parentElement.style.padding = "30px 0"; // modern
</script>The IDL property marginHeight (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 marginheight were difficult for assistive technology; modern single-page layouts with semantic HTML are preferred.Pixel value on iframe.
Top and bottom internal gap.
Content avoids border edges.
Today: CSS padding on wrapper.
The marginheight attribute is deprecated in HTML5. Modern browsers largely ignore it or treat it as a legacy hint. CSS 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 marginheight in new code. CSS padding on a wrapper is supported and predictable.
title on every iframe for accessibilitymargin for spacing the iframe in the page layoutmarginheight when refactoring old markupmarginheight to new iframe elementsmarginiframe.marginHeight for layout<frame> in new projectsThe marginheight attribute was a legacy way to add top and bottom 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 padding, use CSS margin for external page layout, and style embedded documents directly when possible. Pair this knowledge with the related marginwidth attribute for complete legacy iframe spacing context.
marginheightBookmark these before embedding iframes.
Legacy elements only.
ScopeInternal vertical px.
MeaningInside vs outside box.
CompareUse CSS padding.
StatusHorizontal partner.
Related<iframe> and obsolete <frame> elements. It is not valid on other HTML elements.marginheight controlled space inside the iframe viewport. CSS margin on an iframe affects spacing around the iframe in the parent page layout.marginheight sets top and bottom internal margins; marginwidth sets left and right. Both are deprecated—replace with CSS padding on a wrapper.iframe.marginHeight = 30 (camelCase) or setAttribute("marginheight", "30"). Prefer iframe.parentElement.style.padding = "30px 0" instead.marginheight when updating legacy iframe markup.Explore legacy marginheight markup and CSS padding replacements in the Try It editor.
5 people found this page helpful