Html 如何使 Bootstrap 中的 <button> 看起来像导航标签中的普通链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19981949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 15:19:36  来源:igfitidea点击:

How to make a <button> in Bootstrap look like a normal link in nav-tabs?

htmlcsstwitter-bootstrap

提问by meustrus

I'm working in (formerly Twitter) Bootstrap 2 and I wanted to style buttons as though they were normal links. Not just any normal links, though; these are going in a <ul class="nav nav-tabs nav-stacked">container. The markup will end up like this:

我在(以前的 Twitter)Bootstrap 2 中工作,我想设计按钮的样式,就好像它们是普通链接一样。但是,不仅仅是任何正常的链接;这些都装在一个<ul class="nav nav-tabs nav-stacked">容器里。标记最终会是这样的:

<form action="..." method="post">
  <div class="row-fluid">
    <!-- Navigation for the form -->
    <div class="span3">
      <ul class="nav nav-tabs nav-stacked">
        <li><button type="submit" name="op" value="Link 1">Link 1</button></li>
        <li><button type="submit" name="op" value="Link 2">Link 2</button></li>
        <!-- ... -->
      </ul>
    </div>
    <!-- The actual form -->
    <div class="span9">
      <!-- ... -->
    </div>
  </div>
</form>

Does Bootstrap have any way to make these <button>s look like they were actually <a>s?

Bootstrap 有没有办法让这些<button>s 看起来像它们实际上是<a>s?

回答by SW4

As noted in the official documentation, simply apply the class(es) btn btn-link:

正如官方文档中所述,只需应用类btn btn-link

<!-- Deemphasize a button by making it look like a link while maintaining button behavior -->
<button type="button" class="btn btn-link">Link</button>

For example, with the code you have provided:

例如,使用您提供的代码:

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />


<form action="..." method="post">
  <div class="row-fluid">
    <!-- Navigation for the form -->
    <div class="span3">
      <ul class="nav nav-tabs nav-stacked">
        <li>
          <button class="btn btn-link" role="link" type="submit" name="op" value="Link 1">Link 1</button>
        </li>
        <li>
          <button class="btn btn-link" role="link" type="submit" name="op" value="Link 2">Link 2</button>
        </li>
        <!-- ... -->
      </ul>
    </div>
    <!-- The actual form -->
    <div class="span9">
      <!-- ... -->
    </div>
  </div>
</form>

回答by user3699060

Just make regular link look like button :)

只需让常规链接看起来像按钮:)

<a href="#" role="button" class="btn btn-success btn-large">Click here!</a>

"role" inside a href code makes it look like button, ofc you can add more variables such as class.

href 代码中的“角色”使其看起来像按钮,ofc 您可以添加更多变量,例如类。

回答by Rubyist

Just add remove_button_css as class to your button tag. You can verify the code for Link 1

只需将 remove_button_css 作为类添加到您的按钮标记中。您可以验证链接 1 的代码

.remove_button_css { 
  outline: none;
  padding: 5px; 
  border: 0px; 
  box-sizing: none; 
  background-color: transparent; 
}

enter image description here

在此处输入图片说明

Extra Styles Edit

额外样式编辑

Add color: #337ab7;and :hoverand :focusto match OOTB (bootstrap3)

添加color: #337ab7;:hover:focus以匹配 OOTB (bootstrap3)

.remove_button_css:focus,
.remove_button_css:hover {
    color: #23527c;
    text-decoration: underline;
}

回答by mcNux

In bootstrap 3, this works well for me:

在引导程序 3 中,这对我很有效:

.btn-link.btn-anchor {
    outline: none !important;
    padding: 0;
    border: 0;
    vertical-align: baseline;
}

Used like:

像这样使用:

<button type="button" class="btn-link btn-anchor">My Button</button>

Demo

演示

回答by oneday

Just get rid of the background color, borders and add hover effects. Here's a fiddle: http://jsfiddle.net/yPU29/

只需去掉背景颜色、边框并添加悬停效果即可。这是一个小提琴:http: //jsfiddle.net/yPU29/

<form action="..." method="post">
<div class="row-fluid">
<!-- Navigation for the form -->
<div class="span3">
  <ul class="nav nav-tabs nav-stacked">
    <li><button type="submit" name="op" value="Link 1" class="button-link">Link 1</button></li>
    <li><button type="submit" name="op" value="Link 2" class="button-link">Link 2</button></li>
    <!-- ... -->
  </ul>
</div>
<!-- The actual form -->
<div class="span9">
  <!-- ... -->
</div>
</div>
</form>

CSS:

CSS:

.button-link {
background-color: transparent;
border: none;
}

.button-link:hover {
color: blue;
text-decoration: underline;
}

回答by Alexey Mezenin

I've tried all examples, posted here, but they do not work without extra CSS. Try this:

我已经尝试了所有的例子,张贴在这里,但如果没有额外的 CSS,它们就无法工作。尝试这个:

<a href="http://www.google.com"><button type="button" class="btn btn-success">Google</button></a>

Works perfectly without any extra CSS.

无需任何额外 CSS 即可完美运行。