HTML Images
Images are used in HTML to improve the design and appreance of the web page.
Example
<html>
<body>
<h2>HTML Image</h2>
<img src="image/jaipur.jpg" alt="Jaipur" width="500" height="333"><
</body>
</html>
Try it »
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 specifies the URL (web address) of the image.
Syntax: <img src="url">
The alt Attribute
- The alt attribute provides an alternate text for an image, if the user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).
- The value of the alt attribute should describe the image.
Example
<html>
<body>
<h2>Alternative text</h2>
<img src="image/jaipur.jpg" alt="Jaipur" width="500" height="333"><
</body>
</html>
Try it »
Note: - The alt attribute is required. A web page will not validate correctly without it.
Image Size - Width and Height
The style attribute is used to specify the width and height of an image.
Example
<html>
<body>
<h2>Image Size</h2>
<img src="image/italy.jpg" alt="Italy" style="width:500px;height:300px;">
</body>
</html>
Try it »
Width and Height, or Style?
- The width, height, and style attributes are valid in HTML.
- However, it is suggested to use the style attribute. It prevents styles sheets from changing the size of images.
Example
<html>
<body>
<h2>Styling Images</h2>
<img src="image/2.jpg" alt="Sea" width="150" height="150">
</body>
</html>
Try it »
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 to store images in a sub-folder. You must then include the folder name in the src attribute.
Example
<html>
<body>
<h2>Images in Another Folder</h2>
<img src="/image/germany.jpg" alt="Germany" style="width:150px;height:150px;">
</body>
</html>
Try it »
Animated Images
Animated GIFs are allowed in HTML.
Example
<html>
<body>
<h2>Images in Another Folder</h2>
<img src="image/tenor.gif" alt="Laughing" style="width:64px;height:64px;">
</body>
</html>
Try it »
Image as a Link
To use an image as a link,the <img> tag is written inside the <a> tag.
Example
<html>
<body>
<h2>Image as a Link</h2>
<p>The image is a link. You can click on it.</p>
</body>
</html>
Try it »
Image Floating
Use the CSS float property to let the image float to the right or to the left of a text.
Example
<html>
<body>
<h2>Floating Images</h2>
<img src="image/tenor.gif" alt="Laughing" style="float:right;width:42px;height:42px;">
</body>
</html>
Try it »
Background Image
To add a background image on an HTML element, use the CSS property background-image.
Example
<html>
<body style="background-image:url('clouds.jpg');">
<h2>Background Image</h2>
<p>By default the background image will repeat itself if it is smaller than the element where it is specified, in this case the BODY element.</p>
</body>
</html>
Try it »
HTML Screen Readers
A screen reader is a software program that reads the HTML code, converts the text, and allows the user to "listen" to the content. Screen readers are useful for people who are visually impaired or learning disabled.