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:
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.
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.
There are deprecated attributes for the image tag that can be recreated using CSS. These properties are
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:
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!
Author comments are in a darker gray color for you to easily identify the posts author in the comments
[...] HTML Basics: Images in [...]
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
That was a wonderful tutorial. Many people do not know how to use HTML so this will benefit those.
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.
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).
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!