HTML Images


Example

GIF Images

JPG Images

Mountain View

PNG Images

Graph

<!DOCTYPE html>
<html>

<body>
 <h2>Spectacular Mountains</h2>
 <img src="pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px">
</body>

</html>

Try it Yourself »

Note Always specify image size. If the size is unknown, the page will flicker while the image loads.


HTML Images Syntax

In HTML, images are defined with the <img> tag.

The <img> tag is empty, it contains attributes only, and does not have a closing tag.

The src attribute defines the url (web address) of the image:

<img src="url" alt="some_text">

The alt Attribute

The alt attribute specifies an alternate text for the image, if it cannot be displayed.

The value of the alt attribute should describe the image in words:

Example

<img src="html5.gif" alt="The official HTML5 Icon">

The alt attribute is required. A web page will not validate correctly without it.


HTML Screen Readers

Screen readers are software programs that can read what is displayed on a screen.

Used on the web, screen readers can "reproduce" HTML as text-to-speech, sound icons, or braille output.

Screen readers are used by people who are blind, visually impaired, or learning disabled.

Note Screen readers can read the alt attribute.


Image Size -  Width and Height

You can use the style attribute to specify the width and height of an image.

The values are specified in pixels (use px after the value):

Example

<img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">

Try it Yourself »

Alternatively, you can use width and height attributes.

The values are specified in pixels (without px after the value):

Example

<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">

Try it Yourself »

Width and Height or Style?

Both the width, the height, and the style attributes, are valid in the latest HTML5 standard.

We suggest you use the style attribute. It prevents styles sheets from changing the default size of images:

Example

<!DOCTYPE html>
<html>
<head>
<style>
  img { width:100%; }
</style>
</head>

<body>

<img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">
<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">

</body>

</html>

Try it Yourself »

Note At W3schools we prefer to use the style attribute.


Images in Another Folder

If not specified, the browser expects to find the image in the same folder as the web page.

However, it is common on the web, to store images in a sub-folder, and refer to the folder in the image name:

Example

<img src="/images/html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">

Try it Yourself »

If a browser cannot find an image, it will display a broken link icon:

Example

<img src="wrongname.gif" alt="HTML5 Icon" style="width:128px;height:128px">

Try it Yourself »

Images on Another Server

Some web sites store their images on image servers.

Actually, you can access images from any web address in the world:

Example

<img src="http://www.w3schools.com/images/w3schools_green.jpg">

Try it Yourself »

Animated Images

The GIF standard allows animated images:

Example

<img src="programming.gif" alt="Computer Man" style="width:48px;height:48px">

Try it Yourself »

Note that the syntax of inserting animated images is no different from non-animated images.


Using an Image as a Link

It is common to use images as links:

Example

<a href="default.asp">
  <img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;border:0">
</a>

Try it Yourself »

Note We have added border:0 to prevent IE9 (and earlier) from displaying a border around the image.


Image Maps

For an image, you can create an image map, with clickable areas:

Example

<img src="planets.gif" alt="Planets" usemap="#planetmap" style="width:145px;height:126px">

<map name="planetmap">
  <area shape="rect" coords="0,0,82,126" alt="Sun" href="sun.htm">
  <area shape="circle" coords="90,58,3" alt="Mercury" href="mercur.htm">
  <area shape="circle" coords="124,58,8" alt="Venus" href="venus.htm">
</map>

Try it Yourself »

Image Floating

You can let an image float to the left or right of a paragraph:

Example

<p>
  <img src="smiley.gif" alt="Smiley face" style="float:left;width:42px;height:42px">
  A paragraph with an image. The image floats to the left of the text.
</p>

Try it Yourself »

Chapter Summary

  • Use the HTML <img> element to define images
  • Use the HTML src attribute to define the image file name
  • Use the HTML alt attribute to define an alternative text
  • Use the HTML width and height attributes to define the image size
  • Use the CSS width and height properties to define the image size (alternatively)
  • Use the CSS float property to define image floating
  • Use the HTML usemap attribute to point to an image map
  • Use the HTML <map> element to define an image map
  • Use the HTML <area> element to define image map areas

Note Loading images takes time. Large images can slow down your page. Use images carefully.


Test Yourself with Exercises!

Exercise 1 »    Exercise 2 »    Exercise 3 »    Exercise 4 »   


HTML Image Tags

Tag Description
<img> Defines an image
<map> Defines an image-map
<area> Defines a clickable area inside an image-map



Color Picker

colorpicker