In This Tutorial
The src attribute of IMG tag
The src is an attribute of <img> tag of HTML. The img’s src stands for source, i.e. specifying image source where image will be pulled.
In img src attribute you can specify relative path e.g. if your domain is: http://www.domain.com then img src can be:
<Img src = “images/image.png”/>. It will be taken as http://www.domain.com/currentpage/images/image.png
Alternatively you can specify absolute path like this:
<Img src = “http://www.domain.com/currentpage/images/image.png”>
If you are using images from same domain then you should use relative path. If you are using third party images (images from other URL) then specify absolute path.
See below “src” examples with relative and absolute path:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!DOCTYPE html>
<html>
<head>
<title>HTML img tag</title>
</head>
<body>
<img src=“TC_logo.png”/><br />
<img src=“https://www.tutorialscollection.com/html-demo/TC_logo.png”/>
</body>
</html>
|
As you can see both specifications are showing same result. However as same domain is referred where src image exists we should use relative path in that case.
Using img tag with src and alt attributes example
alt stand for alternative text. This is quite useful for SEO purpose as well. For example google search for images. Also if somehow image is not displayed, due to user’s slow connection or some other reason the alt text will be displayed.
The example below shows using img src and alt attributes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!DOCTYPE html>
<html>
<head>
<title>HTML img tag</title>
</head>
<body>
<img src=“TC_logo.png” alt=“Tutorials Collection Logo”/>
</body>
</html>
|
img src as HTML links
You can use images linked to other web pages of your website or some other website. In order to link images to other web pages you can nest <a> tag with <img> tag. Or use src inside HTML link tag.
The example below shows how to link an image to other web pages by using src.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!DOCTYPE html>
<html>
<head>
<title>HTML img tag</title>
</head>
<body>
<a href=“https://www.tutorialscollection.com/”><img src=“TC_logo.png” alt=“Tutorials Collection Logo” height=“60” width=“263”/></a>
</body>
</html>
|
Also see HTML img | HTML href | HTML tutorial
Leave A Comment?