Updated March 14, 2023
What is HTML Image Tags?
HTML is a plain text document that allows multiple formats of programming languages to implement web-based applications, which use 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. This syntax is <img src = ‘copy image URL’>, where ‘image’ is the tag name, and ‘src=’ should have the required image’s URL assigned to it. In this topic, we are going to learn about HTML Image Tags.
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 into the said web page; instead, it creates a holding space where the image is put once 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.
Regarding the 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:
Sometimes, 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
Using the background-image CSS property in the page’s Body element, you can assign an image as a background picture of a web page.
<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 look at an example that will help you understand.
<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.
2) Alt
Value type: Text
Alt is used to specify the alternative text of a webpage picture. In cases where displaying an IMG is impossible, 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
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 gives a detailed description of an image by using a URL. The URL in the attribute is used as the image’s description.
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 on 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 defines 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 used as whitespaces 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 – HTML Image Tags
Now that we have looked 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 optimization. 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 have discussed 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 –