Open In App

How To Link Two Pages In HTML ?

Last Updated : 17 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The linking of two pages in HTML is a fundamental aspect of web development, used in over 95% of websites for navigation. The most common method is through the <a> (anchor) tag, which creates hyperlinks to other pages or resources. In this article, we will explore various ways to link pages in HTML, along with syntax and examples for each method.

Using the <a> Tag with href Attribute

The <a> tag is the most straightforward and widely used method to create hyperlinks in the HTML. The href (hypertext reference) attribute within the <a> tag specifies the destination URL of the link. When the user clicks on the hyperlink, the browser navigates to the page specified in the href attribute.

Syntax

<a href="destination.html">Link Text</a>

Example: Below is an example demonstrating the use of the <a> tag with the href attribute. In this example, the text enclosed within the <a> tag becomes a clickable link that, when clicked, opens another page.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
     content="width=device-width,
      initial-scale=1.0">
    <title>Page 1</title>
</head>
<body>
    <h1 style="color: green;">
        GeeksforGeeks</h1>
    <h2>This is Page 1</h2>
    <p><a href="page.html">
        Go to Page 2</a></p>
</body>
</html>
page.html
<!-- page.html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
     content="width=device-width,
      initial-scale=1.0">
    <title>Page 2</title>
</head>

<body>
    <h1 style="color: green;">
        GeeksforGeeks</h1>
    <h2>This is Page 2</h2>
    <p><a href="index.html">
        Go back to Page 1</a></p>
</body>

</html>

Output

Using the <form> Tag with a Button

The form can also be used to link the two pages, although this is less common. This method can involves creating the form with an action attribute that the points to the desired page. When the user clicks the submit button, the form data can sent to the target page specified in the action attribute and the browser navigates to that page.

Syntax

<form action="destination.html">
<input type="submit" value="Button Text">
</form>

Example: Below is the example demonstrating the use of <form> tag with a button, form is primarily used for submitting data to server, but it can also be used simply for navigation.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
     content="width=device-width,
      initial-scale=1.0">
    <title>Page 1</title>
</head>
<body>
    <h1 style="color: green;">
        GeeksforGeeks</h1>
    <h2>This is Page 1</h2>
    <form action="page.html">
        <input type="submit"
         value="Go to Page 2">
    </form>
</body>
</html>
page.html
<!-- page.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
     content="width=device-width,
      initial-scale=1.0">
    <title>Page 2</title>
</head>

<body>
    <h1 style="color: green;">
        GeeksforGeeks</h1>
    <h2>This is Page 2</h2>
    <p><a href="index.html">
        Go back to Page 1</a></p>
</body>
</html>

Output

Using the <button> Tag with JavaScript

The <button> tag can be used to create the clickable button and with the help of the JavaScript, it can be programmed to the navigate to another page. This approach can be useful when you need to perform the some JavaScript-based logic before navigating, or when you want control over the navigation process.

Syntax

<button onclick="location.href='destination.html'">Button Text</button>

Example: Below is the example demonstrating <button> tag with javaScript, here onclick event attribute is used where location.href property is used to change the current URL to specified URL.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
     content="width=device-width,
      initial-scale=1.0">
    <title>Page 1</title>
</head>
<body>
    <h1 style="color: green;">
        GeeksforGeeks</h1>
    <h2>This is Page 1</h2>
    <button onclick="
    location.href='page.html'">
    Go to Page 2</button>
</body>
</html>
page.html
<!-- page.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport"
     content="width=device-width,
      initial-scale=1.0" />
    <title>Page 2</title>
</head>
<body>
    <h1 style="color: green">
        GeeksforGeeks</h1>
    <h2>This is Page 2</h2>
    <p><a href="index.html">
        Go back to Page 1</a></p>
</body>
</html>

Output


Next Article

Similar Reads