Image Tag In HTML Example Program With Code Snippets

Image tags are a fundamental aspect of HTML, enabling us to embed images into our webpages. They not only enhance the visual appeal of a site but also communicate information effectively. Let's delve into the world of image tags in HTML, starting with the basics and gradually moving to more intricate aspects.

Embedding Images in HTML using <img>
Embedding Images in HTML using <img>

At its core, an image tag in HTML is denoted by the opening `` tag and closing `` tag. However, the closing tag is not mandatory and is not used in practice. Here's a simple example:

Harnessing the Power of the <img> Tag in HTML
Harnessing the Power of the <img> Tag in HTML

Basic Image Tag Syntax

The basic syntax of an image tag in HTML is as follows:

🏷️ HTML Tags Cheat Sheet | Essential Tags Every Beginner Should Know
🏷️ HTML Tags Cheat Sheet | Essential Tags Every Beginner Should Know

<img src="URL" alt="alternative text">

The `src` attribute specifies the URL of the image. The `alt` attribute provides alternative text for an image if a user for some reason cannot view it. This could be due to visual impairment, slow connection, or the image failing to load.

HTML TAG
HTML TAG

Using Absolute URLs

Absolute URLs start with `http://` or `https://`. If your image is hosted on another domain, you should use an absolute URL:

<img src="https://example.com/image.jpg" alt="Description of image">

About HTML tags
About HTML tags

Using Relative URLs

Relative URLs, which start with a slash or refer to a specific directory, are used when the image is on the same domain:

<img src="/images/image.jpg" alt="Description of image">

Commonly Used HTML Tags with Examples - DataFlair
Commonly Used HTML Tags with Examples - DataFlair

Customizing Images with Attributes

Image tags can be customized with a variety of attributes to fit your webpage's needs:

the basic html tag list is displayed in this screenshote screen graber, which shows
the basic html tag list is displayed in this screenshote screen graber, which shows
the ultimate guide to creating and formating tags
the ultimate guide to creating and formating tags
a close look at tags info sheet
a close look at tags info sheet
887 reactions · 135 shares | HTML5 Tags information 2026. #coding #webdevelopment✨ #htmlcssjavascript #programming | Premi Coding | Facebook
887 reactions · 135 shares | HTML5 Tags information 2026. #coding #webdevelopment✨ #htmlcssjavascript #programming | Premi Coding | Facebook
HTML Tags — List of all HTML Tags with Examples TutorialBrain
HTML Tags — List of all HTML Tags with Examples TutorialBrain
Images in HTML
Images in HTML
HTML Heading Tag Example
HTML Heading Tag Example
the list of html tags for different types of webpages and pages, with text overlaiding them
the list of html tags for different types of webpages and pages, with text overlaiding them
Title: HTML Semantic vs Non-Semantic Tags – Guide by RajNandani
Title: HTML Semantic vs Non-Semantic Tags – Guide by RajNandani
How To Add Watermark To Images In HTML Javascript
How To Add Watermark To Images In HTML Javascript
List of HTML TAGS
List of HTML TAGS
HTML TAGS | PART 1
HTML TAGS | PART 1
Complete Beginner HTML Master Notes
Complete Beginner HTML Master Notes
Space, Line Break, and Tags in HTML Content
Space, Line Break, and Tags in HTML Content
HTML5 Tags &HTML5 Attributes
HTML5 Tags &HTML5 Attributes
List of HTML Tags
List of HTML Tags
Essential HTML Tags Every Web Developer Needs! 🏷️
Essential HTML Tags Every Web Developer Needs! 🏷️
the different types of html tags are shown in this screenshote screen graber
the different types of html tags are shown in this screenshote screen graber
an info sheet with the words, tags and key points on it's page
an info sheet with the words, tags and key points on it's page
two different types of web pages with the same text and numbers in each one language
two different types of web pages with the same text and numbers in each one language

Width and Height

The `width` and `height` attributes allow you to specify the size of your image:

<img src="my_image.jpg" alt="Image description" width="300" height="200">

Remember, if you don't set these attributes, the browser will load the image at its original size.

Aligning Images

The `align` attribute is used to position an image with respect to surrounding text. It can take values like `left`, `right`, `top`, etc.

<img src="my_image.jpg" alt="Image description" align="left">

Responsive Images

In today's mobile-first web, responsive images that scale to fit various screen sizes are essential. The `srcset` and `sizes` attributes make this possible:

Using srcset

The `srcset` attribute allows you to specify different image URLs for different screen resolutions. It's used in conjunction with the `sizes` attribute:

<img srcset="small.jpg 480w, medium.jpg 800w, large.jpg 1024w" sizes="(max-width: 600px) 480px, 800px" alt="Image description">

In conclusion, mastering image tags in HTML is key to creating engaging, aesthetically pleasing, and responsive webpages. Practice using these tags and attributes to refine your skills, and always remember the importance of alternative text for accessibility.

Now, go forth and create visually stunning webpages!

Related Articles

What Is The Code To Add Image In Html Upload Images To Use In Html Web Dev Simplified Form What Is Image In Html With Example How To Use Images As Links In Html Html Table Design Examples With Code Img Tag Html With Url Tutorialspoint Typescript Pdf Web Dev Simplified Css Animation Mdn Html Table Basics