Welcome to a tutorial on how to create HTML button links. Text links work great, but they sure are painful on small mobile screens. To create an HTML “button link”, simply wrap the button in anchor tags – <a href="http://site.com"><button>Link</button></a>
. But there are other ways to do it – Read on for the examples!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Click here to download | Example on CodePen
The example code is released under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
HTML BUTTON LINKS
All right, let us now get into the various ways to create a “button link” in HTML.
TUTORIAL VIDEO
1) WRAP BUTTON IN ANCHOR TAGS
<a href="https://code-boxx.com">
<button>Code Boxx</button>
</a>
- It is not a secret that we can wrap images in anchor tags.
- So yes, we can also wrap buttons in anchor tags to create “button links”.
2) JAVASCRIPT REDIRECT
<button onclick="location='https://code-boxx.com'">
Code Boxx
</button>
This is another easy way to create a button link.
- The
onclick
property is used to specify what to do when the button is clicked. location='URL'
will set where to redirect to when the button is clicked.
3) SUBMIT FORM
<form action="https://code-boxx.com" target="_blank">
<input type="submit" value="Code Boxx">
</form>
A “creative way” to use forms. This example will submit an empty form, which effectively acts like a link.
action="URL"
Redirect the user to where you want.target="_blank"
Optional, open in a new tab. If this is not defined, it will default totarget="_self"
and open in the same window.
4) FORM ACTION
<form action="https://code-boxx.com">
<input type="submit" value="Code Boxx">
<input type="submit" value="Wikipedia" formaction="https://wikipedia.com">
<input type="submit" value="Google" formaction="https://google.com/">
</form>
A quick follow up of the previous example:
- An HTML form can have multiple
<input type="submit">
buttons. - Use the
formaction
property to submit the form to an alternative URL. - We can use this to create multiple links in a single form.
5) TURN LINK INTO BUTTON WITH CSS
<style>
.linkbutton {
display: inline-block;
padding: 10px; margin: 5px;
color: #fff; background: #8a110b;
text-decoration: none;
}
</style>
<a class="linkbutton" href="https://code-boxx.com">
Code Boxx
</a>
Finally, turn the link to look like a button using some simple CSS.
EXTRAS
That’s all for this tutorial, and here is a small section on some extras and links that may be useful to you.
WHICH IS THE BEST METHOD?
- Personal preference – Just wrap the button in anchor tags or turn the link into button using CSS.
- These 2 methods are also the most commonly used.
- Javascript and form submit works, but these are considered “funky” and not good for search engine optimization (SEO).
LINKS & REFERENCES
- Window Location – MDN
- Button – MDN
INFOGRAPHIC CHEAT SHEET
THE END
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!