- How to Create an HTML Button That Acts Like a Link?
- Using Inline onclick Event
- Using a Link Styled as an HTML Button
- HTML:
- CSS:
- Overlaying an Invisible Link on an HTML Button Element
- HTML:
- CSS:
- Using HTML Form Submit Button
- How to Add an HTML Button that Acts Like a Link
- Add an inline onclick event
- Example of adding an onclick event to the tag:
- Example of adding an onclick event to the tag:
- Use the action or formaction attribute.
- Example of creating a button acting like a link with the action attribute:
- Example of opening a link from a button in a new window:
- Example of creating a button acting like a link with the formaction attribute:
- Style the link as a button
- Example of styling a link as a button with CSS:
- Example of styling a link as a button and adding a hover effect:
- How about the accessibility?
- How to Create an HTML Button That Acts Like a Link
- How to Create an HTML Button Using the Button Tag in an A Tag
- How To Turn a Link Into a Button with CSS
- How to Put a Button inside a Form Using HTML
- How do I Create an HTML Button That Acts Like a Link?
How to Create an HTML Button That Acts Like a Link?
If you’re looking to create an HTML button that acts like a link (i.e. clicking on it takes you to a custom, specified link), you could do the following:
Using Inline onclick Event
You can simply use onclick event on an HTML button element to redirect to another URL, for example, like so:
Some of the downsides of using this approach might be:
- It won’t work when JavaScript is disabled;
- Opening a link in a new tab/window requires extra bit of coding;
- Browser won’t interpret it as a standard link, making it difficult to copy it to clipboard for sharing;
- Some search bots might ignore the link, making it bad for SEO.
Using a Link Styled as an HTML Button
You can style the HTML anchor element as an HTML button. For example, to style an HTML anchor element similar to the browser’s default styling of a button, you can do the following:
HTML:
CSS:
Some of the downsides of using this approach might be:
- Browser support for the CSS appearance property might be limited;
- Requires complex styling to get the default button styling (which may work only on certain browsers);
- Poor accessibility — does not trigger a click event when the SPACE keyboard key is pressed on link when it has focus.
Overlaying an Invisible Link on an HTML Button Element
You can overlay the HTML button with a link to keep the default appearance of the button, whilst using the functionality of a link:
HTML:
CSS:
.btn-link < display: inline-block; position: relative; >.btn-link > a < margin: auto; position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: 2; cursor: default; >.btn-link > a:active + button < border-style: inset; >.btn-link > button
Some of the downsides of using this approach might be:
- Uses an empty anchor tag;
- Requires an extra parent container element;
- Button active (and other) state styling might have to be done manually and may not resemble default active button styling 100%;
- Poor accessibility — does not trigger a click event when the SPACE keyboard key is pressed on link when it has focus.
Using HTML Form Submit Button
You can use an HTML button wrapped inside a form element with the action attribute specified. This would redirect you to the URL specified by the action attribute when the form is submitted. For example:
As an alternative, you can also use the HTML5 formaction attribute on the button element:
Some of the downsides of using these approaches might be:
- Although it validates as valid markup, it may not always be semantically correct because the form data is not being submitted (which is what it’s telling the browser it’s meant to do, code-wise) but instead it’s being used as a link;
- Submitting with GET will leave a trailing ? at the end of the resulting URL;
- Requires an extra form element.
Hope you found this post useful. It was published 27 Jun, 2016 (and was last revised 03 Mar, 2023 ). Please show your love and support by sharing this post.
How to Add an HTML Button that Acts Like a Link
There are several ways of creating an HTML button, that acts like a link (i.e., clicking on it the user is redirected to the specified URL). You can choose one of the following methods to add a link to the HTML button.
Add an inline onclick event
You can add an inline onclick event to the tag.
Example of adding an onclick event to the tag:
html> html> head> title>Title of the document title> head> body> button onclick="window.location.href='https://w3docs.com';"> Click Here button> body> html>
It is also possible to add an inline onclick event to the tag within the element.
Example of adding an onclick event to the tag:
html> html> head> title>Title of the document title> head> body> form> input type="button" onclick="window.location.href='https://www.w3docs.com';" value="w3docs" /> form> body> html>
Use the action or formaction attribute.
Another way of creating a button that acts like a link is using the action or formaction attribute within the element.
Example of creating a button acting like a link with the action attribute:
html> html> head> title>Title of the document title> head> body> form action="https://www.w3docs.com/"> button type="submit">Click me button> form> body> html>
To open the link in a new tab, add target=»_blank» .
Example of opening a link from a button in a new window:
html> html> head> title>Title of the document title> head> body> form action="https://www.w3docs.com/" method="get" target="_blank"> button type="submit">Click me button> form> body> html>
Since there is no form and no data is submitted, this may be semantically incorrect. However, this markup is valid.
Example of creating a button acting like a link with the formaction attribute:
html> html> head> title>Title of the document title> head> body> form> button type="submit" formaction="https://www.w3docs.com">Click me button> form> body> html>
The formaction attribute is only used with buttons having type=»submit» . Since this attribute is HTML5-specific, its support in old browsers may be poor.
Style the link as a button
Add a link styled as a button with CSS properties. A href attribute is the required attribute of the tag. It specifies a link on the web page or a place on the same page where the user navigates after clicking on the link.
Example of styling a link as a button with CSS:
html> html> head> title>Title of the document title> style> .button < background-color: #1c87c9; border: none; color: white; padding: 20px 34px; text-align: center; text-decoration: none; display: inline-block; font-size: 20px; margin: 4px 2px; cursor: pointer; > style> head> body> a href="https://www.w3docs.com/" class="button">Click Here a> body> html>
Let’s see one more example.
Example of styling a link as a button and adding a hover effect:
html> html> head> title>Title of the document title> style> .button < display: inline-block; padding: 10px 20px; text-align: center; text-decoration: none; color: #ffffff; background-color: #7aa8b7; border-radius: 6px; outline: none; transition: 0.3s; > .button:hover < background-color: #c2c7c7; > style> head> body> a class="button" href="https://www.w3docs.com/learn-html/html-button-tag.html">HTML button tag a> body> html>
How about the accessibility?
Let’s take accessibility into our account for the last example. Here are some improvements to make the code more accessible:
If the button contained an image, it would be important to provide an alt attribute to make the image accessible to screen readers. Since this button doesn’t have an image, we don’t need to worry about this.
Adding a label to the button will help users who rely on assistive technology understand the purpose of the button. We can do this by wrapping the button text in a element and adding an aria-label attribute to the button.
To improve visibility for users with low vision, we can increase the contrast between the text color and background color of the button. We can achieve this by making the background color darker or the text color lighter.
Adding a focus style to the button will help users who navigate using the keyboard to see which element is currently focused. We can add a simple border to the button to achieve this.
Here’s the updated code with these improvements:
html> html> head> title>Title of the document title> style> .button < display: inline-block; padding: 10px 20px; text-align: center; text-decoration: none; color: #ffffff; background-color: #3c5d6e; border-radius: 6px; outline: none; transition: 0.3s; border: 2px solid transparent; > .button:hover, .button:focus < background-color: #c2c7c7; border-color: #7aa8b7; > style> head> body> a class="button" href="https://www.w3docs.com/learn-html/html-button-tag.html" aria-label="Learn about the HTML button tag">span>HTML button tag span> a> body> html>
How to Create an HTML Button That Acts Like a Link
Sometimes you may want to use a button to link to another page or website rather than to submit a form or something like that. This is fairly simple to do and can be achieved in several ways.
How to Create an HTML Button Using the Button Tag in an A Tag
One way is to simply wrap your tag in an tag:
This transforms your entire button into a link.
How To Turn a Link Into a Button with CSS
A second option is to create your link as you normally would with your tag and then style it via CSS:
Once you’ve created your link, you can the use CSS to make it look like a button. For instance, you could add a border, a background color, some styles for when the user is hovering the link.
How to Put a Button inside a Form Using HTML
Another way to add a button is to wrap an input inside form tags. Specify the desired target URL in the form action attribute.
I hope you’ve found tutorial helpful. Happy coding.
If this article was helpful, tweet it .
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
How do I Create an HTML Button That Acts Like a Link?
You have a button that you want to use as a link on your website. How do you make a button, which is usually used to perform an action such as submitting a form or opening a menu, act like a link?
You can make a button act like a link or make a link look like a button.
Wrap the Button in a Link Tag
You can wrap a button in an tag to make it act like a link:
The problem with this solution is that if you run this HTML through the W3C Markup Validation Service, you’ll get the following message:
Error: The element button must not appear as a descendant of the a element.
This code is not good for accessibility. If a user is navigating using their keyboard, the link is focused and then the button is focused. This can be confusing for the user. Also, a button can be triggered by the enter key or space bar when it’s in focus. A link that’s in focus will only link to the new location if the enter key is pressed. Nothing will happen if the space bar is pressed. If the link looks like a button, the user may expect the space bar to work. You can fix this by adding a space bar event listener to the link and navigating to a new page using the following line of JavaScript code:
window.location.href = "https://sentry.io/answers/";
This is not ideal, as it requires extra code that you could easily forget to add.
Also, if you use a link as a button, you should override its role using the ARIA role attribute:
This will ensure that if a user is using a screen reader, the screen reader will announce the element is a link.
Styling a Link to Look Like a Button
Another option is not to use a button at all and style the tag to look like a button using CSS:
You can add styles using a CSS class, such as the “btn” class in the code above. It could have the following simple styles:
This solution is not great because links and buttons are used for different purposes:
- Links are used to take a user to a new location. This can be a new web page or a different section of the current page.
- Buttons trigger an action, for example, submitting a form or opening a menu.
Add a Button Inside a Form
You can add a button inside a form and add the link URL to the form action attribute:
You can also set the button’s type attribute to “reset” and use the onclick attribute to change the location:
Using a button inside a form element works but it’s not semantically correct. A form is typically used to submit information.
You can make a button that acts like a link or make a link that looks like a button but should avoid doing this. Links and buttons have different roles. If you must choose between the two, for example, if you are implementing a design created by a UX designer, make a link that is styled to look like a button. Using a button as a link can cause issues for users who navigate using their keyboard or who use a screen reader. You can read more about the accessibility concerns of using a button as a link in the following article: Button versus Link.