How do I create an HTML button that acts like a link? So that clicking the button redirects the user to a page.
I want it to be accessible, and with minimal extra characters or parameters in the URL.
?
on the URL. This is caused by the form being type="GET"
, change this to type="POST"
and the ?
at the end of the URL disappears. This is because GET sends all variables in the URL, hence the ?
.
GET
it will fail. I still think that using a link make sense with the caveat that it will not react to "spacebar" when active like button does. Also some style and behavior will be different (such as draggable). If you want the true "button-link" experience, having server side redirects for URL finishing by ?
to remove it might be also an option.
HTML
The plain HTML way is to put it in a <form>
wherein you specify the desired target URL in the action
attribute.
<form action="https://google.com">
<input type="submit" value="Go to Google" />
</form>
If necessary, set CSS display: inline;
on the form to keep it in the flow with the surrounding text. Instead of <input type="submit">
in above example, you can also use <button type="submit">
. The only difference is that the <button>
element allows children.
You'd intuitively expect to be able to use <button href="https://google.com">
analogous with the <a>
element, but unfortunately no, this attribute does not exist according to HTML specification.
CSS
If CSS is allowed, simply use an <a>
which you style to look like a button using among others the appearance
property (it's only not supported in Internet Explorer).
a.button { -webkit-appearance: button; -moz-appearance: button; appearance: button; text-decoration: none; color: initial; } Go to Google
Or pick one of those many CSS libraries like Bootstrap.
<a href="https://google.com" class="btn btn-primary">Go to Google</a>
JavaScript
If JavaScript is allowed, set the window.location.href
.
<input type="button" onclick="location.href='https://google.com';" value="Go to Google" />
Instead of <input type="button">
in above example, you can also use <button>
. The only difference is that the <button>
element allows children.
Note that the type="button"
attribute is important, since its missing value default is the Submit Button state.
If it's the visual appearance of a button you're looking for in a basic HTML anchor tag then you can use the Twitter Bootstrap framework to format any of the following common HTML type links/buttons to appear as a button. Please note the visual differences between version 2, 3 or 4 of the framework:
<a class="btn" href="">Link</a>
<button class="btn" type="submit">Button</button>
<input class="btn" type="button" value="Input">
<input class="btn" type="submit" value="Submit">
Bootstrap (v4) sample appearance:
https://i.stack.imgur.com/ytswI.gif
Bootstrap (v3) sample appearance:
https://i.stack.imgur.com/pXgPr.png
Bootstrap (v2) sample appearance:
https://i.stack.imgur.com/bMTDv.gif
Use:
<a href="http://www.stackoverflow.com/">
<button>Click me</button>
</a>
Unfortunately, this markup is no longer valid in HTML5 and will neither validate nor always work as potentially expected. Use another approach.
As of HTML5, buttons support the formaction
attribute. Best of all, no JavaScript or trickery is needed.
Caveats
Must be surrounded by
It is actualy very simple and without using any form elements. You can just use the tag with a button inside :).
Like this:
<a href="http://www.google.com" target="_parent"><button>Click me !</button></a>
And it will load the href into the same page. Want a new page? Just use target="_blank"
.
EDIT
Couple of years later, while my solution still works, keep in mind you can use a lot of CSS to make it look whatever you want. This was just a fast way.
<button type="button">...
so that it doesn't by default function as a Submit
If you are using an inside form, add the attribute type="reset" along with the button element. It will prevent the form action.
<button type="reset" onclick="location.href='http://www.example.com'">
www.example.com
</button>
type="button"
<form>
<input type="button" value="Home Page" onclick="window.location.href='http://www.wherever.com'">
</form>
input
does not have an ending tag.
input
element is a void element. It must have a start tag but must not have an end tag.
There seems to be three solutions to this problem (all with pros and cons).
Solution 1: Button in a form.
<form method="get" action="/page2">
<button type="submit">Continue</button>
</form>
But the problem with this is that in some version of popular browsers such as Chrome, Safari and Internet Explorer, it adds a question mark character to the end of the URL. So in other words for the code above your URL will end up looking like this:
http://someserver/pages2?
There is one way to fix this, but it will require server-side configuration. One example using Apache Mod_rewrite would be to redirect all requests with a trailing ?
to their corresponding URL without the ?
. Here is an example using .htaccess, but there is a full thread here:
RewriteCond %{THE_REQUEST} \?\ HTTP [NC]
RewriteRule ^/?(index\.cfm)? /? [R=301,L]
Similar configurations can vary depending on the webserver and stack used. So a summary of this approach:
Pros:
This is a real button, and semantically it makes sense. Since it is a real button, it will also act like a real button (e.g. draggable behavior and/or mimic a click when pressing space bar when active). No JavaScript, no complex style required.
Cons:
Trailing ? looks ugly in some browsers. This can be fixed by a hack (in some cases) using POST instead of GET, but the clean way is to have a server-side redirect. The downside with the server side redirect is that it will cause an extra HTTP call for these links because of the 304 redirect. Adds extra
You can simply put a tag around the element:
<a href="http://google.com" target="_blank">
<button>My Button</button>
</a>
https://jsfiddle.net/hj6gob8b/
<button>
inside <a>
.
Just place your button inside of a reference tag, e.g.,
<a href="https://www.google.com/"><button>Next</button></a>
This seems to work perfectly for me and does not add any %20 tags to the link, just how you want it. I have used a link to Google to demonstrate.
You could of course wrap this in a form tag, but it is not necessary.
When linking another local file, just put it in the same folder and add the file name as the reference. Or specify the location of the file if in is not in the same folder.
<a href="myOtherFile"><button>Next</button></a>
This does not add any character onto the end of the URL either, however it does have the files project path as the URL before ending with the name of the file. e.g
If my project structure was...
.. denotes a folder \
denotes a file while four | denote a sub directory or file in parent folder
..public |||| ..html |||| |||| -main.html |||| |||| -secondary.html
If I open file main.html, the URL would be,
http://localhost:0000/public/html/main.html?_ijt=i7ms4v9oa7blahblahblah
However, when I clicked the button inside main.html to change to secondary.html, the URL would be,
http://localhost:0000/public/html/secondary.html
No special characters are included at the end of the URL.
By the way - (%20 denotes a space in a URL it encoded and inserted in the place of them.)
Note: The localhost:0000 will obviously not be 0000. You'll have your own port number there.
Furthermore, the ?_ijt=xxxxxxxxxxxxxx at the end of the main.html URL, x is determined by your own connection, so obviously it will not be equal to mine.
It might seem like I'm stating some really basic points, but I just want to explain as best as I can.
If you want to avoid having to use a form or an input and you're looking for a button-looking link, you can create good-looking button links with a div wrapper, an anchor and an h1
tag. You'd potentially want this so you can freely place the link-button around your page. This is especially useful for horizontally centering buttons and having vertically-centered text inside of them. Here's how:
Your button will be comprised of three nested pieces: a div wrapper, an anchor, and an h1, like so:
.link-button-wrapper { width: 200px; height: 40px; box-shadow: inset 0px 1px 0px 0px #ffffff; border-radius: 4px; background-color: #097BC0; box-shadow: 0px 2px 4px gray; display: block; border:1px solid #094BC0; } .link-button-wrapper > a { display: inline-table; cursor: pointer; text-decoration: none; height: 100%; width:100%; } .link-button-wrapper > a > h1 { margin: 0 auto; display: table-cell; vertical-align: middle; color: #f7f8f8; font-size: 18px; font-family: cabinregular; text-align: center; }
Here's a jsFiddle to check it out and play around with it.
Benefits of this setup: 1. Making the div wrapper display: block makes it easy to center (using margin: 0 auto) and position (while an is inline and harder to positionand not possible to center).
You could just make the display:block, move it around, and style it as a button, but then vertically aligning text inside of it becomes hard. This allows you to make the display: inline-table and the display: table-cell, which allows you to use vertical-align: middle on the
and center it vertically (which is always nice on a button). Yes, you could use padding, but if you want your button to dynamically resize, that won't be as clean. Sometimes when you embed an within a div, only the text is clickable, this setup makes the whole button clickable. You don't have to deal with forms if you're just trying to move to another page. Forms are meant for inputting information, and they should be reserved for that. Allows you to cleanly separte the button styling and text styling from each other (stretch advantage? Sure, but CSS can get nasty-looking so it's nice to decompose it).
It definitely made my life easier styling a mobile website for variable-sized screens.
The only way to do this (except for BalusC's ingenious form idea!) is by adding a JavaScript onclick
event to the button, which is not good for accessibility.
Have you considered styling a normal link like a button? You can't achieve OS specific buttons that way, but it's still the best way IMO.
Going along with what a few others have added, you can go wild with just using a simple CSS class with no PHP, no jQuery code, just simple HTML and CSS.
Create a CSS class and add it to your anchor. The code is below.
.button-link {
height:60px;
padding: 10px 15px;
background: #4479BA;
color: #FFF;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
border: solid 1px #20538D;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.4);
-webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
-moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
}
.button-link:hover {
background: #356094;
border: solid 1px #2A4E77;
text-decoration: none;
}
<HTML>
<a class="button-link" href="http://www.go-some-where.com"
target="_blank">Press Here to Go</a>
That is it. It is very easy to do and lets you be as creative as you'd like. You control the colors, the size, the shapes(radius), etc. For more details, see the site I found this on.
Seven ways to do that:
Using window.location.href = 'URL' Using window.location.replace('URL') Using window.location = 'URL' Using window.open('URL') Using window.location.assign('URL') Using HTML form Using HTML anchor tag
To Nicolas' answer, the following worked for me as that answer didn't have type="button"
due to which it started behaving as submit type...since I already have one submit type. It didn't work for me ... and now you can either add a class to the button or to <a>
to get the required layout:
<a href="http://www.google.com/">
<button type="button">Click here</button>
</a>
Another option is to create a link in the button:
<button type="button"><a href="yourlink.com">Link link</a></button>
Then use CSS to style the link and button, so that the link takes up the entire space within the button (so there's no miss-clicking by the user):
button, button a{position:relative;}
button a{top:0;left:0;bottom:0;right:0;}
I have created a demo here.
Keep in mind the spec says this is not valid as buttons should not contain any interactive descendants.
If you want to create a button that is used for a URL anywhere, create a button class for an anchor.
a.button {
background-color: #999999;
color: #FFFFFF !important;
cursor: pointer;
display: inline-block;
font-weight: bold;
padding: 5px 8px;
text-align: center;
-webkit-border-radius: 5px;
border-radius: 5px;
}
.button:hover {
text-decoration: none;
}
I knew there have been a lot of answers submitted, but none of them seemed to really nail the problem. Here is my take at a solution:
Use the
Also you can use a button:
For example, in ASP.NET Core syntax:
// Some other tags
<form method="post">
<input asp-for="YourModelPropertyOrYourMethodInputName"
value="@TheValue" type="hidden" />
<button type="submit" class="link-button" formaction="/TheDestinationController/TheDestinationActionMethod">
@(TextValue)
</button>
</form>
// Other tags...
<style>
.link-button {
background: none !important;
border: none;
padding: 0 !important;
color: #20a8d8;
cursor: pointer;
}
</style>
For HTML 5 and a styled button along with an image background
People who have answered using <a></a>
attributes on a <button></button>
was helpful.
But then recently, I encountered a problem when I used a link inside a <form></form>
.
The button is now regarded like/as a submit button (HTML5). I've tried working a way around and have found this method.
Create a CSS style button like the one below:
.btn-style {
border: solid 1px #0088cc;
border-radius: 6px;
moz-border-radius: 6px;
-webkit-box-shadow: 0px 0px 2px rgba(0, 0, 0, 1.0);
-moz-box-shadow: 0px 0px 2px rgba(0, 0, 0, 1.0);
box-shadow: 0px 0px 2px rgba(0, 0, 0, 1.0);
font-size: 18px;
color: #696869;
padding: 1px 17px;
background: #eeeeee;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #eeeeee), color-stop(49%, #eeeeee), color-stop(72%, #cccccc), color-stop(100%, #eeeeee));
background: -moz-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
background: -webkit-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
background: -o-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
background: -ms-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
background: linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#eeeeee', endColorstr='#eeeeee', GradientType=0);
}
Or create a new one here: CSS Button Generator
And then create your link with a class tag named after the CSS style you have made:
<a href='link.php' class='btn-style'>Link</a>
Here's a fiddle:
Create a button using the <a>
tag and proper css
.abutton { background: #bada55; padding: 5px; border-radius: 5px; transition: 1s; text-decoration: none; color: black; } .abutton:hover { background: #2a2; } Continue
You could also set the buttons type-property
to "button" (it makes it not submit the form), and then nest it inside a link (makes it redirect the user).
This way you could have another button in the same form that does submit the form, in case that's needed. I also think this is preferable in most cases over setting the form method and action to be a link (unless it's a search-form I guess...)
Example:
This way the first button redirects the user, while the second submits the form.
Be careful to make sure the button doesn't trigger any action, as that will result in a conflict. Also as Arius pointed out, you should be aware that, for the above reason, this isn't strictly speaking considered valid HTML, according to the standard. It does however work as expected in Firefox and Chrome, but I haven't yet tested it for Internet Explorer.
You can use JavaScript:
Replace http://www.google.com
with your website, and make sure to include http://
before the URL.
In JavaScript
setLocation(base: string) {
window.location.href = base;
}
In HTML
<button onclick="setLocation('/<whatever>')>GO</button>"
Type window.location
and press Enter in your browser console. Then you can get the clear idea what location
contains:
hash: ""
host: "stackoverflow.com"
hostname: "stackoverflow.com"
href: "https://stackoverflow.com/questions/2906582/how-to-create-an-html-button-
that-acts-like-a-link"
origin: "https://stackoverflow.com"
pathname: "/questions/2906582/how-to-create-an-html-button-that-acts-like-a-link"
port: ""
protocol: "https:"
You can set any value from here.
So for redirecting another page, you can set the href
value with your link.
window.location.href = your link
In your case:
<button onclick="window.location.href='www.google.com'">Google</button>
If you are using an SSL certificate:
<a href="https://www.google.com" target="_blank"><button>Click me !</button></a>
HTML Answer: If you want to create an HTML button that acts like a link, use the two common attributes for it: <a>
and/or action=""
:
<form action="stackoverflow.com"/>
<button type="submit" value="Submit Form"
Or...
"href" is part of the <a>
attribute. It helps direct links:
<a href="stackoverflow.com">Href</a>
Success story sharing
display: inline
to the form to keep the button in the flow.