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.
Fundamentals
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.
Foundation
📝 Syntax
Add hspace to a supported element with a non-negative pixel value:
Text flows beside the image with 20 pixels of horizontal space on each side of the image.
hspace="20" adds 20 px on the left and 20 px on the right.
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
<imgsrc="example.jpg"alt="Example Image"style="margin-left: 20px; margin-right: 20px;"><!-- Or with a class --><style>
.spaced-img { margin: 0 20px; }
</style><imgclass="spaced-img"src="example.jpg"alt="Example Image">
Same visual result using CSS margin instead of the deprecated hspace attribute.
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:
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.
A11y
♿ Accessibility
Spacing is visual only — hspace 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.
Compatibility
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 ChromeLegacy support
Deprecated
Mozilla FirefoxLegacy support
Deprecated
Apple SafariLegacy support
Deprecated
Microsoft EdgeLegacy support
Deprecated
Internet ExplorerFully supported (legacy)
Deprecated
OperaLegacy support
Deprecated
hspace attributeUse CSS margin instead
Bottom line: Recognized for legacy compatibility, but CSS margin is the modern standard for horizontal spacing.
Pro Tips
💡 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
Wrap Up
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.