Twitter-bootstrap: 当鼠标悬停在 < a >-tag 中的 btn-group 上时,如何去除带下划线的按钮文本?

使用下面的标记,当鼠标悬停在按钮上时,按钮文本会有下划线。我怎样才能消除这种行为呢?

有没有更好的方法在引导程序中添加指向 btn-group 的链接来避免这种行为?

<a href="#">
<div class="btn-group">
<button class="btn">Text</button>
<button class="btn">Text</button>
</div>
</a>

经过测试的 CSS 行:

a:hover .btn-group { text-decoration: none }
a .btn-group:hover { text-decoration: none }
a:hover .btn-group .btn { text-decoration: none }
a .btn-group .btn:hover { text-decoration: none }

任何额外的! 重要的不工作,也(建议洗礼)。

194414 次浏览
{ text-decoration: none !important}

EDIT 1:

For you example only a{text-decoration: none} will works

You can use a class not to interfere with the default behaviour of <a> tags.

<a href="#" class="nounderline">
<div class="btn-group">
<button class="btn">Text</button>
<button class="btn">Text</button>
</div>
</a>

CSS:

.nounderline {
text-decoration: none !important
}
a.btn {
text-decoration: none;
}

Buttons with the btn class do not have underlines unless you are doing something wrong: In this case nesting <button> inside of <a>.

Something that I think you might be trying to do, is to create a bootstrap button without any decorations (underline, outline, button borders, etc). In other words, if your anchor is not a hyperlink, it is semantically a button.

Bootstrap's existing btn class appears to be the correct way to remove underline decorations from anchor buttons:

Use the button classes on an <a>, <button>, or <input> element

EDIT: Hitesh points out that btn will give you a shadow on :active. Thanks! I have modified my first example to use btn-link and incorporated the accepted answer's text-decoration: none to avoid this problem. Note that nesting a button inside of an anchor remains malformed html, a point which isn't addressed by any of the other answers.

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet"/>
<div>
<!-- use anchors for borderless buttons -->
<a href="#" class="btn btn-link" style="text-decoration: none">Text</a>
<a href="#" class="btn btn-link" style="text-decoration: none">Text</a>
</div>

Alternatively, for a regular button group using anchors:

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet"/>
<div class="btn-group">
<!-- use anchors for borderless buttons -->
<a href="#" class="btn btn-default">Text</a>
<a href="#" class="btn btn-default">Text</a>
</div>

In other words, it should not be necessary to introduce your own nounderline class and/or custom styling as the other answers suggest. However, be aware of certain subtleties.


According to the HTML5 spec, <a><button>..</button></a> is illegal:

Content model: Transparent, but there must be no interactive content descendant.

...

Interactive content is content that is specifically intended for user interaction. a, audio (if the controls attribute is present), audio3, embed, iframe, img (if the usemap attribute is present), input (if the type attribute is not in the hidden state), keygen, label, object (if the usemap attribute is present), audio0, audio1, audio2 (if the controls attribute is present)


P.S. If, conversely, you wanted a button that has underline decorations, you might have used btn-link. However, that should be rare - this is almost always just an anchor instead of a button!

If you are using Less or Sass with your project, you can define the link-hover-decoration variable (which is underline by default) and you're all set.

a:hover{text-decoration: underline !important}
a{text-decoration: none !important}

The problem is that you're targeting the button, but it's the A Tag that causes the text-decoration: underline. So if you target the A tag then it should work.

a:hover, a:focus { text-decoration: none;}
a:hover, /* OPTIONAL*/
a:visited,
a:focus
{text-decoration: none !important;}

.btn is the best way, in modern website, it's not good while using anchor element without href so make the anchor tag to button is better.

Why not just apply nav-link class?

<a href="#" class="nav-link">

Bootstrap 4+

This is now easy to do in Bootstrap 4+

<a href="#" class="text-decoration-none">
<!-- That is all -->
</a>

Easy way to remove the underline from the anchor tag if you use bootstrap. for my case, I used to like this;

 <a href="#first1" class=" nav-link">
<button type="button" class="btn btn-warning btn-lg btn-block">
Reserve Table
</button>
</a>

just use bootstrap class "btn" in the link it will remove underline on hover

add the Bootstrap class text-decoration-none to your anchor tags

<a href="#" class="text-decoration-none">
<div class="btn-group">
<button class="btn">Text</button>
<button class="btn">Text</button>
</div>
</a>

Add this css code to your css file:

a.btn { text-decoration: none !important; }

Use the a tag:

<a href="account/login" class="btn btn-outline-success mx-2">Login</a>

enter image description here