377

I need to make a button look like a link using CSS. The changes are done but when I click on it, it shows as if it's pushed as in a button. Any idea how to remove that, so that the button works as a link even when clicked?

James Hibbard
  • 16,490
  • 14
  • 62
  • 74
  • 1
    cletus: rather “why not use a link” ;) – knittl Sep 02 '09 at 12:36
  • 1
    Dare I ask why not just use a link? – cletus Sep 02 '09 at 12:37
  • Using a button is easier as the onclick event is easier to use than the navigateURL to trigger JS functions. –  Sep 02 '09 at 12:37
  • 5
    onclick events on links are as simple as on buttons – knittl Sep 02 '09 at 13:13
  • 3
    @knittl, @cletus Actually, links and buttons have very different meanings in HTML. You should read http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#links. It may be not a good idea to style button to look like link, but it depends on UI design, while using link instead is against HTML specs. – Anton Strogonoff Mar 27 '11 at 10:47
  • 6
    There are a few reasons why styling a button like a link may be necessary. (1) button has type="submit" (2) button has fancy styling ie background image with variable length – T. Junghans Apr 27 '12 at 08:31
  • 116
    It also may be more semantically correct for something to be a button, even if you want it to look like a link. For example imagine a pair of "Expand All | Collapse All" links that change something on the page. Clicking these causes an action, but doesn't take the user anywhere - the semantics are those of a button. However the designer may have specfied links for appearance reasons. So this is actually a very good question. – shacker May 15 '12 at 23:38
  • 20
    I'm using a button instead of a link because triggering JS methods with a link, I'm forced to link to `#` then use `event.preventDefault()`. That's nasty, as is linking to `javascript:void(0);`. – Archonic Apr 15 '13 at 21:44
  • 1
    @knittl links should not be used for delete action for example – user1068352 Jul 08 '13 at 07:18
  • 2
    Using a button allows you to issue a POST request, which is spec-defined to be non-idempotent. – Christopher Schultz Dec 10 '15 at 14:10
  • "I'm forced to link to #" --- Really ... by whom? Unless you have some JS that's enforcing a policy you can do: onclick="foo();return false;" – Kevin_Kinsey Oct 20 '17 at 21:03
  • Styling a button as a link is an easy way to get a menu item which looks and behaves like `text` but is actually `
    `, so it GETs or POSTs data.
    – Adam Chalcraft May 18 '19 at 08:25

8 Answers8

470

button {
  background: none!important;
  border: none;
  padding: 0!important;
  /*optional*/
  font-family: arial, sans-serif;
  /*input has OS specific font-family*/
  color: #069;
  text-decoration: underline;
  cursor: pointer;
}
<button> your button that looks like a link</button>
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
adardesign
  • 33,973
  • 15
  • 62
  • 84
  • 27
    My very slight variation on the fiddle - moved the text-decoration:underline rule to act on button:hover. see in my [forked fiddle](http://jsfiddle.net/zYCN9/1/). – odedbd Jan 30 '13 at 07:18
  • 12
    If you don't want all buttons to be styled as links, scope the style with something like `button.link {...styles...}` then ``. This also avoids using `!important` which is always a good idea. – Archonic Apr 15 '13 at 21:47
  • `border-bottom` is a bit of a hack for making it looked underlined. This answer is otherwise a good start, but the second answer here is really more thorough and has the right solution for underlining. – michael Mar 31 '14 at 04:29
  • 3
    Don't forget `font: inherit`; most browsers use their own font for buttons. – rvighne May 21 '14 at 00:50
  • Expanding on @SiliconMind's comment, to better simulate a link, you could include something like: `button:active, button:focus { outline: none; }`. This will get rid of the outline that browsers tend to place around the button while your mouse click is held down (active), and released (focused). – Joe Majewski Jun 02 '16 at 20:58
  • 6
    Please do not remove the outline as that would make your button inaccessible: http://www.outlinenone.com – Dominik Aug 23 '16 at 00:49
  • ... and add `outline:none`. – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ Oct 11 '16 at 15:22
  • Don't use !important, it is evil. Ther isn't even a need for it. – Bjørn Moholt May 28 '19 at 13:52
  • From my experience trying this, it seems to be out of date on the latest browsers. There's better answers below. – technogeek1995 Jul 31 '19 at 16:29
104

If you don't mind using twitter bootstrap I suggest you simply use the link class.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<button type="button" class="btn btn-link">Link</button>
starball
  • 20,030
  • 7
  • 43
  • 238
BoJack Horseman
  • 4,406
  • 13
  • 38
  • 70
98

The code of the accepted answer works for most cases, but to get a button that really behaves like a link you need a bit more code. It is especially tricky to get the styling of focused buttons right on Firefox (Mozilla).

The following CSS ensures that anchors and buttons have the same CSS properties and behave the same on all common browsers:

button {
  align-items: normal;
  background-color: rgba(0,0,0,0);
  border-color: rgb(0, 0, 238);
  border-style: none;
  box-sizing: content-box;
  color: rgb(0, 0, 238); 
  cursor: pointer;
  display: inline;
  font: inherit;
  height: auto;
  padding: 0;
  perspective-origin: 0 0;
  text-align: start;
  text-decoration: underline;
  transform-origin: 0 0;
  width: auto;
  -moz-appearance: none;
  -webkit-logical-height: 1em; /* Chrome ignores auto, so we have to use this hack to set the correct height  */
  -webkit-logical-width: auto; /* Chrome ignores auto, but here for completeness */
}

/* Mozilla uses a pseudo-element to show focus on buttons, */
/* but anchors are highlighted via the focus pseudo-class. */

@supports (-moz-appearance:none) { /* Mozilla-only */
  button::-moz-focus-inner { /* reset any predefined properties */ 
    border: none;
    padding: 0;
  }
  button:focus { /* add outline to focus pseudo-class */
    outline-style: dotted;
    outline-width: 1px;
  }
}

The example above only modifies button elements to improve readability, but it can easily be extended to modify input[type="button"], input[type="submit"] and input[type="reset"] elements as well. You could also use a class, if you want to make only certain buttons look like anchors.

See this JSFiddle for a live-demo.

Please also note that this applies the default anchor-styling to buttons (e.g. blue text-color). So if you want to change the text-color or anything else of anchors & buttons, you should do this after the CSS above.


The original code (see snippet) in this answer was completely different and incomplete.

/* Obsolete code! Please use the code of the updated answer. */

input[type="button"], input[type="button"]:focus, input[type="button"]:active,  
button, button:focus, button:active {
 /* Remove all decorations to look like normal text */
 background: none;
 border: none;
 display: inline;
 font: inherit;
 margin: 0;
 padding: 0;
 outline: none;
 outline-offset: 0;
 /* Additional styles to look like a link */
 color: blue;
 cursor: pointer;
 text-decoration: underline;
}
/* Remove extra space inside buttons in Firefox */
input[type="button"]::-moz-focus-inner,
button::-moz-focus-inner {
    border: none;
    padding: 0;
}
Torben
  • 6,317
  • 1
  • 33
  • 32
6

try using the css pseudoclass :focus

input[type="button"], input[type="button"]:focus {
  /* your style goes here */
}

edit as for links and onclick events use (you shouldn’t use inline javascript eventhandlers, but for the sake of simplicity i will use them here):

<a href="some/page.php" title="perform some js action" onclick="callFunction(this.href);return false;">watch and learn</a>

with this.href you can even access the target of the link in your function. return false will just prevent browsers from following the link when clicked.

if javascript is disabled the link will work as a normal link and just load some/page.php—if you want your link to be dead when js is disabled use href="#"

knittl
  • 246,190
  • 53
  • 318
  • 364
  • i would use it but am not very sure as to what changes to apply so that the button doesn't get indented. –  Sep 02 '09 at 12:48
  • yeah. N even after giving the paddina as 0 the indentation still remains –  Sep 23 '09 at 10:11
4

You can't style buttons as links reliably throughout browsers. I've tried it, but there's always some weird padding, margin or font issues in some browser. Either live with letting the button look like a button, or use onClick and preventDefault on a link.

Jacob Rask
  • 22,804
  • 8
  • 38
  • 36
3

You can achieve this using simple css as shown in below example

button {
    overflow: visible;
    width: auto;
}
button.link {
    font-family: "Verdana" sans-serif;
    font-size: 1em;
    text-align: left;
    color: blue;
    background: none;
    margin: 0;
    padding: 0;
    border: none;
    cursor: pointer;
   
    -moz-user-select: text;
 
    /* override all your button styles here if there are any others */
}
button.link span {
    text-decoration: underline;
}
button.link:hover span,
button.link:focus span {
    color: black;
}
<button type="submit" class="link"><span>Button as Link</span></button>

enter image description here

Gauri Bhosle
  • 4,985
  • 1
  • 15
  • 19
3

I think this is very easy to do with very few lines. here is my solution

.buttonToLink{
   background: none;
   border: none;
   color: red
}

.buttonToLink:hover{
   background: none;
   text-decoration: underline;
}
<button  class="buttonToLink">A simple link button</button>
jerryurenaa
  • 3,863
  • 1
  • 27
  • 17
0
 button {
  text-decoration: underline;
  cursor: pointer;
  }

 <button onClick="javascript:window.location.href='link'">Domain</button>
Siddharth Shukla
  • 1,063
  • 15
  • 14