I would like to create an HTML button that works as if it were a clickable link. When I press the button, I would like it to redirect me to a page. And I would like it to be as accessible as possible.
I would also like there to be no extra characters or parameters in the URL.
How can this be done?
Links and buttons have different functionality: links connect to other content, while buttons will be associated with actions (they don't have to redirect anywhere or connect to anything).
Considering accessibility, it is not recommended to change a button to function as a link . In fact, that is the same suggestion that is made from W3C (my translation):
Not only is it semantically inconsistent, it can also lead to problems depending on the method you follow to implement it. For example, if the user has JavaScript disabled (rare in browsers, not so rare in some screen readers), some of the methods described in the Wiki answer won't work directly.
That's why, from an accessibility point of view, the best way to create a button that works like a link is to use a link and style it to look like a button:
If despite this recommendation you want to use a button as if it were a link, you can use any of the methods described above... But to make it accessible, you must tell the browser (or screen reader) that this button is not really a link. button but a link and that you should treat it as such. For this you must use
role="link"
.With
role="link"
you are indicating that the behavior of that element will be similar to that of a link . So you could have something like this:HTML
The plain HTML way to do this is to specify the destination URL in an
<formulario>
as an attributes action (action
).If necessary, set the CSS
display: inline
in the form to maintain the flow of the surrounding text. Instead of using<input type="submit">
in the example above, you can use<button type="submit">
. The only difference is that this element allows children.One intuitively expects to be able to use
<button href="http://google.com">
with the element<a>
, but unfortunately, this attribute does not exist according to the HTML specification .CSS
If you can use CSS, just use a
<a>
with the style to make it look like a button using other appearance properties (only IE supports it (as of July 2015) and it's still poor).Or use one of the many CSS libraries like bootstrap .
javascript
If JavaScript is allowed, set
window.location.href
.If what you want to achieve is the basic appearance of the button in plain HTML, inside an anchor (
<a>
) tag, then you can use the Twitter Bootstrap framework to format any of the following regular HTML buttons/links as if it were a button. There are slight differences between versions of this framework, keep that in mind.Bootstrap (v2) display example:
Bootstrap (v3) display example:
See Bootstrap (v2) or Bootstrap (v3) in the buttons section for some examples.
If what you want to do is an html link with the appearance of a button, that can be easily achieved by giving a css class to the link in question, let me explain:
To give it a finished look I recommend using the Bootstrap style libraries, placing the Bootstrap cdn in the head of your page (the cdn is a connector that allows you to consume the libraries without physically having them in your project, you must be connected to the internet to be able to use the styles, this is important!).
Subsequently, create any link and add a class to it, Bootstrap has many, the class for buttons is btn, in its different guises btn-success, btn-warning, btn-danger, among others.
I hope I have helped. Success with your project!