Lesson 9: Images
Wouldn't it be great if you could have an image of actor and music legend David Hasselhoff right in the centre of your page?That sounds like a bit of a challenge...
Maybe, but in fact it is pretty easy to do. All you need is an element:Example 1:
<img src="david.jpg" alt="David" />
img
) and then where it is located (src
, short for "source"). Do you get the picture?Notice how the
img
element is opened and closed using the same tag. Like the <br />
tag, it is not tied to a piece of text."david.jpg" is the name of the image file you want to insert in your page. ".jpg" is the file type of the image. Just like the extension ".htm" shows that a file is an HTML document, ".jpg" tells the browser that a file is a picture. There are three different types of image file types you can insert into your pages:
- GIF (Graphics Interchange Format)
- JPG / JPEG (Joint Photographic Experts Group)
- PNG (Portable Network Graphics)
Traditionally, the GIF and JPEG formats have been the two dominant image types, but lately, the PNG format has become more and more popular (primarily at the expense of the GIF format). The PNG format contains in many ways the best of both the JPEG and GIF format: millions of colours and effective compressing.
Where do I get my images from?
To make your own images, you need an image editing program. An image editing program is one of the most essential tools you need to create beautiful websites.Unfortunately, no good image editing programs comes with Windows or other operating systems. Thus, you might consider investing in either Paint Shop Pro, PhotoShop or Macromedia Fireworks, which are three of the best image editing programs currently on the market.
However, as we said before, it will not be necessary to buy expensive programs to complete this tutorial. For now, you can download the excellent image editing program IrfanView which is so-called freeware and therefore costs nothing.
Or you can just borrow images from other sites by downloading them. But please be careful not to violate copyrights when downloading pictures. Still, it's useful to know how to download pictures, so here's how you do it:
- Right-click on an image on any image on the Internet.
- Choose "Save picture as..." in the menu that appears.
- Choose a location for the image on your computer and press "Save".
Now you can insert the image into one of your own pages. Try it yourself.
Is that all I need to know about images?
There are a few more things you should know about images.First, you can easily insert pictures located in other folders, or even pictures that are located on other websites:
Example 2:
<img src="images/logo.png" />
<img src="http://www.html.net/logo.png" />
Example 4:
<a href="http://www.html.net">
<img src="logo.png" /></a>
Are there any other attributes I should know about?
You always need to use the attributesrc
, which tells
the browser where the image is located. Besides that, there are a number
of other attributes which can be useful when inserting images.The
alt
attribute is used to give an alternate
description of an image if, for some reason, the image is not shown for
the user. This is especially important for users with impaired vision,
or if the page is loaded very slowly. Therefore, always use the alt
attribute:Example 5:
<img src="logo.gif" alt="HTML.net logo" />
alt
attribute, the aim is to provide an alternative description of the picture. The alt
attribute should not be used to create special pop-up messages for the
user since then visually impaired users will hear the message without
knowing what the picture is.The
title
attribute can be used to add information to the image:Example 6:
<img src="logo.gif" title="Learn HTML from HTML.net" />
Two other important attributes are
width
and height
:Example 7:
<img src="logo.png" width="141px" height="32px" />
width
and height
attributes can be
used to set the height and width of an image. The value that is used to
set the width and height is pixels. Pixels are the units of measurement
used to measure the resolution of screens. (The most common screen
resolution is 1024x768 pixels). Unlike centimetres, pixels are relative
units of measurement which depend on the resolution of the screen. To a
user with a high screen resolution, 25 pixels may correspond to 1
centimetre, while the same 25 pixel in a low screen resolution may
correspond to 1.5 centimetres on the screen.If you do not set the width and height, the image will be inserted in its actual size. But with width and height you can manipulate the size:
Example 8:
<img src="logo.gif" width="32px" height="32px" />
That said, it is still a good idea to use the width and height attributes because the browser will then be able to detect how much space the image will need in the final page layout before the image is fully downloaded. This allows your browser to set up the page nicely in a quicker way.
That's enough about David Hasselhoff and images for now.
No comments:
Post a Comment