Menu
Adding Space Around an Image

The img tag, used to display an image on a webpage, creates an inline html element. That means that as the web browser comes upon the html code for the image, it displays the image on the same baseline as the text, right along with the text, as shown below.

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?" Inline img element So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.

If you want the image to appear on its own separate line, you can place a <br /> (line break) tag before and after the code for the image, or nest the code for the image within <p> </p> (paragraph) tags as shown below.

<p><img border="0" width="100" height="100" src="images/cherry.jpg" /></p>

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?"
Image on its own line
So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.

Another way to get some space around an image is set its border attribute to something greater than 0, as shown below.

<img border="20" width="100" height="100" src="cherry.jpg" />

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?" Image with border So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.

Note that the border color is black by default. With CSS (styles) you can set the boarder to any color you want. Since this is about using only html, I won't go into how to use styles.

You can create some space on each side of the image by using the hspace attribute as shown below.

<img hspace="50" border="0" width="100" height="100" src="images/cherry.jpg" />

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?" Using hspace attribute So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.

You can create some space on each side of the image by using the vspace attribute as shown below.

<img vspace="50" border="0" width="100" height="100" src="cherry.jpg" />

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?" Using vspace attribute So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.

You can use the hspace and vspace attributes together to clear some space around the image as shown below.

<img hspace="20" vspace="20" border="0" width="100" height="100" src="cherry.jpg" />

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?" Using hspace and vspace attributes So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.

Although through html alone you have some control over the space around an image, today CSS is the prefered method of laying out web page elements. In fact the hspace and vspace attributes were deprecated in HTML 4.01, meaning that they may not be supported in future browsers. And, although technically they're not supported in html version 5, if you specify your webpage as html 5, they still work in all modern browsers.


Learn more at amazon.com

More HTML Code:
• Create a Meta Tag Slide Show - No Java Script Required
• Nesting HTML Lists
• How to Write and Run HTML
• Setting the Number of Items Visible in a Select List
• Aligning an Image on Your Web Page
• Radio Button Basics
• The Font Tag
• Web Page Template
• Semantic (X)HTML: Markup with Meaning
• HTML abbr and acronym Tag