HTML marginwidth Attribute
The obsolete HTML marginwidth attribute was historically used to specify the amount of horizontal space between the left and right edges of a frame's viewport and the content displayed inside it.
marginwidth attribute is obsolete and should not be used in modern HTML. Use CSS margins or padding within the embedded document to control horizontal spacing around its content.
Historical Syntax
The marginwidth attribute accepted a numeric value representing the requested horizontal margin in pixels:
<iframe src="example.html" marginwidth="20" title="Example Content"></iframe>
In this example, supporting browsers were requested to leave 20 pixels of space along the left and right sides of the content inside the iframe.
Historical Elements
The marginwidth attribute was historically associated with inline frames and the obsolete frame element.
| Element | Historical Purpose |
|---|---|
<iframe> |
Specified the horizontal space between the iframe's viewport edges and its embedded document content. |
<frame> |
Specified horizontal spacing inside a frame in an obsolete frameset layout. |
Historical Use
Authors used marginwidth to control the amount of empty space along the left and right sides of a frame's content area. Increasing the value moved the content farther away from the frame's vertical edges.
The attribute was often used with marginheight, which controlled the corresponding space along the top and bottom.
<iframe src="example.html"
marginwidth="20"
marginheight="15"
title="Example Content"></iframe>
These attributes controlled spacing inside the frame. They did not specify the space between the iframe element and surrounding content in the parent webpage.
Why It Became Obsolete
The marginwidth attribute mixed presentational instructions with HTML markup. CSS provides a more flexible way to control spacing within a document, including separate values for the left and right sides.
The old <frame> and <frameset> elements also became obsolete. The <iframe> element remains available in modern HTML, but its embedded document should use CSS to control its own content spacing.
Modern Alternative
Use CSS inside the embedded document instead of placing marginwidth on the iframe. The CSS padding-left and padding-right properties can create space along the left and right sides of the embedded content.
Historical HTML
<iframe src="example.html" marginwidth="20" title="Example Content"></iframe>
Modern HTML
In the parent document, use an iframe without the obsolete attribute:
<iframe src="example.html" title="Example Content"></iframe>
In the embedded document, example.html, use CSS to control the spacing:
<style>
body {
margin: 0;
padding-left: 20px;
padding-right: 20px;
}
</style>
Setting the body's margin to zero removes its default margin, while the padding adds 20 pixels of space along the left and right sides of the content. CSS can also use different values for each side when needed.
Important: CSS applied to the iframe element in the parent document controls the iframe's own box. It does not directly control the spacing inside the embedded document. If the iframe loads a document from another website, the parent page generally cannot change that document's internal CSS.
Play in EditorLearn More About CSS Spacing
The CSS tutorials explain how margins and padding control the space around and inside elements:
- CSS Padding - Learn how to add space inside an element.
- CSS Margins - Learn how to control the space outside an element.
- CSS Box Model - Learn how content, padding, borders, and margins work together.
Legacy Browser Support
The marginwidth attribute was recognized by older browsers that supported traditional frame layouts and iframe presentation attributes.
Modern browsers may continue to recognize some legacy uses for compatibility with older webpages, but new and updated webpages should use CSS within the embedded document to control content spacing.
For additional information about historical browser compatibility, visit Can I Use? . Search for the HTML element or attribute you want to check. Search results can include related HTML features, element attributes, APIs, and other technologies, so select the result that most closely matches the legacy feature you are researching.
Related Attributes
Other obsolete HTML attributes historically associated with frame spacing and presentation include:
marginheight- Historically used to control vertical spacing inside a frame.frameborder- Historically used to control whether a frame border was displayed.framespacing- Historically used to control spacing between frames in older frameset layouts.scrolling- Historically used to control scrollbar behavior in frames.
Specifications
The marginwidth attribute is obsolete and should not be used in modern HTML. Use CSS within the embedded document to control its internal spacing. See the WHATWG HTML Living Standard for information about obsolete and non-conforming features.
