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.
Syntax
HTML Syntax
syntax.html
<img hspace="pixels">
pixels is a non-negative integer (CSS pixels of space on both the left and the right).
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
Surrounding text shows the horizontal gap created by the legacy attribute.
hspace="20" (deprecated demo)
Code
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>
Both deprecated. Replace with one CSS margin rule, e.g. margin: 8px 16px;.
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.
Compatibility
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.
LegacyCompatibility focus
Google ChromeLegacy support
Supported
Mozilla FirefoxLegacy support
Supported
Apple SafariLegacy support
Supported
Microsoft EdgeLegacy support
Supported
OperaLegacy support
Supported
Samsung InternetLegacy support
Supported
Internet ExplorerSupported
Supported
hspaceLegacy / deprecated
Bottom line: Expect old pages to still show hspace gaps, but migrate to CSS margin whenever you touch the markup.
Remember
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.