How to stop buttons from staying depressed with Bootstrap 3

HtmlCssTwitter Bootstrap-3Html Input

Html Problem Overview


How do you make a button in Bootstrap 3 undepress automatically after being clicked?

To replicate my problem, make a page with some buttons, give them appropriate bootstrap classes (and include bootstrap):

<input id="one" type="button" class="btn btn-default" value="one">
<input id="two" type="button" class="btn btn-primary" value="two">

Load the page and click on one of the buttons. It becomes depressed and highlighted until you click somewhere else on the page (using FF29 and chrome35beta).

Inspecting the input element while clicked and unclicked doesn't show any additional classes being attached and removed from it.

Here's an example of Bootstrap buttons staying depressed: http://jsfiddle.net/52VtD/5166/

Html Solutions


Solution 1 - Html

In your example, the buttons do not stay depressed. They stay focused. If you want to see the difference, do the following:

  1. Click and hold on a button.
  2. 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.

Example

This example uses jQuery but you can achieve the same effect with vanilla JavaScript.

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

Fiddle

Solution 2 - Html

Or you can just use an anchor tag which can be styled exactly the same, but since it's not a form element it doesn't retain focus:

<a href="#" role="button" class="btn btn-default">one</a>.

See the Anchor element section here: http://getbootstrap.com/css/#buttons

Solution 3 - Html

The button remains focused. To remove this efficiently you can add this query code to your project.

$(document).ready(function () {
  $(".btn").click(function(event) {
    // Removes focus of the button.
    $(this).blur();
  });
});

This also works for anchor links

$(document).ready(function () {
  $(".navbar-nav li a").click(function(event) {
    // Removes focus of the anchor link.
    $(this).blur();
  });
});

Solution 4 - Html

My preference:

<button onmousedown="event.preventDefault()" class="btn">Calculate</button>

Solution 5 - Html

Or angular way:

function blurElemDirective() {
  return {
    restrict: 'E',
    link: function (scope, element, attrs) {
      element.bind('click', function () {
        element.blur();
      });
    }
  };
}

app.directive('button', blurElemDirective);
app.directive('_MORE_ELEMENT_', blurElemDirective);

Replace MORE_ELEMENT with your others elements.

Or attribute way:

function blurElemDirective() {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      element.bind('click', function () {
        element.blur();
      });
    }
  };
}

app.directive('blurMe', blurElemDirective);

Then add the attribute to your html element: blur-me

<button blur-me></button>

Solution 6 - Html

It's the browser's focus since it's a form element (input). You can easily remove the focusing with a little css

input:focus {
    outline: 0;
}

Here's the fiddle with your example: http://jsfiddle.net/52VtD/5167/

EDIT

Ah, I just saw now that the colour of the button itself changes too. Bootstrap changes the button of e.g. your btn-default button with this css:

.btn-default:focus {
    color: #333;
    background-color: #ebebeb;
    border-color: #adadad;
}

If you don't want this behaviour, just overwrite it with your css.

Solution 7 - Html

This has to do with the :active and :focus element states. You need to modify the styles for those states for these buttons. For example, for the default button:

.btn-default:focus, .btn-default:active, .btn-default.active, .open .dropdown-toggle.btn-default {
    color: #333;
    background-color: #ccc;
    border-color: #fff;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobert ByersView Question on Stackoverflow
Solution 1 - HtmlablView Answer on Stackoverflow
Solution 2 - Htmljme11View Answer on Stackoverflow
Solution 3 - Htmlchris31389View Answer on Stackoverflow
Solution 4 - HtmlDrazen BjelovukView Answer on Stackoverflow
Solution 5 - HtmlnaorzView Answer on Stackoverflow
Solution 6 - HtmlAnthony WeberView Answer on Stackoverflow
Solution 7 - HtmlrneviusView Answer on Stackoverflow