HTML - Images: Insert Image
HTML - Images: Insert Image
Images are very important to beautify as well as to depict many complex concepts in simple way on your
web page. This tutorial will take you through simple steps to use images in your web pages.
Insert Image
You can insert any image in your web page by using <img> tag. Following is the simple syntax to use
this tag.
The <img> tag is an empty tag, which means that, it can contain only list of attributes and it has no
closing tag.
Example
To try following example, let's keep our HTML file test.htm and image file test.png in the same directory −
<!DOCTYPE html>
<html>
<head>
<title>Using Image in Webpage</title>
</head>
<body>
<p>Simple Image Insert</p>
<img src = "/html/images/test.png" alt = "Test Image" />
</body>
</html>
You can use PNG, JPEG or GIF image file based on your comfort but make sure you specify correct
image file name in src attribute. Image name is always case sensitive.
The alt attribute is a mandatory attribute which specifies an alternate text for an image, if the image
cannot be displayed.
Example
Assuming our image location is "image/test.png", try the following example −
<!DOCTYPE html>
<html>
<head>
<title>Using Image in Webpage</title>
</head>
<body>
<p>Simple Image Insert</p>
<img src = "/html/images/test.png" alt = "Test Image" />
</body>
</html>
1
Set Image Width/Height
You can set image width and height based on your requirement using width and height attributes. You
can specify width and height of the image in terms of either pixels or percentage of its actual size.
Example
<!DOCTYPE html>
<html>
<head>
<title>Set Image Width and Height</title>
</head>
<body>
<p>Setting image width and height</p>
<img src = "/html/images/test.png" alt = "Test Image" width = "150"
height = "100"/>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<title>Set Image Border</title>
</head>
<body>
<p>Setting image Border</p>
<img src = "/html/images/test.png" alt = "Test Image" border = "3"/>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<title>Set Image Alignment</title>
</head>
<body>
<p>Setting image Alignment</p>
2
<img src = "/html/images/test.png" alt = "Test Image" border = "3" align
= "right"/>
</body>
</html>
Example
It's simple to use an image as hyperlink. We just need to use an image inside hyperlink at the place of
text as shown below −
<!DOCTYPE html>
<html>
<head>
<title>Image Hyperlink Example</title>
</head>
<body>
<p>Click following link</p>
<a href = "https://www.bisegrw.com" target = "_self">
<img src = "/images/logo.png" alt = "Gujranwala Board" border = "0"/>
</a>
</body>
</html>
Hyperlinks allow visitors to navigate between Web sites by clicking on words, phrases, and images. Thus
you can create hyperlinks using text or images available on a webpage.
Linking Documents
A link is specified using HTML tag <a>. This tag is called anchor tag and anything between the opening
<a> tag and the closing </a> tag becomes part of the link and a user can click that part to reach to the
linked document. Following is the simple syntax to use <a> tag.
Example
Let's try following example which links http://www.bisegrw.com at your page −
<!DOCTYPE html>
<html>
<head>
<title>Hyperlink Example</title>
3
</head>
<body>
<p>Click following link</p>
<a href = "https://www.bisegrw.edu.pk" target = "_self">Gujranwala
Board</a>
</body>
</html>
1
_blank
2
_self
3
_parent
4
_top
5
targetframe
Example
Try following example to understand basic difference in few options given for target attribute.
<!DOCTYPE html>
<html>
<head>
<title>Hyperlink Example</title>
<base href = "https://www.bisegrw.edu.pk/">
</head>
<body>
<p>Click any of the following links</p>
<a href = "/html/index.htm" target = "_blank">Opens in New</a> |
<a href = "/html/index.htm" target = "_self">Opens in Self</a> |
<a href = "/html/index.htm" target = "_parent">Opens in Parent</a> |
<a href = "/html/index.htm" target = "_top">Opens in Body</a>
</body>
4
</html>
Example
Following example makes use of <base> tag to specify base URL and later we can use relative path to
all the links instead of giving complete URL for every link.
<!DOCTYPE html>
<html>
<head>
<title>Hyperlink Example</title>
<base href = "https://www.bisegrw.edu.pk">
</head>
<body>
<p>Click following link</p>
<a href = "/html/index.htm" target = "_blank">HTML Tutorial</a>
</body>
</html>
Example
Save the following and open it in any web browser to see how link, alink and vlink attributes work.
<!DOCTYPE html>
<html>
<head>
<title>Hyperlink Example</title>
<base href = "https://www.bisegrw.edu.pk">
</head>
</html>
Download Links
5
You can create text link to make your PDF, or DOC or ZIP files downloadable. This is very simple; you
just need to give complete URL of the downloadable file as follows −
<!DOCTYPE html>
<html>
<head>
<title>Hyperlink Example</title>
</head>
<body>
<a href = "https://www.bisegrw.edu.pk page.pdf">Download PDF File</a>
</body>
</html>
Assignment Solution
<html>
<body>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th> </tr>
<tr> <td>Jill</td>
<td>Smith</td>
<td>50</td>
</td> <td>Jackson</td>
<td>94</td> </tr>
<tr> <td>John</td>
<td>Doe</td> <td>80</td>
</tr>
</table>
</body>
</html>
6
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<td>
</tr>
<tr>
<td>3</td> <td>4</td>
</tr>
</table>
</td>
</tr>
<tr> <td>This cell contains a list <ul> <li>car</li> <li>bike</li> <li>bicycle</li> </ul>
</td>
<td>HELLO</td>
</tr>
</table>
</body>
</html>