HTML hspace Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The hspace attribute sets horizontal white space in pixels on the left and right of an <img> (and historically <object>). For example, hspace="20" adds 20px on each side.

Its vertical twin is vspace. For new pages, use CSS margin instead of either attribute.

Deprecated.

Presentational HTML. MDN: use the CSS margin property (or margin-left / margin-right) instead. Keep this page for reading legacy markup.

Still rendered in many browsers.

Support remains for compatibility, but it is not recommended for new projects or modern responsive layouts.

HTML Syntax

syntax.html
<img hspace=" pixels">

pixels is a non-negative integer (CSS pixels of space on both the left and the right).

Attribute Values

Legacy value form and the modern replacement:

Value / approachDescriptionTry it
integerPixels of space on left and right (e.g. 20)Try it
0No extra horizontal space from hspaceTry it
CSS marginRecommended replacement for left/right spacingTry it
With vspaceOld pages often set both; both are deprecatedTry it

Live Preview

Change the legacy hspace value and watch the gap beside the image. Prefer CSS for real projects.

Demo only. New layouts should use CSS margin.

Image + text

HTML logo Surrounding text shows the horizontal gap created by the legacy attribute.

hspace="20" (deprecated demo)

HTML Examples

Short snippets for study. Prefer the CSS example for new work. Use Try It Yourself for a live editor.

Example 1 — Legacy Image hspace

How old pages added left/right space around an image.

HTML
<p>
  <img
    src="/images/html.png"
    alt="HTML logo"
    width="64"
    height="64"
    hspace="20"
    align="left">
  Text wraps beside the image with 20px of space on each side.
</p>
Try It Yourself

How It Works

  1. hspace is pixels. 20 means 20px on the left and 20px on the right.
  2. Applies to the image. Space sits between the image box and nearby content.
  3. align is also legacy. Old demos often paired align="left" with hspace; both are deprecated.
  4. Study only. Do not copy this pattern into new production pages.

Example 2 — Modern CSS Replacement

Same idea with margin — the approach MDN recommends.

HTML
<p>
  <img
    src="/images/html.png"
    alt="HTML logo"
    width="64"
    height="64"
    style="float: left; margin: 0 20px 8px 0;">
  Same visual spacing using CSS margin instead of hspace.
</p>
Try It Yourself

How It Works

  1. Replace hspace. Horizontal gaps become margin-left / margin-right (or shorthand).
  2. More control. CSS can set unequal sides, rem units, or responsive rules.
  3. Prefer a stylesheet. Move spacing out of inline styles for real sites.
  4. Same for vspace. Top/bottom space belongs in margin-top / margin-bottom.

Example 3 — hspace with vspace

Legacy pages often set both axes of presentational spacing.

HTML
<img
  src="/images/html.png"
  alt="HTML logo"
  width="64"
  height="64"
  hspace="16"
  vspace="8">
Try It Yourself

How It Works

  1. hspace="16". 16px left and 16px right.
  2. vspace="8". 8px above and 8px below.
  3. Both deprecated. Replace with one CSS margin rule, e.g. margin: 8px 16px;.
  4. Migration tip. Search the codebase for hspace= and convert matches to CSS.

Edge Cases & Pitfalls

Treat hspace as history. Modern layout needs CSS, not presentational attributes.

deprecated

Do not use in new code

Validators and style guides flag hspace. Use CSS margins for all new markup.

both sides

Always left and right

hspace cannot set only one side. CSS can: set margin-left or margin-right alone.

units

Pixels only

The attribute takes an integer pixel count. CSS allows rem, %, and responsive values.

css wins

Stylesheets override clarity

Mixing hspace with CSS margin makes spacing hard to reason about. Pick CSS and remove the attribute.

a11y

Keep alt text

Spacing attributes do not replace a meaningful alt on images.

Browser Support

hspace is a deprecated HTML attribute kept for legacy pages. Prefer CSS margin in modern code.

✓ Deprecated · Legacy support

hspace

Still honored in many browsers for old content, but removed from modern authoring guidance. Do not rely on it for new layouts.

Legacy Compatibility focus
Google Chrome Legacy support
Supported
Mozilla Firefox Legacy support
Supported
Apple Safari Legacy support
Supported
Microsoft Edge Legacy support
Supported
Opera Legacy support
Supported
Samsung Internet Legacy support
Supported
Internet Explorer Supported
Supported
hspace Legacy / deprecated

Bottom line: Expect old pages to still show hspace gaps, but migrate to CSS margin whenever you touch the markup.

Key Takeaways

  • Role: legacy left/right pixel spacing on images.
  • Status: deprecated — do not use in new HTML.
  • Replace with: CSS margin / margin-inline.
  • Pair: vspace is the vertical twin — also deprecated.

One line: recognize hspace in old HTML, then space images with CSS margin.

Frequently Asked Questions

It sets horizontal white space in pixels on the left and right of elements such as img (and historically object). For example, hspace="20" adds 20px on each side.
Yes. MDN lists hspace as a deprecated presentational attribute. New code should use CSS margin (or margin-left / margin-right / margin-inline) instead.
It was used mainly on img, and also on older object (and related) embeds. Prefer CSS spacing for all of them today.
hspace adds left and right space. vspace adds top and bottom space. Both are deprecated in favor of CSS margin.
Use margin for equal sides, or margin-left and margin-right for control. Example: style="margin-left: 20px; margin-right: 20px;" or a stylesheet rule.
Yes, briefly — you may see it in old pages. Understand it, then migrate spacing to CSS for maintainable layouts.

Next: http-equiv

Pragma directives on the meta element.

http-equiv tutorial →

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.

8 people found this page helpful