100

Using the markup below, the button text is underlined when hovered over. How can I get rid of that behavior?

Is there a better way to add links to a btn-group in bootstrap that avoids this behavior?

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

Tested CSS lines:

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 }

Any additional !important does not work, either (suggested by baptme).

3
  • How about css line "a:hover .btn-group { text-decoration: none }" ?
    – Lyth
    Commented Aug 6, 2012 at 13:08
  • The line is still there. I also tried a:hover .btn-group .btn without success.
    – BerndBrot
    Commented Aug 6, 2012 at 14:18
  • Try putting anchor tag inside <button> and adding a{display:block;}....it will work fine Commented Jan 3, 2017 at 8:22

14 Answers 14

156

Bootstrap 4+

This is now easy to do in Bootstrap 4+

<a href="#" class="text-decoration-none">
    <!-- That is all -->
</a>
1
  • 3
    simple but perfect! Commented Apr 8, 2021 at 8:38
105
{ 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
}
7
  • I added !important to the four css lines mentioned above, but the underline still appears.
    – BerndBrot
    Commented Aug 6, 2012 at 15:33
  • 8
    For better code readability, I would suggest .no-underline as I have no idea what a Nounderline is :P Commented Jan 2, 2015 at 10:45
  • You can do whatever you want with Nounderlines, plus they're very tasty
    – Julien
    Commented May 16, 2015 at 17:17
  • 1
    It still bugs me that this is the accepted answer... <button> inside of <a> is not legal html and .btn normally does not have an underline if used correctly. Commented Jun 19, 2016 at 22:04
  • 3
    You don't need to add !important, it's bad practice to add those everywhere. If you add more detail to the selector it will have a higher precedence than other selectors. a.no-underline { text-decoration: none; } Commented Nov 3, 2018 at 13:34
18

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), button, 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), select, textarea, video (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!

3
  • Can I ask why the downvote? I put some time into thinking about this, adding references and answering in a helpful way; if I'm incorrect I'd like to know why! (additionally, I think that the other two answers are suboptimal or at least not idiomatic... I suspect that if no-underline was actually necessary it would already be included with bootstrap) Commented Aug 15, 2015 at 23:18
  • I did a pretty big revamp of my answer in the hopes that it will correct any misinterpretation I may have had about the original question. I hope this answer satisfies everyone. Commented Aug 21, 2015 at 20:31
  • I didn't spot that, thank you! I suppose .btn-link with the .nounderline suggested in the accepted answer is probably the way to go then... Commented Feb 19, 2017 at 0:12
13

Why not just apply nav-link class?

<a href="#" class="nav-link">
1
  • there is padding around and link is display in block
    – OzzyCzech
    Commented Feb 15, 2022 at 13:41
7
a.btn {
  text-decoration: none;
}
7

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;}
2
  • This worked for me by just adding one more thing : a:hover, a:focus { text-decoration: none !important;} Thanks
    – Mazhar MIK
    Commented Mar 6, 2019 at 5:33
  • 1
    it's typically not good practice to add !important on styles. I would try to add more specific selectors before resorting to using !important. developer.mozilla.org/en-US/docs/Web/CSS/Specificity Commented Mar 6, 2019 at 5:56
5

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>
2

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.

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

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>
0
a:hover{text-decoration: underline !important}
a{text-decoration: none !important}
0

.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.

0

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

0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.