GIF Images | JPG Images | PNG Images |
<!DOCTYPE html>
<html>
<body>
<h2>Spectacular Mountains</h2>
<img src="pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px">
</body>
</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 »
Always specify the size of an image. If the size is unknown, the page will flicker while it 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.
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 behind the value):
Alternatively, you can use width and height attributes.
The values are specified in pixels (without px behind the value):
Width and Height or Style?
Both width and height and the style attribute, are specified in the HTML5 standard.
Since both are allowed, you are free to choose which one to use.
Note: Using the style attribute prevents external styles to set default size to 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>
<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 »
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:
Moving Images
The GIF standard allows moving images:
Example
<img src="programming.gif" alt="Computer Man" style="width:48px;height:48px">
Try it Yourself »
Note that the syntax of inserting moving images is no different from non-moving 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>
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;border:0">
</a>
Try it Yourself »
We have added border:0 to prevent IE9 (and earlier) from displaying a border around the image. |
Image Maps
For an image, you can crate 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>
<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>
<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 »