Html link with button style

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.
Читайте также:  Java язык программирования сообщение

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.

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.

Источник

HTML Button Link Code Examples – How to Make HTML Hyperlinks Using the HREF Attribute on Tags

In this article, we are going to explore three different ways you can make an HTML button act like a link.

These are the methods we’ll go over:

  1. Styling a link to look like a button
  2. Using the action and formaction attributes in a form
  3. Using the JavaScript onclick event

But first, let’s take a look at the wrong approach.

Why doesn’t this approach with the a element work?

The code snippet below leads to the freeCodeCamp website when it is clicked.

However, this is not valid HTML.

The a element can be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g., buttons or other links). — (Source: Web Hypertext Application Technology Working Group)

This is considered bad practice because it makes it unclear as to the user’s intent.

Links are supposed to navigate the user to another part of the webpage or an external site. And buttons are supposed to perform a specific action like submitting a form.

When you nest one inside the other, it makes it confusing as to what action you want performed. That is why it is best to not nest a button inside an anchor tag.

This first approach does not use the button at all. We can style an anchor tag to look like a button using CSS.

This is the default HTML styling for an anchor tag.

blue-anchor-tag

We can add a class to the anchor tag and then use that class selector to style the element.

If you wanted the link to open up a new page, you can add the target=»_blank» attribute like this:

Then, we can add a background color and change the font color like this:

background-and-white-text

The next step would be to add some padding around the text:

adding-padding-1

Lastly, we can use the text-decoration property to remove the underline from the text:

removing-underline

Now we have an anchor tag that looks like a button.

We can also make this «button» be a little more interactive by changing the background color depending on the state of the link.

bootstrap-styles

If your project already includes Bootstrap, then you can use the built-in button styles. But I would not import Bootstrap just to style one link.

What are the issues with this approach?

There is some debate whether it is good practice to style links as buttons. Some will argue that links should always look like links and buttons should look like buttons.

In the web book titled Resilient Web Design, Jeremy Keith states that

Why did I bother to bring up this debate?

My goal is not to make you choose one side of the debate over another. I just want you to be aware of this ongoing discussion.

How to use the action and formaction attributes to make a button in a form

How to use the action attribute

Another alternative would be to nest the button inside a form and use the action attribute.

This would be the default button style.

default-button-style

We could use the same styles as earlier, but we would have to add the cursor pointer and set the border to none, like this:

removing-underline-1

How to use the formaction attribute

Similar to the previous approach, we can create a form and use the formaction attribute.

You can only use the formaction attribute with inputs and buttons that have type=»image» or type=»submit» .

Is this semantically correct?

While this appears to be a working solution, there is a question if this is semantically correct.

We are using the form tags but this does not function like a real form. The purpose of a form is to collect and submit user data.

But we are using the submit button to navigate the user to another page.

When it comes to semantics, this is a not a good way to use the form tags.

Side effects for using the action and formaction attributes

When you click on the button, something interesting happens with the URL. The URL now has a question mark at the end of it.

question-mark-at-end

The reason for this change is because the form is using the GET method. You could switch to the POST method, but there might be cases where that is not ideal either.

While this approach is valid HTML, it does come with this unintended side effect.

How to use the JavaScript onclick event to make a button

In the previous approaches, we have looked at HTML and CSS solutions. But we can also use JavaScript to achieve the same result.

The location.href represents the location of a specific URL. In this case, Window.location.href will return https://www.freecodecamp.org/.

Drawbacks to this approach

While this solution does work, there are some potential issues to consider.

If the user has decided to disable JavaScript in their browser, then clearly this solution would not work. Unfortunately, that could lead to a poor user experience.

Conclusion

The goal of this article was to show you three different ways you can make buttons act like links.

The first approach was to design a link to look like a button. We also looked into the debate whether it is a good idea to change the appearance of links to look like another element.

The second approach used the form and formaction attributes. But we also learned that this approach has some side effects with the URL and is not semantically correct.

The third approach used the JavaScript onclick event and the Window.location.href. But we also learned that this approach might not work if the user decides to disable JavaScript in their browser.

As a developer, it is really important to look at the pros and cons of a particular approach before incorporating it into your project.

I hope you enjoyed this article and learned a few things along the way.

Источник

Оцените статью