Open In App

How To Link Pages In HTML?

Last Updated : 09 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In HTML, linking pages is the fundamental practice that allows users to navigate from one web page to the another within same website or to an entirely different website. These links, also known as hyperlinks, are created using the <a> tag, which can direct the users to another HTML document, an external site, a section within the same page, an email address, or other types of resources.

Prerequisites

Linking to the Another Web Page

This is the most common type of link, which can allow you to navigate from one web page to another page. These links can point to other pages within the same website or to external websites. They can use the <a> tag with the href attribute to specify the destination URL.

Syntax:

<a href="URL">Link Text</a>
  • URL: The destination URL. it can be absolute or relative.
  • Link Text: The clickable text that will appear on the web page.

Example: In this example, the anchor (<a>) tag can be used to create the link that takes the user to the blog page of the external website, example.com. The link text is "Visit Our Blog," which is what users will see and click on. When clicked, the browser navigates to https://www.geekforgeeks.org.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Linking to Another 
        Web Page</title>
</head>

<body>
    <h1 style="color: green;">
        GeeksforGeeks</h1>
    <p>Click the link below to
         visit our blog:</p>
    <a href="
    https://www.geeksforgeeks.org/">
    Visit Our GeeksforGeeks Blog
    </a>
</body>

</html>

Output: When the Visit Our Blog link can be clicked then the browser navigates to https://www.geekforgeeks.org website.

Linking to the Section of the Same Page

It is also known as the anchor links or bookmarks, these links can allow you to navigate to the specific section within the same page. It can be particularly useful for the long pages where you want to provide the quick navigation to the different sections.

Syntax:

<a href="#section-id">Link Text</a>

Example: In this example, the link <a href="#contact">Go to Contact Section</a> points to the section with the ID contact. When the user clicks on the link then the page will scroll down to the "Contact Us" section, which is marked by <h2 id="contact">Contact Us</h2>.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Anchor Link Example</title>
</head>

<body>
    <h1 style="color: green;">
        GeeksforGeeks</h1>
    <h2>Welcome to Our Page</h2>

    <p>Click the link below to go to 
        the contact section:</p>
    <a href="#contact">
        Go to Contact Section</a>

    <!-- Content -->
    <div style="height: 800px;"></div>
     <!-- Just to create some space -->

    <!-- Target Section -->
    <h2 id="contact">Contact Us</h2>
    <p>Feel free to reach out via our
         contact form or by phone.</p>
</body>

</html>

Output: Clicking the Go to Contact Section link will scroll the page down to the Contact Us section.

Linking to an Email Address

These links can use the mailto: protocal to open the users default email client and start the new email with the specified recipient address. It can be useful for the contact links that automatically open the email editor.

Syntax:

<a href="mailto:[email protected]">Send Email</a>

Example: In this example, when users can click the "Email Support" link, their default email client opens with the "To" field pre-filled with [email protected]. The mailto: protocol handles this behavior.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Email Link Example
    </title>
</head>

<body>
    <h1 style="color: green;">
        GeeksforGeeks</h1>
    <h2>Contact Us</h2>
    <p>For more information,
        please send us an email:</p>
    <a href="
    mailto:[email protected]">
        Email Support</a>
</body>

</html>

Output: Clicking the Email Support link will open the default email client with the new email window and the to field to filled with [email protected].

congfg
Output of Linking to an Email Address

Linking to the File

Link to the files are used when you want to provide the downloadable content, such as the PDFs, images, or documents. The user can click on the link to view or download the file. The download attribute can be added to the ensure the file can be downloaded instead of the open in the browser.

Syntax:

<a href="path/to/file.ext" download>Download File</a>

Example: In this example, the anchor tag links to the GIF file (gfg1.gfg). The download attribute can be used to make sure the file can be downloaded to the user device instead of opening the browser. When the user click on the "Download User Guide," the file will be downloaded.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>File Download Link 
        Example</title>
</head>

<body>
    <h1 style="color: green;">
        GeeksforGeeks</h1>
    <h2>Download Our PDF Guide</h2>
    <p>Click the link below to 
        download our user guide:</p>
    <a href="gfg1.gif" download>
        Download User Guide</a>
</body>

</html>

Output:

Linking to the JavaScript Function

Links can be used to trigger JavaScript functions. This is useful for the interactive web pages where you want to execute the some client-side script when the link is clicked. The javaScript protocal can be used in the href attribute of the <a> tag. It can be invokes the JavaScript function directly from the Link.

Syntax:

<a href="javascript:functionName()">Link Text</a>

Example: In this example, the <a href="javascript:showAlert()">Show Alert</a> can calls the JavaScript function showAlert()> When the user clicks the link, a pop-up alert box with the message "Hello! This is a JavaScript alert." will appear on the screen.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>JavaScript Function Link Example
    </title>
    <script>
        function showAlert() {
            alert('Hello! This is a JavaScript alert.');
        }
    </script>
</head>

<body>
    <h1 style="color: green;">GeeksforGeeks</h1>
    <h2>JavaScript Link Example</h2>

    <p>Click the link below to trigger a JavaScript alert:</p>
    <a href="javascript:showAlert()">Show Alert</a>
</body>

</html>

Output: Clicking the Show Alert link will display the JavaScript alert box with the message "Hello! this a javaScript alert.".


Next Article
Article Tags :

Similar Reads