👀 Live Preview
CSS vertical margins (modern equivalent of vspace="8"):
Text above the image.
Text below the image.

The vspace attribute was historically used to specify vertical space in pixels above and below an <img> element. It helped separate images from surrounding text in early web layouts.
Although vspace is now obsolete in HTML5 and replaced by CSS, you may still encounter it in legacy markup. Understanding it makes older pages easier to read and refactor.
Numeric values.
Both sides.
Main use case.
Vertical vs horizontal.
Modern fix.
Migrate safely.
The vspace attribute was designed to add vertical spacing around images within a document. A single number applied the same gap above and below the image, improving readability when inline images sat between paragraphs of text.
Presentation attributes like vspace mixed layout with content. HTML5 moved spacing concerns to CSS for better control, responsiveness, and maintainability.
Replace vspace="10" with margin-top: 10px; margin-bottom: 10px; (or a utility class) on the image.
Add vspace with a non-negative pixel number on an image (legacy only):
<img src="photo.jpg" alt="Sample" vspace="10">10 = 10px top and bottom).<img>; also seen on <object> and <embed> in old pages.hspace for horizontal spacing.style.marginTop / marginBottom, not vspace.The vspace attribute accepts a non-negative integer representing pixels of vertical space:
0 — No extra space above or below the image.10 — Adds 10 pixels of space above and below the image.20 — Adds 20 pixels above and below (common in legacy layouts).<img src="image.jpg" alt="Sample Image" vspace="10">In this example, vspace="10" tells the browser to leave 10 pixels of vertical gap both above and below the image relative to adjacent content.
| Legacy attribute | Effect | CSS equivalent |
|---|---|---|
vspace="10" | 10px above + below | margin-top/bottom: 10px |
hspace="10" | 10px left + right | margin-left/right: 10px |
| Both together | Space on all four sides | margin: 10px 10px |
| Status | Obsolete | Use CSS in new work |
| Modern shorthand | — | margin-block: 10px |
| JavaScript | — | img.style.marginTop = "10px" |
| Element | Supported? | Notes |
|---|---|---|
<img> | Historical | Primary use case for vspace |
<object>, <embed> | Historical | Legacy plugin/embed spacing |
<input type="image"> | Historical | Rare; CSS preferred |
<div>, <p> | No | Use CSS margin/padding |
| All elements (CSS) | Modern | margin, padding, gap work everywhere |
vspace vs hspace vs CSS| Method | Direction | Status |
|---|---|---|
vspace | Vertical (top + bottom) | Obsolete |
hspace | Horizontal (left + right) | Obsolete |
margin-top / margin-bottom | Vertical (independent control) | Recommended |
padding on wrapper | Any side | Flexible layout |
Compare obsolete vspace, the recommended CSS approach, and dynamic spacing with JavaScript.
CSS vertical margins (modern equivalent of vspace="8"):
Text above the image.
Text below the image.
Using vspace attribute (obsolete):
<img src="image.jpg" alt="Sample Image" vspace="20">vspace="20" adds 20 pixels of vertical space above and below the image. Browsers may still honor this on legacy pages, but validators flag it as obsolete.
Modern equivalent with full control over each side:
<img src="image.jpg" alt="Sample Image"
style="margin-top: 20px; margin-bottom: 20px;">CSS margins separate the image from surrounding content. Unlike vspace, you can set different top and bottom values and use responsive units (rem, em, %).
Adjust vertical spacing at runtime with CSS (not vspace):
<img id="dynamicImage" src="image.jpg" alt="Sample">
<script>
var img = document.getElementById("dynamicImage");
img.style.marginTop = "30px";
img.style.marginBottom = "30px";
</script>JavaScript sets inline margins for responsive layouts or user interactions. This replaces dynamically changing vspace in legacy code.
alt — Image spacing does not replace descriptive alternative text.On img element.
N px top + bottom.
Clearer separation.
Better control.
Browsers may still apply vspace on images for backward compatibility, but the attribute is obsolete in HTML5. Do not depend on it in new code; CSS margins work consistently everywhere.
Migrate vspace to CSS margins when updating old pages.
Bottom line: Replace with CSS margin-top and margin-bottom in all new and maintained code.
margin-top and margin-bottomvspace when reading legacy HTMLdisplay: block on images when neededvspace to new HTML5 documentshspace / margins)vspace; use CSS for layout and spacing.margin-top and margin-bottom.The vspace attribute once added vertical space around images in HTML, but it has been superseded by CSS. Understanding it helps you maintain legacy sites and appreciate why presentation moved to stylesheets.
By using CSS margins and JavaScript responsibly, you gain precise, responsive control over image spacing and cleaner, standards-compliant markup.
vspaceBookmark these before your next vspace implementation.
vspace added equal pixel space above and below an image.
The attribute is obsolete in HTML5.
SyntaxUse CSS margin-top / margin-bottom instead.
hspace was the horizontal companion attribute.
cellpadding applied to tables. vspace applied vertical space around images and similar replaced elements.vspace alone (same value both sides). CSS lets you set margin-top and margin-bottom independently.margin-block: 10px;.Compare legacy vspace with the CSS margin replacement.
5 people found this page helpful