HTML vspace Attribute

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

Introduction

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.

What You’ll Learn

01

Pixels

Numeric values.

02

Top + bottom

Both sides.

03

img focus

Main use case.

04

vs hspace

Vertical vs horizontal.

05

CSS margin

Modern fix.

06

Legacy code

Migrate safely.

Purpose of vspace Attribute

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.

💡
Use CSS margins today

Replace vspace="10" with margin-top: 10px; margin-bottom: 10px; (or a utility class) on the image.

📝 Syntax

Add vspace with a non-negative pixel number on an image (legacy only):

index.html
<img src="photo.jpg" alt="Sample" vspace="10">

Syntax Rules

  • Value is a number interpreted as pixels (e.g. 10 = 10px top and bottom).
  • Applies equal space above and below the element.
  • Most common on <img>; also seen on <object> and <embed> in old pages.
  • Obsolete in HTML5 — use CSS instead.
  • Pair historically with hspace for horizontal spacing.
  • JavaScript: set style.marginTop / marginBottom, not vspace.

💎 Values

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).
vspace-value.html
<img src="image.jpg" alt="Sample Image" vspace="10">

How It Works

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.

⚡ Quick Reference

Legacy attributeEffectCSS equivalent
vspace="10"10px above + belowmargin-top/bottom: 10px
hspace="10"10px left + rightmargin-left/right: 10px
Both togetherSpace on all four sidesmargin: 10px 10px
StatusObsoleteUse CSS in new work
Modern shorthandmargin-block: 10px
JavaScriptimg.style.marginTop = "10px"

Applicable Elements

ElementSupported?Notes
<img>HistoricalPrimary use case for vspace
<object>, <embed>HistoricalLegacy plugin/embed spacing
<input type="image">HistoricalRare; CSS preferred
<div>, <p>NoUse CSS margin/padding
All elements (CSS)Modernmargin, padding, gap work everywhere

vspace vs hspace vs CSS

MethodDirectionStatus
vspaceVertical (top + bottom)Obsolete
hspaceHorizontal (left + right)Obsolete
margin-top / margin-bottomVertical (independent control)Recommended
padding on wrapperAny sideFlexible layout

Examples Gallery

Compare obsolete vspace, the recommended CSS approach, and dynamic spacing with JavaScript.

👀 Live Preview

CSS vertical margins (modern equivalent of vspace="8"):

Text above the image.

Demo

Text below the image.

Implementation Example

Using vspace attribute (obsolete):

vspace-legacy.html
<img src="image.jpg" alt="Sample Image" vspace="20">

How It Works

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.

Implementation Example — CSS (recommended)

Modern equivalent with full control over each side:

vspace-css.html
<img src="image.jpg" alt="Sample Image"
     style="margin-top: 20px; margin-bottom: 20px;">

How It Works

CSS margins separate the image from surrounding content. Unlike vspace, you can set different top and bottom values and use responsive units (rem, em, %).

Dynamic Values with JavaScript

Adjust vertical spacing at runtime with CSS (not vspace):

dynamic-vspace.html
<img id="dynamicImage" src="image.jpg" alt="Sample">

<script>
  var img = document.getElementById("dynamicImage");
  img.style.marginTop = "30px";
  img.style.marginBottom = "30px";
</script>

How It Works

JavaScript sets inline margins for responsive layouts or user interactions. This replaces dynamically changing vspace in legacy code.

♿ Accessibility

  • Spacing aids readability — Adequate vertical space helps all users, including those with cognitive disabilities.
  • Do not rely on vspace alone — Use semantic HTML and CSS; spacing attributes do not help screen readers.
  • Always include alt — Image spacing does not replace descriptive alternative text.
  • Responsive design — CSS margins scale better than fixed pixel attributes on mobile.
  • Focus on content order — Logical DOM order matters more than pixel gaps for accessibility.

🧠 How vspace Worked

1

Set vspace=N

On img element.

HTML
2

Browser adds gap

N px top + bottom.

Layout
3

Text flows around

Clearer separation.

Render
=

Today: CSS margin

Better control.

Browser Support

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.

⚠️ Obsolete · Use CSS

Legacy support only

Migrate vspace to CSS margins when updating old pages.

CSS 100% modern
CSS margins Fully supported
Use this
vspace attr Legacy only
Obsolete
HTML5 validator Flags obsolete
Avoid
margin-block Modern CSS
Recommended
vspace attribute Obsolete

Bottom line: Replace with CSS margin-top and margin-bottom in all new and maintained code.

💡 Best Practices

✅ Do

  • Use CSS margin-top and margin-bottom
  • Recognize vspace when reading legacy HTML
  • Replace with classes in stylesheets
  • Test spacing on mobile and desktop
  • Combine with display: block on images when needed

❌ Don’t

  • Add vspace to new HTML5 documents
  • Mix many obsolete presentation attributes
  • Assume equal top/bottom is always desired
  • Use fixed pixels when responsive units fit better
  • Forget horizontal spacing (hspace / margins)
  • Avoid obsolete attributes like vspace; use CSS for layout and spacing.
  • For vertical spacing around images, use margin-top and margin-bottom.
  • Test pages across browsers to ensure consistent appearance.

🎉 Conclusion

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.

Key Takeaways

Knowledge Unlocked

4 truths every developer should know about vspace

Bookmark these before your next vspace implementation.

4
Core concepts
📝 02

The attribute is obsolete

The attribute is obsolete in HTML5.

Syntax
⚙️ 03

Use CSS margin-top /

Use CSS margin-top / margin-bottom instead.

Usage
🔒 04

hspace was the horizontal

hspace was the horizontal companion attribute.

Dynamic

❓ Frequently Asked Questions

No. cellpadding applied to tables. vspace applied vertical space around images and similar replaced elements.
Not with vspace alone (same value both sides). CSS lets you set margin-top and margin-bottom independently.
Some browsers may still honor it on legacy pages, but it is obsolete and should not be used in new HTML.
A modern CSS shorthand for block-axis margins (top and bottom in horizontal writing modes): margin-block: 10px;.
Yes. Replace with equivalent CSS margins in a stylesheet for easier maintenance and responsive design.

Practice image spacing

Compare legacy vspace with the CSS margin replacement.

Try the CSS 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