How do we include an anchor in HTML?


In this article we are going to perform the task of how do we include an anchor in HTML. Let’s dive into the article to know mpore about anchor in HTML.

The anchor element is used to link a source anchor to a destination anchor. The destination is the resource that the source anchor connects to, while the source is the text, picture, or button that points to another site.

The hyperlinks are one of the key technologies that make the internet the information superhighway.

To understand better on how do we include an anchor in HTML, let’s look into the following examples

Example 1

In the following example we are creating a jumping anchor link.

<!DOCTYPE html>
<html>
<head>
   <style>
      .main-content {
         height: 90vh;
         text-align: justify;
      }
   </style>
</head>
<body>
   <h2 id="tutorials">Tutorials Point</h2>
   <p class="main-content">
   Tutorials Point originated from the idea that there exists a class of readers who
   respond better to online content and prefer to learn new skills at their own pace
   from the comforts of their drawing rooms.The journey commenced with a single tutorial
   on HTML in 2006 and elated by the response it generated, we worked our way to adding
   fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and
   allied articles on topics ranging from programming languages to web designing to academics
   and much more.</p>
   <p>Click To
      <a href="#tutorials">Jump</a>.
   </p>
</body>
</html>

On running the above script, the output window pops up, displaying the text used in the above script along with an anchor tag mentioned "click to jump", which allows the user to jump to the start of the page when pressed.

Example 2

Considering the following we are creating a example of linking to the anchor from another web page.

<!DOCTYPE html>
<html>
<body>
   <h2 id="Tutorials">TutorialsPoint</h2>
   <p>Tutorials Point originated from the idea that there exists a class of readers
   who respond better to online content and preferto learn new skills at their own
   place from the comforts of their drawing rooms.</p>
   <p>
      <a href="https://www.tutorialspoint.com/market/index.asp">Go To The Course Page.</a>
   </p>
   <p>
      <a href="https://www.tutorialspoint.com/latest/ebooks">Search For Ebooks.</a>
   </p>
</body>
</html>

When the script gets executed, it will generate an output displaying the text used in the above script along with a link attached to the webpage using the anchor tag. When the user clicks on the link, it will open the new page.

Example 3: Using JavaScript

Looking into the following example we are creating a link to javascript.

<!DOCTYPE html>
<html>
<body>
   <a href="javascript:alert('Welcome To TutorialsPoint!');">Click To Execute</a>
</body>
</html>

When the given script is executed, it will provide an output that includes a link placed on the webpage using an anchor with the text "click to execute."

If the user clicks on the link, the script gets triggered and displays an alert "welcome to tutorials point".

Updated on: 15-Dec-2022

332 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements