【问题标题】:Best way to Convert HTML Button to a Link将 HTML 按钮转换为链接的最佳方法
【发布时间】:2015-05-24 01:23:29
【问题描述】:

我有以下代码,其中有一个带有箭头黑客的按钮。唯一的问题是我想让这个显示状态栏上的链接。一个按钮没有实现。如何将其转换为 href 链接并且仍然能够保留该箭头?这是我的代码:

JSFIDDLE

HTML:

<button type="button" class="btn-lg btn-default my-button my-button-active">My Button</button>

CSS:

.my-button {
    color: #fff ;
    background-color: #444346;
    font-size:15px;
    padding: 20px 0px;
    border:none;
    margin-right:20px;
    position: relative;
    outline:0;
    width:31%;
}


.my-button-active:after {
    content:'';
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -10px;
    width: 0;
    height: 0;
    border-top: solid 7px #444346;
    border-left: solid 7px transparent;
    border-right: solid 7px transparent;
}

.my-button-active:hover:after {
    content:'';
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -10px;
    width: 0;
    height: 0;
    border-top: solid 7px #e6e6e6;
    border-left: solid 7px transparent;
    border-right: solid 7px transparent;
}

【问题讨论】:

标签: html css twitter-bootstrap css-shapes


【解决方案1】:

这很简单。只需将按钮转换为 a 并将 display: block;display: inline-block; 添加到 .my-button 类。

小提琴链接:http://jsfiddle.net/samirkumardas/60n0ruyj/2/

【讨论】:

    【解决方案2】:

    将您的 html 更改为锚标记:

    <a class="btn-lg btn-default my-button my-button-active" href="">My Button that links to some other page</a>
    

    将您的 .my-button 选择器替换为:

    .my-button {
        display: block;
        position: relative;
        color: #fff;
        background-color: #444346;
        font-size:15px;
        padding: 20px 0;
        text-align: center;
        border: none;
        margin-right: 20px;
        outline: 0;
        width: 31%;
    }
    

    【讨论】:

      【解决方案3】:

      只需将您的按钮更改为锚点并调整您的 CSS。

      div {
        margin: 30px;
        
      }
      
      a.my-button {
        color: #fff ;
        background-color: #444346;
        font-size:15px;
        padding: 20px 0px;
        border:none;
        margin-right:20px;
        position: relative;
        outline:0;
        width:31%;
        display: inline-block;
        text-align: center;
      }
      
      .my-button:hover {
        text-decoration: none;
      }
      
      
      .my-button-active:after {
        content:'';
        position: absolute;
        top: 100%;
        left: 50%;
        margin-left: -10px;
        width: 0;
        height: 0;
        border-top: solid 7px #444346;
        border-left: solid 7px transparent;
        border-right: solid 7px transparent;
      }
      
      .my-button-active:hover:after {
        content:'';
        position: absolute;
        top: 100%;
        left: 50%;
        margin-left: -10px;
        width: 0;
        height: 0;
        border-top: solid 7px #e6e6e6;
        border-left: solid 7px transparent;
        border-right: solid 7px transparent;
      
      }
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
      <div>
        <a href="#" type="button" class="btn-lg btn-default my-button my-button-active">My Button that links to some other page</a>
      </div>

      【讨论】:

      • 这显示与原来的不同,因为您忘记在 css 中添加一些东西。不是一个完整的解决方案。
      • @Sean Stopnik 可以在这里帮助我并告诉我缺少什么,因为对我来说它看起来完全一样,在 chrome 和 firefox 中查看。
      • 查看我对这个问题的回答。
      • @SeanStopnik 但是按钮的默认显示行为不是块,它是内联块。
      • 问题是将按钮标签替换为锚标签,并使其显示相同。锚标记是一个内联元素。我们必须将 display: 块添加到锚标记以使其定位。然后我们需要添加 text-align: center 让它显示和原来的一样。
      【解决方案4】:

      &lt;button&gt; 标签更改为&lt;a&gt; 标签后,关键是要对其应用display: block;,因为&lt;a&gt; 标签主要是内联元素,不能有填充和边距。

      【讨论】:

      • @Paulie_D 我的意思是,将&lt;button&gt; 标签更改为&lt;a&gt; 标签并在其上使用该CSS。我认为 OP 的意图已经是更改标签。
      • 那么您应该在答案中做出该声明。其他用户不会知道您做出了这种假设,您也无法了解 OP 的想法。
      • @Paulie_D 好吧,OP 确实询问过将其更改为 &lt;a&gt; 标记。我会编辑我的答案
      猜你喜欢
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 2020-11-16
      • 2015-05-13
      相关资源
      最近更新 更多