40

Basically, I like the way that <input type="submit"> is styled, with the clickable button when you add a little CSS. However, regular buttons are not styled as such, they have no such clickability without some major CSS or JS, and you have to use images.

I made submit buttons into links, by using the form action, but this requires me to make a new form for each button. How can I find a happy medium? Using multiple forms is causing problems in my styling that I can't seem to fix unless I find another way to make buttons that are links in HTML, that have a default style that makes them have a pressed state (and I don't mean browser default settings).

Any ideas?

Alohci
  • 78,296
  • 16
  • 112
  • 156
AKor
  • 8,550
  • 27
  • 82
  • 136

7 Answers7

75
<a href="#"><button>Link Text</button></a>

You asked for a link that looks like a button, so use a link and a button :-) This will preserve default browser button styling. The button by itself does nothing, but clicking it activates its parent link.

Demo:

<a href="http://stackoverflow.com"><button>Link Text</button></a>
TrakJohnson
  • 1,755
  • 2
  • 18
  • 31
edeverett
  • 8,012
  • 33
  • 28
20

IMPORTANT: <button> should never be a descendent of <a>.

Try <a href="http://stackoverflow.com"><button>Link Text</button></a> in any html validator like https://validator.w3.org and you'll get an error. There's really no point in using a button if you're not using the button. Just style the <a> with css to look like a button. If you're using a framework like Bootstrap, you could apply the button style(s) btn, btn-primary etc.

jsfiddle : button styled link

.btnStack {
  font-family: Oswald;
  background-color: orange;
  color: #000;
  text-decoration: none;
  display: inline-block;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 14px;
  font-weight: normal;
  line-height: 1.428571429;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}
a.btnStack:hover {
  background-color: #000;
  color: #fff;
}
<link href='https://fonts.googleapis.com/css?family=Oswald:400' rel='stylesheet' type='text/css'>
<a href="http://stackoverflow.com" class="btnStack">stack<b>overflow</b>.com</a>
joby-flick
  • 1,790
  • 1
  • 12
  • 21
8

Use javascript:

<button onclick="window.location.href='/css_page.html'">CSS page</button>

You can always style the button in css anyaways. Hope it helped!

Good luck!

Freeranger
  • 113
  • 1
  • 10
5

You have three options:

  • Style links to look like buttons using CSS.

    Just look at the light blue "tags" under your question.

    It is possible, even to give them a depressed appearance when clicked (using pseudo-classes like :active), without any scripting. Lots of major sites, such as Google, are starting to make buttons out of CSS styles these days anyway, scripting or not.

  • Put a separate <form> element around each one.

    As you mentioned in the question. Easy and will definitely work without Javascript (or even CSS). But it adds a little extra code which may look untidy.

  • Rely on Javascript.

    Which is what you said you didn't want to do.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • Does making separate
    elements cause any sort of spacing? I've tried to make my two buttons appear side-by-side but the stylesheets I'm using (I didn't write them) are very numerous and must have some sort of margins or padding somewhere that causes them to be on top of each other. Any way I can override this or find out where the styling is held for this particular element?
    – AKor Jul 27 '10 at 06:26
  • 2
    You'd use CSS to remove the spacing. Install Firebug and use "Inspect Element" to find out where the existing spacing is coming from. – thomasrutter Jul 27 '10 at 06:27
1

A little bit easier and it looks exactly like the button in the form. Just use the input and wrap the anchor tag around it.

<a href="#"><input type="button" value="Button Text"></a>
Jared
  • 19
  • 1
1

<a id="reset-authenticator" asp-page="./ResetAuthenticator"><input type="button" class="btn btn-primary" value="Reset app" /></a>

Nithya
  • 17
  • 2
-3

The 3 easiest ways IMHO are

1: you create an image of a button and put a href around it. (Not a good way, you lose flexibility and will provide a lot of difficulties and problems.)

2 (The easiest one) -> JQuery

<input type="submit" someattribute="http://yoururl/index.php">

  $('button[type=submit] .default').click(function(){
     window.location = $(this).attr("someattribute");
     return false; //otherwise it will send a button submit to the server

   });  

3 (also easy but I prefer previous one):

<INPUT TYPE=BUTTON OnClick="somefunction("http://yoururl");return false" VALUE="somevalue">

$fn.somefunction= function(url) {
    window.location = url;
};
Nealv
  • 6,856
  • 8
  • 58
  • 89
  • 1
    The image option will not have the behaviours of a normal browser button (expanding according to text content, depressing, etc.) The Javascript option is overkill and leaves a useless button in the page if Javascript fails to load or is turned off. – edeverett Jul 27 '10 at 08:22
  • if you know some css and cutting the image button WILL stretch and WILL have the behaviour of normak browser buttons. – Nealv Jul 27 '10 at 08:23
  • For my sins I've done this plenty in the past, but coping with things like text scaling and the different browser's default button styles make styling a link as a fake button a time consuming compromise. It can work in limited circumstances but is not something I'd recommend. – edeverett Jul 27 '10 at 08:40
  • I wouldn't recommend it either, it was only an alternative :) but ok I'll leave it by that – Nealv Jul 27 '10 at 08:44