HTML marginheight Attribute

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

Introduction

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.

What You’ll Learn

01

<iframe> Only

Legacy frame too.

02

Top + Bottom

Vertical internal space.

03

Pixels

Non-negative integer.

04

Deprecated

Use CSS instead.

05

marginwidth

Horizontal partner attr.

06

Not CSS margin

Internal vs external.

Purpose of marginheight Attribute

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

⚠️
Not the same as CSS margin on an iframe

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

📝 Syntax

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

marginheight-legacy.html
<iframe
  src="example.html"
  width="300"
  height="200"
  marginheight="20"
  marginwidth="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: 20px 10px; /* 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 top and bottom internal margins.
  • Often paired with marginwidth for left and right internal margins.
  • Deprecated in HTML5—use CSS padding or embedded-document styles instead.
  • Always include a descriptive title on iframes for accessibility.

💎 Values

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.
  • Negative values are invalid—use zero or a positive integer only.
marginheight-js.html
// 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";

⚡ Quick Reference

ConceptLegacy (avoid)Modern replacement
Vertical internal spacemarginheight="20"Wrapper padding: 20px 0
Horizontal internal spacemarginwidth="10"Wrapper padding: 0 10px
Space around iframe in pageN/A (wrong attribute)CSS margin on iframe
Embedded page body spacingmarginheight (unreliable)CSS in embedded document
JavaScript updateiframe.marginHeight = 30wrap.style.padding = "30px 0"
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.Nomarginheight is iframe/frame-specific

marginheight vs CSS margin vs CSS padding

FeaturemarginheightCSS margin on iframeCSS padding on wrapper
What it affectsInside iframe (top/bottom)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
Horizontal partnermarginwidthmargin-left/rightpadding-left/right

Examples Gallery

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

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

Example — Legacy iframe with marginheight

Historical pattern setting 20 pixels of top and bottom internal margin:

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

How It Works

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.

Example — Modern CSS Replacement

Wrap the iframe and use padding for vertical spacing:

iframe-wrap.html
<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>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

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

dynamic-marginheight.html
<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>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Always set title on iframes — Screen readers use it to describe embedded content when no accessible name is otherwise available.
  • Spacing improves readability — Adequate padding around embedded widgets helps all users distinguish iframe content from surrounding page content.
  • Use CSS, not marginheight — 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 marginheight were difficult for assistive technology; modern single-page layouts with semantic HTML are preferred.

🧠 How marginheight Worked (Historical)

1

Author sets marginheight

Pixel value on iframe.

Markup
2

Browser insets viewport

Top and bottom internal gap.

Render
3

Embedded content laid out

Content avoids border edges.

Display
=

Spaced iframe content

Today: CSS padding on wrapper.

Browser Support

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.

⚠️ Deprecated · Use CSS

Do not rely on marginheight

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
marginheight on <iframe> Use CSS instead

Bottom line: Never use marginheight in new code. CSS padding on a wrapper is supported and predictable.

💡 Best Practices

✅ Do

  • Use CSS padding on a wrapper around iframes
  • Set title on every iframe for accessibility
  • Style spacing inside embedded pages when you control them
  • Use CSS margin for spacing the iframe in the page layout
  • Remove legacy marginheight when refactoring old markup

❌ Don’t

  • Add marginheight to new iframe elements
  • Confuse marginheight with CSS margin
  • Rely on iframe.marginHeight for layout
  • Set excessively large values causing wasted whitespace
  • Use framesets and <frame> in new projects

Conclusion

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

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about marginheight

Bookmark these before embedding iframes.

5
Core concepts
↕️ 02

Top + bottom

Internal vertical px.

Meaning
📝 03

Not CSS margin

Inside vs outside box.

Compare
⚠️ 04

Deprecated

Use CSS padding.

Status
↔️ 05

marginwidth

Horizontal partner.

Related

❓ Frequently Asked Questions

It set the top and bottom internal margin in pixels between an iframe’s border and its embedded content. The attribute is deprecated and largely ignored in modern browsers.
Historically <iframe> and obsolete <frame> elements. It is not valid on other HTML elements.
No. 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.
Legacy code used iframe.marginHeight = 30 (camelCase) or setAttribute("marginheight", "30"). Prefer iframe.parentElement.style.padding = "30px 0" instead.
No. Use CSS padding on a wrapper element or style the embedded document. Remove marginheight when updating legacy iframe markup.

Space iframe content the modern way

Explore legacy marginheight 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