HTML leftmargin Attribute
The obsolete HTML leftmargin attribute was historically used to specify the amount of space between the left edge of the browser viewport and the document content.
leftmargin attribute is obsolete and should not be used in modern HTML. Use CSS margins or padding to control spacing around page content.
Historical Syntax
The leftmargin attribute was commonly placed on the <body> element with a numeric value:
<body leftmargin="40">
...
</body>
In this example, supporting browsers were requested to leave 40 pixels of space between the left edge of the viewport and the document content.
Historical Elements
The non-standard leftmargin attribute was primarily associated with the <body> element.
| Element | Historical Purpose |
|---|---|
<body> |
Specified the amount of space between the left edge of the viewport and the document content. |
Historical Use
The leftmargin attribute provided a browser-specific method for changing the default space along the left side of a webpage. Authors could increase the value to move the page content farther from the left edge of the browser window.
It was often used with other non-standard margin attributes, including topmargin, rightmargin, and bottommargin, when authors wanted more control over the space surrounding the document.
Because these attributes were browser-specific, authors sometimes used multiple margin attributes in the same document to accommodate different browsers.
Why It Became Obsolete
The leftmargin attribute was a non-standard presentational feature rather than a reliable part of standard HTML. It also mixed visual layout instructions with the document's HTML structure.
CSS provides a standardized and much more flexible way to control page spacing. Margins and padding can be applied to the entire document or to individual layout containers, and each side can be controlled independently.
Modern Alternative
Use CSS instead of the obsolete leftmargin attribute. The margin-left property can add space along the left side of an element.
Historical HTML
<body leftmargin="40">
...
</body>
Modern HTML and CSS
<body>
<main class="page-content">
<h1>Example Page</h1>
<p>This content has space along its left side.</p>
</main>
</body>
<style>
.page-content {
margin-left: 40px;
}
</style>
In many modern layouts, padding on a page container is a better choice when the goal is to keep content away from the edges of the viewport:
.page-content {
padding-left: 40px;
padding-right: 40px;
}
Using a container also allows the page spacing to be changed for different screen sizes without applying presentational attributes to the <body> element.
Learn More About CSS Spacing
The CSS tutorials explain how margins and padding control the space around and inside elements:
- CSS Margins - Learn how to control the space outside an element.
- CSS Padding - Learn how to control the space between an element's content and its border.
- CSS Box Model - Learn how content, padding, borders, and margins work together.
Legacy Browser Support
The leftmargin attribute was a browser-specific feature used by older browsers rather than a dependable standard HTML attribute.
Modern browsers may continue to recognize some legacy uses for compatibility with older webpages, but new and updated webpages should use CSS margins or padding for page 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 used to control document margins include:
rightmargin- Historically used to control the right margin of a document.topmargin- Historically used to control the top margin of a document.bottommargin- Historically used to control the bottom margin of a document.hspace- Historically used to add horizontal space around certain elements.vspace- Historically used to add vertical space around certain elements.
Specifications
The leftmargin attribute was a non-standard presentational extension and should not be used in modern HTML. Use CSS margins or padding to control page spacing.
