What is HTML Image Tags?
HTML is a plain text document that allows multiple formats of programming languages to implement web-based applications, which uses tags for describing the web pages’ functionality. One such essential tag is the image tag that allows the developers to incorporate image files into the code for displaying the respective images on the web page. The syntax for this is <img src = ‘copy image URL’>, where ‘image’ is the tag name, and ‘src=’ should have the required image’s URL assigned to it.
Adding Images to Webpages
You can add IMG to an HTML page by using the <img> tag in the HTML document; here is the syntax :
<img src= enter the IMG URL here >
Here, the IMG tells the browser that the tag is about adding an IMG to the document, and the “src=” specifies where to download the image from.
Example of a Page with an Image
Code:
<!DOCTYPE html>
<html>
<head>
<title> Example HTML IMG Tag </title>
</head>
<body>
<img src = " https://cdn.educba.com/academy/wp-content/uploads/2019/02/Software-Dev.jpg"
alt = " Software development icon " height = " 150 " width = "140" />
</body>
</html>
Output:
One interesting fact about these HTML pages is that when you use the IMG tag, the image is not inserted to the said web page; instead, it creates a holding space where the image is put once it is downloaded.
Browser Support and Compatibility with Attributes
As you can expect, all modern browsers support images and the use of IMG tags. Sometimes, mobile browsers will resize the image to fit it on the screen if the image is not set as responsive.
When it comes to compatibility of attributes with HTML 4.01 and newer HTML5, most tags will work, except for aligning, border, hspace, and space, which are simply not supported in the latter.
Images as a link:
There will be times when you will want an image to work as a link to another page. You can get it done by adding the IMG tag inside the <a> tag.
- <a href=” https://www.educba.com/project-management/ “>
- <img src=” https://cdn.educba.com/academy/wp-content/uploads/2019/02/Project-Management.jpg ” alt=” Project Management ” style=”width:42px; height:42px; border:0;” ></a>
Setting an Image as the Background of a Webpage
You can assign an image as a background picture of a web page by using the background-image CSS property in the page’s Body element.
<body style="background-image:url(‘car.jpg');">
<h1>Background Image </h1>
</body>
Setting an Image to Float in the Browser
We can use the CSS property “float” to set an image to float inside anywhere in the browser window. Let’s take a look at an example that will help you in understanding.
<p> <img src="car.png" alt="Ferrari Car " style="float: right; width:40px; height: 40px;">
Here, the image of the car will float to the right side of the text. </p>
<p><img src=" car.png " alt=" Ferrari Car " style="float: left; width: 40px; height: 40px;">
Here, the image of the car will float to the left side of the text. </p>
Attributes of Image Tag
The following are the attributes of an image tag.
1) Align
Possible Values: Top, Bottom, Middle, Left or Right.
The alight attribute is used to specify the alignment of a picture on the webpage. Popular Course in this categoryHTML Training (12 Courses, 19+ Projects, 4 Quizzes)12 Online Courses | 19 Hands-on Projects | 89+ Hours | Verifiable Certificate of Completion | Lifetime Access | | 4 Quizzes with Solutions
4.5 (6,492 ratings)Course Price
₹6999 ₹41999
View Course
Related CoursesBootstrap Training (2 Courses, 6+ Projects)XML Training (5 Courses, 6+ Projects)CSS Training (9 Courses, 9+ Projects)
2) Alt
Value type: Text
Alt is used to specify the alternative text of a picture of a webpage. In cases where displaying an IMG is not possible, the browser displays this text to the user. Search engines such as Google and Bing use this alt text to show results in the Image Search.
3) Border
Value Type: Pixels
It is used to create a border of user-defined thickness around the picture. It does not work in HTML5.
4) Cross-Origin
Value Type: Anonymous use-Credentials
This attribute is used when we want to specify how cross-origin photos should be dealt with. This is mostly used in cases where canvas elements of JavaScript web apps are used.
5) Height
Value Type: Percentages or Pixels
Obviously, this one is used to denote the height of the image in the HTML webpage.
6) hspace
Value Type: Pixels
Unsupported in HTML5, the hspace attribute is used to specify in pixels how much white space was to be added to the left and right of the inserted image.
7) ismap
Value type: URL of a Page
ismap we used to define the said image as a server-side image map. When the user clicks (or taps) inside the image, the browsers send the click (or tap) coordinates to the web server as a URL.
8) Longdesc
Value Type: URL
Longdesc is used to give a detailed description of an image by using a URL. The URL used in the attribute is used as the description of the image.
9) src
Value Type: URL
src stands for source. It is used to specify the address from which the browser will retrieve the image; this URL can be applied to an image inside a directory in the same server. It can also store an image in a third-party server with a different domain name.
10) usemap
Value Type : #mapname
The usemap attribute is used to define the image for a client-side image map. A usemap is always used with map and area HTML tags.
11) vspace
Value Type: Pixels
Unsupported in HTML5, the Vspace attribute is used to set the number of pixels that are to be used as whitespace at the top and bottom of the image on the webpage.
12) Width
Value Type: Pixels
Just as its name suggests, the width attribute is used to specify the width of a picture inside the HTML web page.
Conclusion
Now that we have taken a look at how images are added to HTML pages and how to set their attributes, we can create attractive looking web pages in a web project.
Besides just adding visual flair to a web page, images are valuable because they help search engine optimisation. Adding proper alt tags and descriptions to images help search engines understand the content of a web page better and improve the ranking of a web page in many cases.
Recommended Articles
This is a guide to HTML Image Tags. Here we discuss the basic concept and attributes of the image tag, such as browser support and compatibility with attributes. You may also have a look at the following articles to learn more –