我需要使用CSS制作一个看起来像链接的按钮.更改已完成,但是当我单击它时,它显示为按下按钮.知道如何删除它,以便即使点击按钮也可以作为链接使用吗?
ada*_*ign 324
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)
<button> your button that looks like a link</button>
Run Code Online (Sandbox Code Playgroud)
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)
我希望这有助于某人:)祝你有愉快的一天!
我认为这很容易用很少的线来完成。这是我的解决方案
.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)
尝试使用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="#"
您无法在整个浏览器中可靠地将按钮样式设置为链接。我试过了,但在某些浏览器中总是有一些奇怪的填充、边距或字体问题。要么让按钮看起来像一个按钮,要么在链接上使用 onClick 和 preventDefault 。