When creating a website, you might want to add a button that users can click to navigate to another page or website. One way to achieve this is by creating an href button in HTML. In this post, we’ll show you how to create a simple href button using two different methods: using an anchor tag and a button tag.
Method 1: Using an Anchor (A) Tag
The first method involves wrapping an anchor tag (<a>) around a button element. The anchor tag is used to create hyperlinks, and the button element is used to create a clickable button. By combining these two elements, you can create an href button. Here’s an example of how to create an href button using an anchor tag:
<a href="https://www.example.com" target="_blank"> <button>Visit Example.com</button> </a>
In this example, the href attribute is set to the URL you want the user to be navigated to when they click the button. The target=”_blank” attribute is optional and will open the link in a new tab or window when the button is clicked. If you want the link to open in the same tab or window, you can omit this attribute.
<button onclick="window.location.href='https://www.example.com'">Visit Example.com</button>
<button onclick="window.open('https://www.example.com', '_blank')">Visit Example.com</button>
In this example, the window.open() function is used to open the specified URL in a new tab or window when the button is clicked.