Как встроить ссылку в кнопку html
Перейти к содержимому

Как встроить ссылку в кнопку html

  • автор:

How to Create an HTML Button that Acts Like a Link

W3docs

S ometimes we need to create an HTML button, that acts like a link (i.e., clicking on it the user is redirected to the specified URL).

There are some methods, we are going to present 3 of them. Choose one of the following methods to add a link to an HTML button.

All right. Let’s go!

1. Add inline onclick event

a) To HTML <button> tag within HTML <form> element.

Example

It might not work if the button is inside a <form> tag.

b) To <input> tag within HTML <form> element.

Example

The links won’t work when JavaScript is disabled, and search engines may ignore such kind of links.

2. Use action or formaction attributes within <form> element

a) Action attribute

To open the link in a new tab add attribute target=”_blank”.

As there is no form and no data is submitted, this may be semantically incorrect. However, this markup is valid.

b) HTML5 formaction attribute.

Example

The formaction attribute is only used for buttons with type=”submit”. As this attribute is HTML5-specific, support in old browsers might be poor.

3. Add a link styled as an HTML button (using CSS)

Example

As complex styling is required, this may not work on specific browsers.

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 <button> tag.

Example of adding an onclick event to the <button> tag:

It is also possible to add an inline onclick event to the <input> tag within the <form> element.

Example of adding an onclick event to the <input> tag:

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 <form> element.

Example of creating a button acting like a link with the action attribute:

To open the link in a new tab, add target=»_blank» .

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

Add a link styled as a button with CSS properties. A href attribute is the required attribute of the <a> 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:

Let’s see one more example.

Example of styling a link as a button and adding a hover effect:

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:

  1. Add a meaningful alt attribute to the image:

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.

  1. Add a label to the button:

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 <span> element and adding an aria-label attribute to the button.

  1. Increase contrast:

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.

  1. Add focus styles:

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.

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:

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:

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.

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.

Please enable JavaScript

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.

QUICK SLIDES

TABLE OF CONTENTS

DOWNLOAD & NOTES

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

QUICK NOTES

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.

BUTTON LINKS

All right, let us now get into the various ways to create a “button link” in HTML.

METHOD 1) JAVASCRIPT REDIRECT

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

METHOD 2) SUBMIT 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 open 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.

METHOD 3) FORM ACTION

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.

METHOD 4) TURN LINK INTO BUTTON WITH CSS

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.

METHOD 5) WRAP BUTTON INSIDE LINK

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.

LINKS & REFERENCES

    – MDN – MDN – W3Docs – Stack Overflow

YOUTUBE TUTORIAL

INFOGRAPHIC CHEAT SHEET

How To Create HTML Button Links (click to enlarge)

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!

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *