5 Ways To Create HTML Button Links – Simple Examples

Welcome to a quick tutorial on how to create HTML button links. Text links work great, but they sure are not catchy enough. Also on mobile devices, one with fat fingers may have a little trouble with tapping that small text link.

The easy way to create HTML button links is to wrap the button in an anchor tag – <a href="https://code-boxx.com"><button>Go to Code Boxx</button></a>

 

But there are actually several other ways to do so – Read on to find out!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Button Links Useful Bits & Links
The End

 

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

1) JAVASCRIPT REDIRECT

<button onclick="location = 'https://code-boxx.com'">
  CLICK HERE TO ACCESS CODE BOXX
</button>

The easiest way to create a button link is probably to handle it with a little bit of Javascript.

  • 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.

P.S. You will notice that some other tutorials use location.href = 'URL' instead. That is not wrong and is the “old fashion” way of redirecting. Both will still work, location is a synonym of location.href.

 

 

2) SUBMIT FORM

<form action="https://code-boxx.com">
<!-- We can use a target="_blank" property to open the form in a new tab instead
<form action="https://code-boxx.com" target="_blank">
-->
  <input type="submit" value="CLICK HERE TO ACCESS CODE BOXX"/>
</form>

Who says that we can’t use forms in creative ways? The above will submit an empty form, which effectively acts just like a link. A couple of short notes for the absolute beginners:

  • Change the action="URL" to redirect the user to where you want.
  • We can also add a target="_blank" to open in a new tab. Otherwise if not defined, it will default to target="_self" and opens in the same window.
  • Take note that we are using <input type="submit"> here instead of <input type="button"> – Submit will process the form when clicked, a button will not.

 

3) FORM ACTION

<form action="https://code-boxx.com">
  <input type="submit" value="ACCESS CODE BOXX"/>
  <input type="submit" value="ACCESS WIKIPEDIA" formaction="https://wikipedia.com"/>
  <input type="submit" value="ACCESS GOOGLE" formaction="http://google.com/"/>
</form>

The formaction is a property added in HTML5, and it is actually used to submit a form to an alternative URL. In the case of “button to link”, we can use it as a convenience to create multiple links in a single form.

 

 

4) TURN LINK INTO BUTTON WITH CSS

<style>
.linkbutton {
  display: inline-block;
  padding: 10px;
  margin: 5px;
  background: #8a110b;
  color: #fff;
  text-decoration: none;
}
</style>

<a class="linkbutton" href="https://code-boxx.com">
  Go to Code Boxx
</a>

The beauty of HTML and CSS – We can pretty much customize almost every element. Yes, we can even customize a link to look exactly like a button… But it is still essentially a link.

 

5) WRAP BUTTON INSIDE LINK

<a href="https://code-boxx.com">
  <button>Go to Code Boxx</button>
</a>

It is not a secret, we can wrap images in links. So yes, we can have the best of both worlds – Wrap buttons in links.

 

 

USEFUL BITS & LINKS

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?

Personally, I will either wrap the button in a link or use CSS to turn the link into a button. The main reasons are actually the ease of implementation, and for the purpose of search engine optimization (SEO). This might be a slightly advanced topic, but in short, we want to help search engines to find relevant links on the webpage as easily as possible.

For example, if we have a page about how to play the piano, we will want to include relevant links to other relevant piano pages… But here’s the problem – Search engines won’t necessarily follow all the Javascripts and forms to see where they lead to. So stick with using the traditional <a> as much as possible.

 

YOUTUBE TUTORIAL

 

INFOGRAPHIC CHEAT SHEET

How To Create HTML Button Links (click to enlarge)

 

LINKS & REFERENCES

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *