HTML hspace Attribute

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

Introduction

The hspace attribute is an HTML feature used to define horizontal space around an element. It adds padding-like spacing on the left and right sides, which helps separate images and embedded content from surrounding text. While still recognized by many browsers, it is deprecated in HTML5—CSS margin is the modern replacement.

What You’ll Learn

01

Pixel Spacing

Left & right gaps.

02

Images

Common use case.

03

hspace vs vspace

Horizontal vs vertical.

04

CSS margin

Modern alternative.

05

JavaScript

Set hspace at runtime.

06

Deprecation

Legacy attribute note.

Purpose of hspace Attribute

The main purpose of the hspace attribute is to specify the amount of horizontal space, in pixels, added on both the left and right sides of an element. This attribute is commonly used with images and embedded objects to adjust their positioning within a layout and keep text from sitting flush against the element.

For example, an image with hspace="20" gets 20 pixels of space on its left edge and 20 pixels on its right edge—similar to setting margin-left: 20px and margin-right: 20px in CSS.

⚠️
Deprecated in HTML5

The hspace attribute is not recommended for new projects. Browsers may still render it on legacy elements, but CSS provides better control and is the standard approach today.

📝 Syntax

Add hspace to a supported element with a non-negative pixel value:

hspace.html
<img src="example.jpg" alt="Example Image" hspace="20">

Syntax Rules

  • Value is a non-negative integer representing pixels of horizontal space on both left and right sides.
  • Historically used on <img>, <object>, and <iframe> elements.
  • Pair with vspace for vertical spacing above and below the element.
  • Deprecated in HTML5—use CSS margin-left and margin-right for new code.
  • Does not replace meaningful alt text or semantic markup on images.

💎 Values

The hspace attribute accepts a numerical value representing the amount of horizontal space, in pixels, to be added on each side of the element:

  • hspace="0" — No extra horizontal space (default behavior).
  • hspace="10" — 10 pixels of space on the left and 10 pixels on the right.
  • hspace="20" — 20 pixels of space on each side; a common choice for image spacing.

Values should be zero or positive integers. Negative values are not meaningful for hspace and should not be used.

hspace-values.html
<!-- Legacy HTML spacing -->
<img src="photo.jpg" alt="Photo" hspace="15" vspace="10">

<!-- Modern CSS equivalent (recommended) -->
<img src="photo.jpg" alt="Photo" style="margin: 10px 15px;">

⚡ Quick Reference

Use Casehspace ValueNotes
No extra space0 or omitDefault
Small gap8 or 10Subtle separation
Medium gap20Common image spacing
CSS replacementmargin: 0 20px;Recommended today
Vertical counterpartvspaceTop & bottom spacing
HTML5 statusDeprecatedUse CSS margin

Applicable Elements

ElementSupported?Notes
<img>Yes (legacy)Most common use—space around images in text flow
<object>Yes (legacy)Embedded object spacing
<iframe>Yes (legacy)Embedded frame spacing
<p>, <div>NoUse CSS margin or padding instead
Modern HTML5DeprecatedPrefer CSS for all new projects

hspace vs vspace vs CSS margin

Attribute / CSSDirectionModern Replacement
hspace="20"Left & right (horizontal)margin-left: 20px; margin-right: 20px;
vspace="10"Top & bottom (vertical)margin-top: 10px; margin-bottom: 10px;
hspace + vspaceAll four sidesmargin: 10px 20px; (vertical horizontal)

Examples Gallery

Basic image spacing, comparison with CSS margin, and dynamic hspace updates with JavaScript.

👀 Live Preview

Image with horizontal spacing (CSS margin shown as the modern equivalent of hspace):

Sample photo

This text sits next to the image with horizontal space on both sides of the image, keeping the layout readable and visually balanced.

Preview uses CSS margin-right: 20px (the modern equivalent of hspace="20").

Example — Basic Image Spacing

Let’s illustrate how to use the hspace attribute with an image:

hspace.html
<img src="example.jpg" alt="Example Image" hspace="20">
Try It Yourself

How It Works

In this example, the hspace attribute is set to 20, indicating that 20 pixels of horizontal space should be added on both sides of the image, separating it from adjacent content.

Example — CSS Margin Alternative (Recommended)

Achieve the same spacing with modern CSS instead of the deprecated attribute:

hspace-css.html
<img
  src="example.jpg"
  alt="Example Image"
  style="margin-left: 20px; margin-right: 20px;">

<!-- Or with a class -->
<style>
  .spaced-img { margin: 0 20px; }
</style>
<img class="spaced-img" src="example.jpg" alt="Example Image">
Try It Yourself

How It Works

margin-left and margin-right (or the shorthand margin: 0 20px) replicate hspace="20" with full CSS control over responsive breakpoints, units, and design systems.

Dynamic Values with JavaScript

You can dynamically set the hspace attribute using JavaScript to adjust spacing based on conditions or user interactions:

dynamic-hspace.html
<img id="dynamicImage" src="example.jpg" alt="Example Image">

<script>
  document.getElementById("dynamicImage").hspace = "10";
</script>
Try It Yourself

How It Works

In this script, the hspace attribute is set to 10 for an image with the id dynamicImage. For new code, prefer updating element.style.marginLeft and marginRight or toggling a CSS class instead.

♿ Accessibility

  • Spacing is visual onlyhspace affects layout appearance but does not convey meaning to assistive technologies.
  • Always include alt text — Horizontal spacing on images does not replace descriptive alternative text for screen reader users.
  • Prefer CSS for responsive layouts — Fixed pixel spacing from hspace may not adapt well on small screens; CSS media queries handle this better.
  • Avoid layout-only attributes for structure — Use semantic HTML and CSS for spacing rather than deprecated presentational attributes when building accessible pages.

🧠 How hspace Works

1

Author sets hspace value

Pixel count for left and right gaps.

Markup
2

Browser adds horizontal space

Space on both sides of the element.

Render
3

Content flows around element

Text and inline content respect the gap.

Layout
=

Clearer visual separation

Images and embeds no longer touch surrounding text.

Browser Support

The hspace attribute is deprecated but still recognized by most browsers on legacy elements like <img>. Support may vary; CSS margin is reliable across all modern browsers and is the recommended approach.

⚠️ Legacy · Deprecated

Legacy browser support

Major browsers still render hspace on supported elements, but the attribute is obsolete in HTML5 specifications.

90% Legacy support
Google Chrome Legacy support
Deprecated
Mozilla Firefox Legacy support
Deprecated
Apple Safari Legacy support
Deprecated
Microsoft Edge Legacy support
Deprecated
Internet Explorer Fully supported (legacy)
Deprecated
Opera Legacy support
Deprecated
hspace attribute Use CSS margin instead

Bottom line: Recognized for legacy compatibility, but CSS margin is the modern standard for horizontal spacing.

💡 Best Practices

✅ Do

  • Use CSS margin for horizontal spacing in new projects
  • Test spacing across different screen sizes and resolutions
  • Apply spacing purposefully to improve readability
  • Understand legacy hspace when maintaining older HTML
  • Pair image spacing with meaningful alt attributes

❌ Don’t

  • Use hspace in new HTML5 projects
  • Apply excessive spacing that breaks visual consistency
  • Rely on negative values—they are not valid for hspace
  • Mix deprecated spacing attributes with inconsistent pixel values
  • Forget responsive design—fixed attributes do not adapt like CSS

Conclusion

The hspace attribute is a useful legacy tool for adjusting horizontal spacing around HTML elements, especially images. By understanding how it works, you can read and maintain older webpages that still use it.

For new development, reach for CSS margin instead. It gives you the same visual control with better flexibility, responsiveness, and alignment with modern web standards.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about hspace

Bookmark these when working with legacy HTML or migrating to CSS.

5
Core concepts
🖼️ 02

Images

Most common element

Scope
📏 03

Pixel Values

0, 10, 20, etc.

Values
⚠️ 04

Deprecated

Use CSS margin

HTML5
↕️ 05

vspace Pair

Vertical counterpart

Related

❓ Frequently Asked Questions

It adds horizontal space in pixels on the left and right sides of an element, commonly used to separate images from surrounding text.
No. hspace is deprecated in HTML5. Browsers may still render it on legacy elements, but CSS margin is the recommended replacement.
Historically <img>, <object>, and <iframe>. For new code, apply CSS margin to any element.
hspace controls left and right spacing. vspace controls top and bottom spacing. Both are deprecated in favor of CSS.
No. hspace expects zero or positive pixel values. Use CSS with negative margins only when you intentionally need overlapping layout effects.
Yes. Use imageElement.hspace = 10. For new projects, prefer updating CSS margin properties or classes instead.

Master legacy and modern spacing

Practice the hspace attribute and its CSS margin alternative in the Try It editor.

Try image spacing 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