HTML - Text Links



A webpage can contain various links that take us directly to other web pages or resources and even specific parts of a given page.

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 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 the linked document.

We recommend you to go through a short tutorial on Understanding URL

Syntax

<a href="Document URL">Text</a>

Examples of HTML Text Links

Here are few example codes that illustrate the use of text links in HTML.

Create a hyperlink

In this example we will create a hyperlink to tutorialspoint website using anchor tag. In the output on clicking Learn tutorial you will be redirected to main page of Tutorialspoint.

<!DOCTYPE html>
<html>

<head>
   <title>Hyperlink Example</title>
</head>

<body>
   <p>Click following link</p>
   <a href="https://www.tutorialspoint.com/" 
      target="_blank">
         Learn Tutorials
   </a>
</body>

</html>

Target attribute of anchor Tag

The target attribute of the anchor tag in HTML specifies where to open the linked document. This attribute is used to open linked document in new browser tab, same tab or parent frame depending upon the need.

<!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>

Use of Base Path

When you link HTML documents related to the same website, it is not required to give a complete URL for every link. You can get rid of it if you use <base> tag in your HTML document header. This tag is used to give a base path for all the links. So your browser will concatenate given relative path to this base path and will make a complete URL.

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.tutorialspoint.com/">
</head>

<body>
   <p>Click following link</p>
   <a href="/html/index.htm" target="_blank">
         HTML Tutorial
   </a>
</body>

</html>

Linking to a Page Section

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 href to navigate section of our need.

<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        div {
            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>

Setting Link Text Colors

You can set colors of your links, active links and visited links using link, alink and vlink attributes of <body> tag.

Save the following in test.htm and open it in any web browser to see how link, alink and vlink attributes work.

<html>

<head>
   <title>Hyperlink Example</title>
   <base href="https://www.tutorialspoint.com/">
</head>
<body alink="red" link="green" vlink="black">
   <p>Click following link</p>
   <a href="/html/index.htm" target="_blank">
         HTML Tutorial
   </a>
</body>

</html>

HTML Downloadable Links

You can create text link to make your PDF, or DOC or ZIP files downloadable. This is achieved by using download attribute of anchor tag.

<!DOCTYPE html>
<html>

<head>
   <title>Hyperlink Example</title>
   <base href="https://www.tutorialspoint.com/">
</head>

<body>
   <a href="/html/src/sample.txt">
      View File
   </a>
   <br><br>
   <a href="/html/src/sample.txt" download>
      download File
   </a>
</body>

</html>
Advertisements