HyperText Markup Language/Images
Let us start with a quick minimum example:
<img src="turtleneck-sweater.jpeg" />
And let us also have a look at more extended example:
<img src="turtleneck-sweater.jpeg" alt="Photograph of a Turtleneck Sweater" title="Photograph of a Turtleneck Sweater" />
Images are normally stored in external files. To place an image into an HTML source, use the
img tag with the
src attribute containing the URL of the image file. To support browsers that cannot render images, you can provide the
alt attribute with a textual description of the image. To provide a tooltip to the image, use the
The space before the
/> in the examples is there on purpose. Some older browsers behave strangely if the space is omitted.
Placement[edit | edit source]
Per default, images are placed inline to their surroundings. To place the image as a block or float instead, you can use Cascading Style Sheets.
Alternative text and tooltip[edit | edit source]
The HTML specification requires that all images have an
alt attribute. This is commonly known as alt text. Images can be split in to two categories:
- those that add to the content of the page;
- those that are purely decorative, e.g. spacers, fancy borders and bullets.
Decorative images should have empty alt text, i.e.
alt="". Images used as bullets may use an asterisk,
All other images should have meaningful alt text. Alt text is not a description of the image, use the
title attribute for short descriptions. Alt text is something that will be read instead of the image. For example,
<img src="company_logo.png" alt="???"> makes the best widgets in the world.
The alt text should be the company's name not the ever popular 'Our logo', which would give the sentence 'Our logo makes the best widgets in the world.' when read in a text only browser.
alt attribute stands for alternate which non-graphic-capable browsers (such as Lynx) may use to better enable its user to understand the purpose of the image. Some older browsers incorrectly use the
alt attribute' tag to produce image tooltips. However, the
title attribute should actually be used for this.
Width and height[edit | edit source]
To have an image appear smaller than it is in the external file, use the attributes
height. When only one or the other is specified, images will scale to keep the same overall ratio.
Format[edit | edit source]
Most images for the web will be made using a limited number of formats. Below is a simple list of commonly supported formats.
- jpg / jpeg - Useful for photographs. Near universal support.
- webp - More efficient alternative to jpg. Also supports animation and transparency.
- png - Useful for images with transparency and simple or flat colors, such as cartoons.
- svg - Vector format with infinite resolution. Great for logos and simple geometry.
- gif - Very limited in color and resolution support, but commonly used for animations. Very inefficient format.