Basic Images



index
Disabled back button Next Section
printable version

Section 1: HTML for Images

Images are defined with the <img> tag.


src

The most important attribute of the image tag is src (meaning source).


alt

The required alt attribute specifies an alternate text for an image, if the image cannot be displayed, such as with a screen reader, and can assist people with visual impairments in understanding the content or purpose of the image on the page.


title

The optional title attribute represents advisory information for the element, such as would be appropriate for a tooltip.

Section 2: CSS for Images: Centering

A common task for CSS is to horizontally center an image.To center an image you must specify multiple CSS property:value pairs:

The display property must be set to block. Block will be discussed more later, but a block element is an element that takes up the full width available, and has a line break before and after it.

display: block;

Block elements can be center-aligned by setting the left and right margins to "auto".

margin-left:auto;
margin-right:auto;

Here is an example of a centered image. Inline CSS is used only for demonstration purposes.

<img src = "figures/pigInternet.jpg" alt="World Wide Pig"
style="display:block; margin-left:auto; margin-right:auto;">

World Wide Pig
Section 3: Image Size

To display a full-size image it is usually adequate to specify only the width in units like pixels or ems.

<img src="figures/pigHula.gif" alt="Hula pig." style="width:122px;" >

Hula pig.

If you specify an image width using a percentage, the browser will interpret that as the percentage of the container in which the image is placed, like the body tag, a div tag, or a figure tag.

If you want an image to be a percentage of its natural size, you can use special code like the following:

<img src="example.png" alt="example image."
onload="this.width*=0.5; this.onload=null;" >

Section 4: Image Formats

JPEG or JPG was named for the committee that created the standard, the Joint Photographic Experts Group.

GIF stands for Graphics Interchange Format

PNG stands for Portable Network Graphics – pronounced "ping" or spelled out P-N-G

BMP

SVG stands for Scalable Vector Graphic.

Section 5: Resources

Advanced Images (INFO 2220 Supplement)

Image Maps (INFO 2220 Supplement)

Web Image Formats & Google’s WebP

Image Compression (INFO 2220 Supplement)

Image Optimization (INFO 2220 Supplement)

Multimedia Part 1 (INFO 1110 notes)

Multimedia Part 2 (INFO 1110 notes)