如何使按钮看起来像一个链接?

260 css button hyperlink

我需要使用CSS制作一个看起来像链接的按钮.更改已完成,但是当我单击它时,它显示为按下按钮.知道如何删除它,以便即使点击按钮也可以作为链接使用吗?

ada*_*ign 324

HTML

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;
}
Run Code Online (Sandbox Code Playgroud)

CSS

<button> your button that looks like a link</button>
Run Code Online (Sandbox Code Playgroud)

参见演示(在JSfiddle上)

  • 我对小提琴的微小变化 - 移动了文本装饰:强调规则以按钮操作:悬停.请参阅我的[分叉小提琴](http://jsfiddle.net/zYCN9/1/). (22认同)
  • 如果您不希望所有按钮都被设置为链接,请使用类似`button.link {... styles ...}`然后`<button class ="link">您的按钮</ button>来设置样式的范围. `.这也避免使用`!important`,这总是一个好主意. (10认同)
  • 请不要删除大纲,因为这会使您的按钮无法访问:http://www.outlinenone.com (4认同)
  • 代替`border-bottom`,使用`text-decoration:underline`。 (4认同)
  • 记得要照顾`outline`.有些浏览器会添加按钮.你可以设置`outline-color:transparent`. (3认同)
  • 不要忘记“字体:继承”;大多数浏览器使用自己的按钮字体。 (3认同)

Tor*_*ben 85

接受答案的代码适用于大多数情况,但要获得一个真正像链接一样的按钮,您需要更多代码.在Firefox(Mozilla)上获得专注按钮的样式尤其棘手.

以下CSS确保锚点和按钮具有相同的CSS属性,并且在所有常见浏览器上的行为相同:

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

上面的示例仅修改button元素以提高可读性,但也可以轻松扩展为修改input[type="button"], input[type="submit"]input[type="reset"]元素.如果你只想使某些按钮看起来像锚,你也可以使用一个类.

请参阅此JSFiddle以获取实时演示.

另请注意,这会将默认锚样式应用于按钮(例如蓝色文本颜色).因此,如果您想要更改文本颜色或锚点和按钮的任何其他内容,您应该上面的CSS 之后执行此操作.


此答案中的原始代码(请参阅代码段)完全不同且不完整.

/* 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;
}
Run Code Online (Sandbox Code Playgroud)


BoJ*_*man 68

如果你不介意使用twitter bootstrap我建议你只使用链接类.

<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>
Run Code Online (Sandbox Code Playgroud)

我希望这有助于某人:)祝你有愉快的一天!

  • 这很好用,但留下了一些填充而不是真正的<a>链接.只需添加一种填充样式:0!重要且它是金色的.谢谢! (6认同)

jer*_*naa 6

我认为这很容易用很少的线来完成。这是我的解决方案

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

.buttonToLink:hover{
   background: none;
   text-decoration: underline;
}
Run Code Online (Sandbox Code Playgroud)
<button  class="buttonToLink">A simple link button</button>
Run Code Online (Sandbox Code Playgroud)


kni*_*ttl 5

尝试使用CSS伪类 :focus

input[type="button"], input[type="button"]:focus {
  /* your style goes here */
}
Run Code Online (Sandbox Code Playgroud)

编辑为链接和onclick事件使用(您不应该使用内联javascript事件处理程序,但是为了简单起见,我将在这里使用它们):

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

使用this.href,您甚至可以访问函数中链接的目标。return false只会阻止浏览器在单击时跟随链接。

如果禁用JavaScript的链接将作为一个正常的链接,只加载some/page.php-如果你希望你的链接是死的时候JS是被禁止的使用href="#"


Jac*_*ask 5

您无法在整个浏览器中可靠地将按钮样式设置为链接。我试过了,但在某些浏览器中总是有一些奇怪的填充、边距或字体问题。要么让按钮看起来像一个按钮,要么在链接上使用 onClick 和 preventDefault 。

  • 当浏览器使用本机小部件时,情况确实如此。任何甚至远程最近的浏览器仍然如此吗? (2认同)
  • 现在是 2017 年,我的 FireFox 仍然使用本机小部件。 (2认同)