Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop buttons from staying depressed with Bootstrap 3

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/

like image 524
Robert Byers Avatar asked May 03 '14 11:05

Robert Byers


People also ask

How do you keep active CSS style after clicking a button?

:active denotes the interaction state (so for a button will be applied during press), :focus may be a better choice here. However, the styling will be lost once another element gains focus.

How do I block a button in Bootstrap?

Use the . disabled class in Bootstrap to disable a button.

Can you customize Bootstrap buttons?

Because of its framework, Bootstrap's buttons are already styled when they're added to the site. However, if you want to create your own custom button style, you can do that in Bootstrap's CSS file. If you haven't yet installed Bootstrap on your server, see our Setting Up Bootstrap on Your Server article.


1 Answers

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

like image 112
abl Avatar answered Sep 28 '22 03:09

abl