HTML Links Hyperlinks
HTML Links Hyperlinks
HTML Hyperlinks
A hyperlink is a specific type of link that allows users to navigate from one web page or
resource to another by clicking on it. You can create hyperlinks using text or images
available on a webpage. A hyperlink is created using the HTML Anchor Tag (</a>).
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
Syntax
Open Compiler
Read more about creating URLs, we recommend to read this chapter: Understanding URL
As discussed above, you can create hyperlinks by using the HTML <a> tag with the href
attribute. The href attribute specifies the page/document to be linked.
Syntax
Page 2 of 7
Example
In this example, we are creating a simple HTML document that demonstrates how to use
a hyperlink:
Open Compiler
<!DOCTYPE html>
<html>
<head>
<title>Hyperlink Example</title>
</head>
<body>
<p>Click following link</p>
<a href="https://www.tutorialspoint.com/" target="_self">Tutorials
Point</a>
</body>
</html>
On executing the above example, a link will be displayed. You can click on the link
generated to reach to the home page of Tutorials Point.
_blank
1
Opens the linked document in a new window or tab.
_self
2
Opens the linked document in the same frame.
_parent
3
Opens the linked document in the parent frame.
Page 3 of 7
_top
4
Opens the linked document in the full body of the window.
targetframe
5
Opens the linked document in a named targetframe.
Example
Try following example to understand basic difference in few options given for target
attribute.
Open Compiler
<!DOCTYPE html>
<html>
<head>
<title>Hyperlink Example</title>
<base href="https://www.tutorialspoint.com/">
</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>
</html>
This will produce the following result, where you can click on different links to
understand the difference between various options given for target attribute.
Example
Page 4 of 7
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:
Open Compiler
<!DOCTYPE html>
<html>
<head>
<title>Hyperlink Example</title>
<base href="https://www.tutorialspoint.com/">
</head>
<body>
<p>Click following link</p>
<a href="/html/index.htm" target="_blank">HTML Tutorial</a>
</body>
</html>
This will produce the following result, where you can click on the link generated HTML
Tutorial to reach to the HTML tutorial.
Example
In the below code, we demonstrate the usage of the href attribute to navigate to a
different section within the same page. We provide #idofsection inside the href to
navigate sections of our need:
Open Compiler
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div {
Page 5 of 7
height: 900px;
}
</style>
</head>
<body>
<h2>Ed-Tech</h2>
<div>
<p>
Tutorialspoint: Simply Easy Learning
</p>
<a href="#about">Know More</a>
</div>
<h2 id="about">Section 2</h2>
<div>
<p>
Tutorials Point is an online learning platform
providing free tutorials, paid premium courses,
and eBooks. Learn the latest technologies and
programming languages SQL, MySQL, Python, C,
C++, Java, Python, PHP, Machine Learning, data
science, AI, Prompt Engineering and more.
</p>
</div>
</body>
</html>
Example
Save the following in test.htm and open it in any web browser to see how link, alink
and vlink attributes work.
Open Compiler
Page 6 of 7
<html>
<head>
<title>Hyperlink Example</title>
<base href="https://www.tutorialspoint.com/">
</head>
<body alink="#54A250" link="#040404" vlink="#F40633">
<p>Click following link</p>
<a href="/html/index.htm" target="_blank">HTML Tutorial</a>
</body>
</html>
This will produce the following result. Just check color of the link before clicking on it,
next check its color when you activate it and when the link has been visited.
Downloadable Links
HTML allows you to create downloadable links where you can create links to make your
PDF, DOC, or ZIP files downloadable. To create any link downloadable, you can use the
download attribute with the <a> tag and specify the downloadable file path in the href
attribute.
Example
The following example demonstrates creating a downloadable link in HTML:
Open Compiler
<!DOCTYPE html>
<html>
<head>
<title>Downloadable Link Example</title>
</head>
<body>
<a href="/html/src/sample.txt" download>Download File</a>
</body>
</html>
You can also specify the filename for the downloaded file. To give a custom filename the
file, you need to provide it to the download attribute.
Here is an example:
For example, if you want to make a filename file downloadable from a given link, then
its syntax will be as follows.
Syntax
#!/usr/bin/perl
# Additional HTTP Header
print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
# Open the target file and list down its content as follows
open( FILE, "<FileName" );
while(read(FILE, $buffer, 100)){
print("$buffer");
}
Note: For more detail on PERL CGI programs, go through tutorial PERL and CGI.