Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Bootstrap group buttons to stay focused

How do I prevent buttons from staying focused after being clicked?

HTML

<div class="container">
    <div class="btn-group" role="group" aria-label="...">
        <button type="button" class="btn btn-default">Left</button>
        <button type="button" class="btn btn-default">Middle</button>
        <button type="button" class="btn btn-default">Right</button>
    </div>
</div>

CSS

@import url('http://getbootstrap.com/dist/css/bootstrap.css');

 body {
    margin: 10px;
}

Please see what I mean in this Fiddle.

I know that I could get round this by applying the styling to anchors <a> instead of buttons, but I can't do that because I have to keep the following original asp.net controls.

<asp:Button ID="SearchResultsPDF"  runat="server" Text="PDF Report" CssClass="btn btn-default" OnClick="PDFReport_Click" />

<asp:Button id="SearchResultsCSV" runat="server" Text="CSV Report" CssClass="btn btn-default" onClick="CSVReport_Click"></asp:Button>

Also I have seen this solution with jQuery, which works fine, but it seems too specific.

jQuery

$(".btn").mouseup(function(){
    $(this).blur();
})

I would like to know if there is a CSS only solution and I appreciate some directions on how to do that. Thanks.

like image 272
U r s u s Avatar asked Dec 09 '14 15:12

U r s u s


People also ask

What is a button group in Bootstrap 5?

Responsive button group built with the latest Bootstrap 5. Button group wraps a series of buttons together into a single line (navbar} or stack in a vertical column. Many examples and simple tutorials. Group a series of buttons together on a single line with the button group.

How do I Turn Off the focus of my buttons?

Click and hold on a button. Release. You will see that when you release the mouse the button's appearance changes slightly, because it is no longer pressed. If you do not want your buttons to stay focused after being released you can instruct the browser to take the focus out of them whenever you release the mouse.

How to span the entire width of the screen in Bootstrap?

To span the entire width of the screen, use the .btn-group-justified class: Note: For <button> elements, you must wrap each button in a .btn-group class: Add a Bootstrap class to group the buttons together.

What is the use of button group?

Button group wraps a series of buttons together into a single line (navbar} or stack in a vertical column. Many examples and simple tutorials. Group a series of buttons together on a single line with the button group.


2 Answers

If you don't want to use that jquery snippet, and you can't add a class. The only option I can think of is by overriding the styling for :focus.

.btn-default:focus {
    background-color: #fff;
    border-color: #ccc;
}

Ideally you would want to use your own class, but that depends on if you are able to add a class to the asp:Button.

like image 149
Jonnerz Avatar answered Sep 20 '22 14:09

Jonnerz


You want something like:

<button class="btn btn-primary btn-block" onclick="this.blur();">...

The .blur() method correctly removes the focus highlighting and doesn't mess up Bootstraps's styles.

like image 42
ashish malgawa Avatar answered Sep 22 '22 14:09

ashish malgawa