Open In App

How to Add Link to HTML Button?

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

A link in HTML connects one webpage to another, allow smooth navigation. In HTML , links are created using the <a> tag. The href attribute within the tag specifies the URL or path to the destination.

We are given an HTML button, to add a link to the HTML button, we can wrap the button inside the <a> tag so that the buttons act as a link.

Example: In the below example, we wrap the button inside <a> tag.

HTML
<a href="https://www.geeksforgeeks.org/html-tutorial/">
  	<button>Click me</button>
</a>


Let's discuss some approaches to add a link to an HTML Button.

Approach 1: Using Inline onclick Event

Using an inline onclick event associates a JavaScript function with the button element's onclick attribute. When clicked, the function redirects the user to a specified URL using window.location.href.

Syntax:

<button onclick="window.location.href='https://www.geeksforgeeks.org/community/';" class="GFG">Click Here</button>

Example: In this example we Create an HTML button styled with CSS. On click, it redirects to the GeeksforGeeks Community using an inline onclick event.

HTML
<!DOCTYPE html>
<html>
<head>
    <title>
        Using Inline onclick Event
    </title>
    <style>
        .GFG {
            background-color: white;
            border: 2px solid black;
            color: green;
            padding: 5px 10px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <!-- Adding link to the button on the onclick event -->
    <button class="GFG"
        onclick="window.location.href = 'https://geeksforgeeks.org/community';">
        Click Here
    </button>
</body>
</html>


Approach 2: Using button tag inside <a> tag

This method creates a button inside an anchor tag. The anchor tag redirects the webpage to the given location.

Syntax:


<button class="GFG">
Click Here
</button>

Example:

HTML
<!DOCTYPE html>
<html>
<head>
    <title>
        Using button tag inside a tag
    </title>
    <style>
        .GFG {
            background-color: white;
            border: 2px solid black;
            color: green;
            padding: 5px, 10px;
            /* Corrected padding values */
            cursor: pointer;
        }
    </style>
</head>
<body>
    <!-- Adding button inside the link tag -->
    <a href="https://geeksforgeeks.org/community/">
        <button class="GFG">
            Click Here
        </button>
    </a>
</body>
</html>


This method creates a simple anchor tag link and then applies some CSS properties to make it look like a button.

Replace the below snippet in the body tag given in the above Sample Button Code.

Syntax:

 <a href="https://ide.geeksforgeeks.org/" class="GFG">Click me</a>

Example:

HTML
<!DOCTYPE html> 
<html> 
<head> 
    <title> 
        Create an HTML button that 
        acts like a link 
    </title> 
    <!-- Style to create button -->
    <style> 
        .GFG { 
           width:100px;
           height:50px;
           background:green;
           border:none;
           color:white;
        } 
    </style> 
</head> 
<body> 
    <h1>GeeksforGeeks</h1> 
      
    <!-- Adding link to the button on the onclick event -->
     <a href="https://geeksforgeeks.org/community/" class="GFG">Click me</a>
</body> 
</html>


Approach 4: Using form tags

Another approach is to use the action or form action attribute within a <form> element. This method is semantically correct and works well inside forms.

  <button type="submit" class="GFG">Click me</button>

Example:

HTML
<!DOCTYPE html> 
<html> 
<head> 
    <title> 
        Create an HTML button that 
        acts like a link 
    </title> 
    <!-- Style to create button -->
    <style> 
        .GFG { 
           width:100px;
           height:50px;
           background:green;
           border:none;
           color:white;
        } 
    </style> 
</head> 
<body> 
    <h1>GeeksforGeeks</h1> 
    <!-- Adding link to the button on the onclick event -->
      <form action="https://geeksforgeeks.org/community/">
        <button type="submit" class="GFG">Click me</button>
       </form>
</body> 
</html>


Output:

ide-button

Also Try, To Improve the Look of Your Button

  • Add a Live Demo for Interactive Preview.
  • Add accessibility tips like aria-label or role="button".
  • Mention cross-browser compatibility considerations.
  • Include sample CSS for consistent button styling.
  • Highlight the benefits of using semantic HTML elements.
  • Warn against common mistakes like invalid tag nesting.

Next Article
Article Tags :

Similar Reads