SarahHTML Basics: Images

Written by Sarah from Stuff By Sarah

Once you’ve got your text on a page, the next thing that you usually want to add are images. Images can be used to add visuals to content, or can be used to improve the overall design of a site.

To display images on the page you need to use the tag img. Note, this is for images within the content. Background images should be set using the CSS background property.

The basic use of the image tag is

<img src="images/image.gif" alt="My Image" />

The image tag accepts a number of attributes:

src
This is the source ie. the path to the image. This can be the full URL, absolute to the root or relative to the page. This is required.
alt
This is the alternative text. This is is displayed when the image doesn’t load. This is also read by search engines and screen readers (for visually impaired visitors). This is required.
height
This sets the height of the image. If it’s unused, the true image height will be used.
width
This sets the width of the image. If it’s unused, the true image width will be used.
longdesc
This accepts a URL to a longer description of the image.
usemap
This defines the image as an image map

For a more in depth explanation on longdesc I recommend “longdesc, it’s not just alternate text“. We will cover image maps another time as these will need their own post!

Image tags can also accept the standard general attributes of id, class, title, style and lang. So, for example, the Blogging Tips logo on this site uses the following image code

<img src="http://www.bloggingtips.com/wp-content/themes/BloggingTips/images/logo.gif" alt="Blogging Tips" />

You could also use

<img src="http://www.bloggingtips.com/wp-content/themes/BloggingTips/images/logo.gif" alt="Blogging Tips" width="472" height="77" id="btlogo" />

Which defines the height and width of the image, along with giving the image an id for use in the CSS.

Linking Images

When it comes to linking images, you can use the same properties as linking pages, and just treat the image in the same way as you’d treat text. For example, the Blogging Tips logo is linked back to the front end of the site ie.

<a href="http://www.bloggingtips.com"><img src="http://www.bloggingtips.com/wp-content/themes/BloggingTips/images/logo.gif" alt="Blogging Tips" /></a>

This will then link the whole image.

Image CSS properties

There are deprecated attributes for the image tag that can be recreated using CSS. These properties are

border
This gives the image a border. Set it to ‘none’ to turn off the border, especially when the image is linked, or else you will get a border, the colour of the links, around the image.
margin
This recreates the vspace (vertical margin) and hspace (horizontal margin) attributes.
float
This recreates the align attribute, however it requires a lot more explanation before it can be used without causing layout problems.

To use these in your style sheet you can use:

img { border: none }
img {
   border: 1px;
   margin: 5px;
}
img {
   border: none;
   margin: 0 10px 10px 0;
}

I’ll be explaining the border properties in more depth later on, but here I’ve simply used the property value and given it one value of none or a value in pixels, for the thickness of the border. For the margin property it can accept 1, 2, 3 or 4 values:

  • 1 value: Sets the margin for all sides ie. the top, right, bottom and left margins.
  • 2 values: The first value sets the top and bottom margins, the second sets the left and right margins.
  • 3 values: The first value sets the top margin, the second sets the left and right margin, the third sets the bottom margin.
  • 4 values: Sets the margin values in the order top right bottom left.

Images can be placed on a page on their own, within a div, a paragraph (although this should only be used alongside content and not just for images on their own), a header, a list or links. Just remember, if you use an image, always have an alt attribute that briefly explains what the image is or what it says, so that if the image doesn’t load then the visitor at least knows what should have been there. Even more important if your menu uses image buttons!

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on December 11th, 2008 and filed under Design & Coding
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx
  • BloggingTips Uses Aweber

5 Responses to “HTML Basics: Images”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Blog Expert says:

    That was a wonderful tutorial. Many people do not know how to use HTML so this will benefit those.

  2. Rob O. says:

    I also include a TITLE attribute with most of my images. This serves as an image caption or tooltip (visible on mouseover) for your image so not only does this provide more instructions for your readers, but it may also give search engines a little more to chew on.

  3. Sarah says:

    Hi Rob, I did mention the title attribute ;) You should only use a title attribute when it’s necessary though. If your alt attribute covers what the image is then a title attribute on an image is not really necessary. If you need to say more then you should really use the longdesc attribute instead.

    If you’re linking images then the title attribute, if needed, should be on the anchor tag and not the image tag.

    I’m sure you don’t, but a lot of people either ignore the alt attribute and use the title attribute (very incorrect), or duplicate the content in the alt and title attributes. From an accessibility point of view that’s very bad practice. At the end of the day, if the user can see the image then they don’t really need the tooltip, if they can’t then they see the alternative text.

    I’ve never come across an instance where a title attribute was actually needed on an image (although I’m not saying you never will, but it’s a rare thing).

  4. Rob O. says:

    I apologize, Sarah, I totally missed that you had mentioned the Title attribute.

    I actually do use both Alt & Title on nearly every image. I use the Alt tag to serve as a textual placeholder for browsers that either don’t support images (some mobile browsing platforms, for example) or have the image display option disabled (to speed up page loads, for example).

    I use the Title tag like a caption to be more descriptive of the image because most browsers will display that attribute as a pop-up (or tooltip) when you hover over an image with the cursor.

    I got in the habit of doing this to provide a teensy bit more info for my readers, but also for the sake of SEO. I’ll admit that this may be a little misguided, however, because I haven’t seen anything definitive that the extra info is actually referenced by Google et al. But it’s just rote habit now – since my site is nearly all hand-coded, I can whip out basic HTML tags in my sleep!

Trackbacks

  1. [...] HTML Basics: Images in [...]

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Follow us on Facebook Find Out More About Our Newsletter

Sponsors

Blogging Tips Newsletter

Our WordPress Themes

 

Our Free E-Books