352

My buttons all have a highlight around them after I click them. This is in Chrome.

Unselected Selected

<button class="btn btn-primary btn-block">
    <span class="icon-plus"></span> Add Page
</button>

I am using Bootstrap with a theme, but I'm pretty sure that's not it: I was noticing this before on another project.

It goes away if I use an <a> tag instead of <button>. Why? If I wanted to use <button> how would I make it go away?

Sean Clark Hess
  • 15,859
  • 12
  • 52
  • 100

46 Answers46

412

I found this Q and A on another page, and overriding the button focus style worked for me. This problem may be specific to MacOS with Chrome.

.btn:focus {
  outline: none;
  box-shadow: none;
}

Note though that this has implications for accessibility and isn't advised until you have a good consistent focus state for your buttons and inputs. As per the comments below, there are users out there who cannot use mice.

Aditya Kresna Permana
  • 11,869
  • 8
  • 42
  • 48
jerimiah797
  • 4,525
  • 1
  • 14
  • 13
  • 58
    I also thought of this, but doesn't removing the outline has accessibility problems? – silvenon Oct 14 '14 at 11:23
  • 10
    I guess we have no other choice. You can also add the `.btn:active:focus` selector to remove it from the active state too. – silvenon Oct 14 '14 at 11:37
  • 73
    Removing the outline from a button is definitely bad for accessibility. There are users who can't control a mouse, and need to be able to tab through a page with the keyboard. Removing the outline makes that very difficult. It's probably better to prevent the button from receiving focus on click. – Murphy Randle Jan 27 '16 at 21:30
  • For future travelers, this code completely replaces all styles on all button states for the default button (not all buttons): `.btn-default, .btn-default:hover, .btn-default:active, .btn-default:active:focus, .btn-default:visited, .btn-default:focus { color: #dcdcdc; background-color: #232323; outline: none; }` – Geordie Mar 01 '17 at 02:03
  • 3
    if using built in bootstrap variables, one can achieve this by setting these two vars to: $btn-focus-width:0; $btn-focus-box-shadow:none; – bullettrain May 14 '19 at 14:52
  • Removing the outline is fine, and does not affect accessibility as long as you replace the outline with some other visual indicator that the button has focus. Adding the same style to :focus that you have on :hover is a good option. – Vincent Dec 03 '19 at 22:05
  • $(document).ready(function() { $(".btn").on('click', function () { this.blur(); }); }); – Steve Whitby Apr 20 '20 at 13:58
  • You could make there no outline for `.btn:clicked` because that would work for accessibility. – Elijah Mock May 20 '20 at 22:54
  • 1
    It's really no good practice to remove those states from buttons and other input elements. They are so important for accessibility. And remember two things: First accessibility is not just for "the handicapped". If you like to tab through a site you need those states. Second we will all be handicapped in our lives at some point somehow. – Juuro Aug 10 '20 at 01:37
  • in my case `box-shadow:none !important;` was needed – mojmir.novak Jan 17 '21 at 01:53
  • 4
    This really shouldn't be an acceptable answer as it completely goes against basic accessibility standards. – Ryan Mar 21 '21 at 15:32
  • Ryan, that has been discussed in great detail over the course of this answer. There's a difference between 'how to do this' vs 'should I do this'. Both aspects are covered in this answer, ad nauseam. – jerimiah797 Mar 22 '21 at 23:08
169

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.

cshotton
  • 2,784
  • 2
  • 14
  • 11
  • 13
    This displays the outline for a brief moment, which still ruins it :/ – silvenon Oct 14 '14 at 11:30
  • 3
    I don't see any flicker if I trigger off the focus event instead of the click event. – Charles May 25 '15 at 05:52
  • 3
    This worked out great! Would there be potential problems if I tried to attach a different click() event handler(i.e. the code that executes when you click said button) to the same button? – chasethesunnn Jul 22 '15 at 20:18
  • Can this be used with AngularJS as well? I tried ````, but it did not work. The outline is still there after clicking the button. – user1438038 May 10 '19 at 15:14
  • I suspect, that it's not a good idea to use both ``ng-click`` and ``onclick`` on an element with AngularJs. – user1438038 May 10 '19 at 15:15
  • 1
    It turns out that when using the mentioned solution, not every browser will remember that element was briefly focused and continue tabbing from there. For example, Chrome 81 will remember that you focused particular button, while Firefox 68/76 and Edge won't. This may result in a bad ux for some users. – Andrew Starostin May 04 '20 at 21:40
  • For some reason it didn't work for me with onclick, but worked with onfocus. – xji Dec 21 '20 at 15:04
  • focus will still be displayed if you release the click outside of the button. – Sumak Oct 29 '21 at 15:41
  • this worked on MacOS and Chrome when nothing else would. – cloudxix Nov 10 '22 at 03:05
84

Use focus-visible

Note 1: In each of the 3 options outlined below, buttons behave the same way (no focus ring on click), but selects and inputs vary slightly in their default behavior. Only Option 3 removes focus rings consistently around buttons, inputs, and selects. Please compare all approaches and make sure you understand the implications.

Note 2: Due to the cascading nature of CSS, the order of the CSS rules is important.

Note 3: There are still some accessibility concerns with any focus-visible approach. Namely, that until browsers expose a configuration to let users choose when to show visible focus rings, focus-visible should be considered worse for accessibility than using focus rings everywhere all the time, but better than the harmful :focus {outline:none} approach mentioned in other answers to this question. See "A note about accessibility" section at the bottom of this answer for more details.

OPTION 1: Use the :focus-visible pseudo-class

The :focus-visible pseudo-class can be used to remove outlines and focus rings on buttons and various elements for users that are NOT navigating via keyboard (i.e., via touch or mouse click).

Warning: As of 2021, the :focus-visible pseudo-class is ** widely supported across modern browsers but fails on fringe browsers**. If old-browser support is important, the Javascript polyfill in option 2 below is the closest approximation.

/**
 * Remove focus styles for non-keyboard focus.
 */
:focus:not(:focus-visible) {
  outline: 0;
  box-shadow: none;
}

/**
 * Cross-browser styles for explicit focus via 
 * keyboard-based (eg Tab) navigation or the
 * .focus-visible utility class.
 */
:focus,
.focus-visible:focus:not(:focus-visible) {
  outline: 0;
  box-shadow:
    0 0 0 .2rem #fff,
    0 0 0 .35rem #069;
}
<h3>Defaults:</h3>
<button>Foo</button>
<input type="button" value="Bar"/> 
<select><option>Baz</option></select>
<input type="text" placeholder="Qux"/>
<textarea placeholder="Quux" rows="1"></textarea>

<h3>Force focus on click:</h3>
<button class="focus-visible">Foo</button>
<input class="focus-visible" type="button" value="Bar"/> 
<select class="focus-visible"><option>Baz</option></select>
<input class="focus-visible" type="text" placeholder="Qux"/>
<textarea class="focus-visible" placeholder="Quux" rows="1">
</textarea>

OPTION 2: Use a .focus-visible polyfill

This solution uses a normal CSS class instead of the pseudo-class mentioned above and has wider browser support (in 2021). It requires either 1 or 2 Javascripts be added to your HTML; one for the official focus-visible polyfill and the other for older browsers that don't support classList.

Note: In Chrome, the polyfill seems to treat selects differently than the native :focus-visible pseudo-class.

/**
 * Cross-browser focus ring for explicit focus 
 * via keyboard-based (eg Tab) navigation or the
 * .focus-visible utility class.
 */
:focus {
  outline: 0;
  box-shadow:
    0 0 0 .2rem #fff,
    0 0 0 .35rem #069;
}

/**
 * Remove focus ring for non-explicit scenarios.
 */
:focus:not(.focus-visible) {
  outline: 0;
  box-shadow: none;
}
<h3>Defaults:</h3>
<button>Foo</button>
<input type="button" value="Bar"/> 
<select><option>Baz</option></select>
<input type="text" placeholder="Qux"/>
<textarea placeholder="Quux" rows="1"></textarea>

<h3>Force focus on click:</h3>
<button class="focus-visible">Foo</button>
<input class="focus-visible" type="button" value="Bar"/> 
<select class="focus-visible"><option>Baz</option></select>
<input class="focus-visible" type="text" placeholder="Qux"/>
<textarea class="focus-visible" placeholder="Quux" rows="1">
</textarea>

<!-- place this code just before the closing </html> tag -->
<script src="https://cdn.polyfill.io/v2/polyfill.js? 
features=Element.prototype.classList"></script>
<script src="https://unpkg.com/focus-visible"></script>

OPTION 3: Use a global key-navigation vs mouse-navigation state

An inverse solution to focus-visible, is to disable outlines on mousemove, and enable them on keydown -> "Tab". In this case, rather than specifying which elements shouldn't show an outline, you must specify which elements should.

document.addEventListener("mousemove", () => 
  document.body.classList.remove("focus-visible")
);

document.addEventListener("keydown", ({key}) => 
  (key === "Tab") && document.body.classList.add("focus-visible")
);
/**
 * Cross-browser focus ring for explicit focus 
 * via keyboard-based (eg Tab) navigation or the
 * .focus-visible utility class.
 */
:focus {
  outline: 0;
  box-shadow:
    0 0 0 .2rem #fff,
    0 0 0 .35rem #069;
}

/**
 * Remove focus ring for non-explicit scenarios.
 */
body:not(.focus-visible) :focus:not(.focus-visible) {
  outline: 0 !important;
  box-shadow: none !important;
}
<h3>Defaults:</h3>
<button>Foo</button>
<input type="button" value="Bar"/> 
<select><option>Baz</option></select>
<input type="text" placeholder="Qux"/>
<textarea placeholder="Quux" rows="1"></textarea>

<h3>Force focus on click:</h3>
<button class="focus-visible">Foo</button>
<input class="focus-visible" type="button" value="Bar"/> 
<select class="focus-visible"><option>Baz</option></select>
<input class="focus-visible" type="text" placeholder="Qux"/>
<textarea class="focus-visible" placeholder="Quux" rows="1">
</textarea>

A note about accessibility

Removing all focus rings a la :focus { outline: none; } or :focus { outline: 0; } is a known accessibility issue and is never recommended. Additionally, there are folks in the accessibility community who would rather you never remove a focus ring outline and instead make everything have a :focus style — either outline or box-shadow could be valid if styled appropriately.

Finally, some folks in the accessibility community believe developers should not implement :focus-visible on their websites until all browsers implement and expose a user preference which lets people pick whether all items should be focusable or not. I personally don't subscribe to this thinking, which is why I provided this solution that I feel is far better than the harmful :focus { outline:none }. I think :focus-visible is a happy medium between design concerns and accessibility concerns. As of 2022, Chrome browser has exposed a user preference to set focus visibility styles, but FireFox has not.

Resource:

Demo:

JamesWilson
  • 3,833
  • 2
  • 30
  • 40
  • But if even Chrome doesn't support it yet, it's probably not the best solution (for now) – Elijah Mock May 20 '20 at 22:56
  • 1
    What do you suggest instead? – JamesWilson May 28 '20 at 19:21
  • Hide the outline like the other answers say until this solution is more standardized – Elijah Mock May 29 '20 at 02:36
  • 1
    From my read of things, if accessibility is a concern, hiding the outline carte blanche with `:focus { outline: none }` is a worse idea than this solution. There are actually people in the accessibility community who would rather you NEVER remove outline and instead make *everything* have a :focus style (either outline or box-shadow), and do not want to support :focus-visible until browsers implement a way to let the users specify a user preference on whether all items should be focusable or not. I like to think :focus-visible is a happy medium between design concerns and a11y. – JamesWilson May 29 '20 at 12:36
  • @SephReed Thank you for OPTION 3 edit. It makes sense, and a potentially more elegant solution than the polyfill, albeit one that may require more work if you do want to show focus rings anywhere (form fields come to mind). – JamesWilson Mar 05 '21 at 01:07
  • Maybe the CSS in OPTION 3 could be simplified and generalized by adding the class to `document.activeElement` instead of to body? – JamesWilson Mar 05 '21 at 01:17
  • I think Github does this with the "Close Issue" and "Comment" buttons on their issues pages. If you click on the buttons, no focus indicator is visible. But if you navigate there using the tab key, the focus indicator around the buttons is visible. – Waruyama Jun 04 '21 at 07:30
  • @Waruyama thats correct, Github adds the `intent-mouse` class when mousedown is used and then *removes* outline/box-shadow when that class is present. This is basically the inverse of our example in OPTION 3 above. Nice find. – JamesWilson Jun 08 '21 at 23:31
  • In tailwindcss one can use `focus-visible:` to handle this. – eduludi Nov 25 '21 at 17:24
77

my understanding is that the focus is first applied following the onMouseDown event, so calling e.preventDefault() in onMouseDown may be a clean solution depending on your needs. This is certainly an accessibility friendly solution, but obviously it adjusts the behaviour of mouse clicks which may not be compatible with your web project.

I am currently using this solution (within a react-bootstrap project) and I do not receive a focus flicker or retained focus of buttons after a click, but I am still able to tab my focus and visually visualize the focus of the same buttons.

pjs
  • 2,601
  • 1
  • 20
  • 24
  • 3
    Yes. your solution works. Thanks for the tip. Here is the [Fiddle](http://jsfiddle.net/w5y5tak5/1/) for the solution. But It requires a javascript handler to be attached to every button. It would be great if we can achieve this through CSS only solution. – Galeel Bhasha Jun 28 '16 at 12:49
  • 1
    I think people expect focus to go on an element when you click on it, and this solution prevents that. I often click on an element and then move away before releasing the mouse to give it focus so that I can then tab from there on. – Michael Tontchev Jan 11 '17 at 21:00
  • 13
    For anyone browsing for a snippet, this should work and preserve onClick: `onMouseDown={e => e.preventDefault()}` – Adam K Dean Jan 19 '17 at 11:33
  • 1
    This prevents the space bar and the return key from "clicking" the button. Perhaps a more complete solution is to add this back with: `onKeyUp={(e) => {if (e.keyCode === 13 || e.keyCode === 32) handleClick()}`. – jfbloom22 May 03 '18 at 20:00
  • You should Not call e.preventDefault() inside the `onKeyUp` event. It turns out if you do that, then the button stays focused after hitting the space key or the return key and tabbing or clicking anywhere. – jfbloom22 May 03 '18 at 20:11
  • Using Angular, this was the good answer in my case. `(mousedown)="onMouseDown($event)"` in template, then `onMouseDown(event) { event.preventDefault(); }` in ts file. – Sixteen Aug 25 '20 at 20:35
  • Thank you , this worked. Since react-bootstrap is mentioned I am sharing how I used this answer to make my no highlight button: `import Button from "react-bootstrap/Button" export default function ButtonNoHighlight(props) { return ( ) } ` – Avi R Aug 06 '21 at 07:20
39

Can't believe nobody has posted this yet.

Use a label instead of a button.

<label type="button" class="btn btn-primary btn-block">
<span class="icon-plus"></span> Add Page
</label>

Fiddle

Adam McKenna
  • 583
  • 4
  • 2
34

Although it's easy to just remove outline for all focused buttons (as in user1933897's answer), but that solution is bad from the accessibility point of view (for example, see Stop Messing with the Browser's Default Focus outline)

On the other hand, it's probably impossible to convince your browser to stop styling your clicked button as focused if it thinks that it's focused after you clicked on it (I'm looking at you, Chrome on OS X).

So, what can we do? A couple options come to my mind.

1) Javascript (jQuery): $('.btn').mouseup(function() { this.blur() })

You're instructing your browser to remove the focus around any button immediately after the button is clicked. By using mouseup instead of click we're keeping the default behavior for keyboard-based interactions (mouseup doesn't get triggered by keyboard).

2) CSS: .btn:hover { outline: 0 !important }

Here you turn off outline for hovered buttons only. Obviously it's not ideal, but may be enough in some situations.

Community
  • 1
  • 1
Alexis
  • 4,317
  • 1
  • 25
  • 34
  • 4
    The number 1 is a useful tip. It will disable the focus when the button is not pressed, however it will leave it displayed while it's pressed. I've disabled focus on clicked active element with `:active:focus {outline:none;}`. – certainlyakey Sep 06 '16 at 07:34
  • The shorthand functions that bind event listeners are deprecated in jQuery, so it is better if you use `.on('mouseup', function() { ... })` instead of `.mouseup()` – Crazy Redd Aug 06 '18 at 13:33
18

This worked for me. I created a custom class which overrides the necessary CSS.

.custom-button:focus {
    outline: none !important;
    border: none !important;
    -webkit-box-shadow: none !important;
    box-shadow: none !important;
}

enter image description here

The -webkit-box-shadow will work for Chrome and safari browsers.

menaka_
  • 1,092
  • 1
  • 13
  • 23
  • works for me but I had to remove `border: none !important;` for button to maintain same dimensions – Emeka Mbah Jul 24 '18 at 17:24
  • 1
    I was looking to remove onClick focus above answers not worked but "box-shadow: none !important;" its work for me thanks @Menaka – Zaif Warm Jul 26 '19 at 09:56
13

This works for me, another solution not mentioned. Just throw it in the click event...

$(this).trigger("blur");

Or call it from another event/method...

$(".btn_name").trigger("blur");
Rob
  • 1,226
  • 3
  • 23
  • 41
  • The problem with this is that when a keyboard user "clicks" the button (by focusing it then pressing space) it loses focus. You then have to tab all the way back to wherever you were. – Tamlyn Sep 05 '19 at 14:20
10

I find a solution. when we focus, bootstrap use box-shadow, so we just disable it(not enough reputation, cannot upload image :( ).

I add

.btn:focus{
    box-shadow:none !important;
}

it works.

eran
  • 6,731
  • 6
  • 35
  • 52
nuclear
  • 3,181
  • 3
  • 19
  • 38
9

If you use the rule :focus { outline: none; } to remove outlines, the link or control will be focusable but with no indication of focus for keyboard users. Methods to remove it such with JS like onfocus="blur()" are even worse and will result in keyboard users being unable to interact with the control.

The hacks you can use, that are sort of OK, includes adding :focus { outline: none; } rules when users interacts with the mouse and remove them again if keyboard interaction is detected. Lindsay Evans has made a lib for this: https://github.com/lindsayevans/outline.js

But i would prefer to setting a class on the html or body tag. And have control in the CSS file of when to use this.

For example (inline event handlers is for demonstration purposes only):

<html>
<head>
<style>
  a:focus, button:focus {
    outline: 3px solid #000;
  }
  .no-focus a, .no-focus button {
    outline: none;
  } 
</style>
</head>
<body id="thebody" 
onmousedown="document.getElementById('thebody').classList.add('no-focus');"
onkeydown="document.getElementById('thebody').classList.remove('no-focus');">
    <p>This her is <a href="#">a link</a></p>   
    <button>Click me</button>
</body>
</html>

I did put togheter a Pen: http://codepen.io/snobojohan/pen/RWXXmp

But beware there are performance issues. This forces repaint every time the user switches between mouse and keyboard. More about Avoiding Unnecessary Paints http://www.html5rocks.com/en/tutorials/speed/unnecessary-paints/

snobojohan
  • 2,225
  • 21
  • 13
6

I've noticed the same and even though it really annoys me, I believe there is no proper way of handling this.

I would recommend against all the other solutions given because they kill the accessibility of the button completely, so now, when you tab to the button, you won't get the expected focus.

This should be avoided!

.btn:focus {
  outline: none;
}
  • I do not think this is wrong, because you still have slightly different lightning on button when focused, so even keyboard users can still recognize that. But this aint enought, because you'll still get border frame when you press and hold left mouse button – apocalypz Oct 21 '14 at 07:10
6

Late, but who knows it may help someone. The CSS would look like:

.rhighlight{
   outline: none !important;
   box-shadow:none
}

The HTML would look like:

<button type="button" class="btn btn-primary rHighlight">Text</button> 

This way you can keep btn and it's associated behaviors.

5

If the above doesn't work for you, try this:

.btn:focus {outline: none;box-shadow: none;border:2px solid transparent;}

As user1933897 pointed out, this might be specific to MacOS with Chrome.

thomaux
  • 19,133
  • 10
  • 76
  • 103
alns
  • 59
  • 1
  • 2
5

I found the same situation using a submit button in a form using a bootstrap (v4.4.1) class. The problem arose as I was building a single-page user interface using JavaScript to manipulate all the required changes to the DOM. The form data was submitted to the server via 'fetch' using a JSON string rather than a HTTP POST request. Note that usually the form's default behaviour is to reload the document, and normally this would refresh the button, however the form's default behaviour was prevented by using e.preventDefault() in the listener function for the form's submit event (it is a single-page UI so the document is never reloaded and traffic to the server is minimised to data only). Given the document was not reloaded the button appeared to stay depressed until the user clicked elsewhere in the window. This is what I had (with the problem):

<input type="submit" class="btn btn-primary">

This is what I used to solve the problem of the button remaining pressed:

<input type="submit" class="btn btn-primary" onmouseup="this.blur()">

PeterG
  • 51
  • 1
  • 1
4

For people wanting a pure css way to do that:

:focus:not(:focus-visible) { outline: none }

This could also work for link and so on, and bonus, it keeps the keyboard accessibilities. Lastly it is ignored by browsers that don’t support :focus-visible

ich_cb
  • 99
  • 8
  • 4
    This will only work on Chrome with experimental features enabled, as per https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible – Justin Kahn Jan 31 '19 at 03:09
  • `focus-visible` now has an official polyfill. My answer https://stackoverflow.com/a/60219624/413538 on this question elaborates on how to actually make sense of all this. – JamesWilson Apr 15 '20 at 16:21
  • This is now supported by Firefox as well (https://caniuse.com/css-focus-visible). I noticed Safari does not focus on click by default. I think this is the best solution regarding simplicity and accessibility. – codingFriend1 Apr 26 '21 at 19:33
4

For anyone who's using react-bootstrap and encountered this problem, here's what I did to make things work:

.btn:focus {
    /* the !important is really the key here, it overrides everything else */
    outline: none !important;
    box-shadow: none !important;
}

And things did not work before adding !important.

timthedev07
  • 454
  • 1
  • 6
  • 17
3

I mentioned this in a comment above, but it's worth listing as a separate answer for clarity. As long as you don't need to ever actually have focus on the button, you can use the focus event to remove it before it can apply any CSS effects:

$('buttonSelector').focus(function(event) {
    event.target.blur();
});

This avoids the flicker that can be seen when using the click event. This does restrict the interface, and you won't be able to tab to the button, but that isn't a problem in all applications.

Charles
  • 534
  • 1
  • 4
  • 3
  • This did half of the trick for me. I'm using React and React-Bootstrap. After click, button was staying highlighted with a glow around this. Adding e.target.blur(); removes the glow, but the button stays a different color. – pixelwiz Sep 15 '17 at 18:06
3

Style

.not-focusable:focus {
    outline: none;
    box-shadow: none;
}

Using

<button class="btn btn-primary not-focusable">My Button</button>
  • How is this solution different from the other answers? – apaul Aug 11 '15 at 20:38
  • 2
    There is only one a little difference - you can remove focus for elements that you need with specifying class "not-focusable" without overriding behaviour of all buttons, links etc. – Bogdan Kanteruk Sep 08 '15 at 09:48
3

Try this solution for remove border around the button. Add this code in css.

Try

button:focus{
outline:0px;
}

If not works then use below.

button:focus{
 outline:none !important;
 }
Rana Aalamgeer
  • 702
  • 2
  • 8
  • 22
3

It is work, I hope help you

.btn:focus, .btn:focus:active {
    outline: none;
}
3

A bit nuclear, but this is simple way that worked for me on Angular 9. Use with causon since it affects every html element.

*:focus {
  outline: 0 !important;
}
NobodySomewhere
  • 2,997
  • 1
  • 16
  • 12
2

We were suffering a similar problem and noticed that Bootstrap 3 doesn't have the problem on their tabs (in Chrome). It looks like they're using outline-style which allows the browser to decide what best to do and Chrome seems to do what you want: show the outline when focused unless you just clicked the element.

Support for outline-style is hard to pin down since the browser gets to decide what that means. Best to check in a few browsers and have a fall-back rule.

TeknoFiend
  • 191
  • 2
  • 3
2

Another possible solution is to add a class using a Javascript listener when the user clicks on the button and then remove that class on focus with another listener. This maintains accessibility (visible tabbing) while also preventing Chrome's quirky behaviour of considering a button focused when clicked.

JS:

$('button').click(function(){
    $(this).addClass('clicked');
});
$('button').focus(function(){
    $(this).removeClass('clicked');
});

CSS:

button:focus {
    outline: 1px dotted #000;
}
button.clicked {
    outline: none;
}

Full example here: https://jsfiddle.net/4bbb37fh/

2
.btn:focus:active {
  outline: none;
}

this removes the outline on click, but keeps the focus when tabbing (for a11y)

yennefer
  • 189
  • 6
  • 20
2

This works best

.btn-primary.focus, .btn-primary:focus {
-webkit-box-shadow: none!important;
box-shadow: none!important;
}
2

I found a solution simply add below line in your css code.

button:focus { outline: none }
2

React with TS solution

  const btnRef = useRef<HTMLButtonElement | null>(null);
  const handleOnMouseUp = () => {
    btnRef.current?.blur();
  };
  
  <button
    ref={btnRef}
    onClick={handleOnClick}
    onMouseUp={handleOnMouseUp}
  >
    <span className="icon-plus"></span> Add Page
  </button>
1
  .btn:focus,.btn:active, a{
        outline: none !important;
        box-shadow: none;
     }

this outline:none will work for both button and a tag

Milan Panigrahi
  • 546
  • 9
  • 11
1

You can set tabIndex="-1". It will make browser to skip this button when you TAB through focusable controls.

Other "fixes" suggested here, only remove focus outline, but still leaves buttons tabable. However, from usability point of view, you already removed glow, so your user won't know what is currently focused button, any way.

On other hand, making button non-tabable have accessibility implications.

I'm using it to remove focus outline from X button in bootstrap modal, which have duplicate "Close" button at the bottom any way, so my solution have no impact on accessibility.

zmechanic
  • 1,842
  • 21
  • 27
1

Add this in CSS:

*, ::after, ::before {
    box-sizing: border-box;
    outline: none !important;
    border: none !important;
    -webkit-box-shadow: none !important;
    box-shadow: none !important;
}
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
1

I found no solid answers that didn't either break accessibility or subvert functionality.

Perhaps combining a few will work better overall.

<h1
  onmousedown="this.style.outline='none';"
  onclick="this.blur(); runFn(this);"
  onmouseup="this.style.outline=null;"
>Hello</h1>

function runFn(thisElem) { console.log('Hello: ', thisElem); }

Keith DC
  • 661
  • 1
  • 9
  • 24
1

Although the CSS solutions offered here work,

for anyone who prefers to use the Bootstrap 4 way, as suggested in the official Bootstrap Theming guide, this should help:

in your custom.scss file (see guide above ^) where you add your variable overrides, add the following variable to remove the box-shadow for buttons:

// import bootstrap's variables & functions to override them
@import "node_modules/bootstrap/scss/functions";
@import "node_modules/bootstrap/scss/variables";
@import "node_modules/bootstrap/scss/mixins";

// override the variables you want (you can look them up in node_modules/bootstrap/scss/variables file, they're the ones that have the !default keyword at the end)
$btn-focus-box-shadow: none;

// option A: include all of Bootstrap (see the above guide for other options)
@import "node_modules/bootstrap/scss/bootstrap";

this works for me.

please note that there are many variables in bootstrap's variables file for box-shadow, for other controls as well, so it might require some more research on your side if you want to use them and/or this specific variable doesn't work for you.

1

If you want to accomplish the removal of the outline on mousedown but not on keyboard tab, and you're using VueJS, here's the solution:

<button @mousedown="$event.preventDefault()">No Online On Click</button>

This basically prevents it from receiving focus on click, but any other way of receiving focus stays active.

TetraDev
  • 16,074
  • 6
  • 60
  • 61
0

I just had this same problem on MacOS and Chrome while using a button to trigger a "transition" event. If anyone reading this is already using an event listener, you can solve it by calling .blur() after your actions.

Example:

 nextQuestionButtonEl.click(function(){
    if (isQuestionAnswered()) {
        currentQuestion++;
        changeQuestion();
    } else {
        toggleNotification("invalidForm");
    }
    this.blur();
});

Though if you're not using an event listener already, adding one just to solve this might add unnecessary overhead and a styling solution like previous answers provide is better.

0

If you're using a webkit browser (and potentially a browser compatible with webkit vendor prefixing), that outline belongs to the button's -webkit-focus-ring pseudoclass. Simply set it's outline to none:

*:-webkit-focus-ring {
  outline: none;
}

Chrome is such a webkit browser, and this effect happens on Linux too (not just a macOS thing, although some Chrome styles are macOS only)

Nate Symer
  • 2,185
  • 1
  • 20
  • 27
0

I was having the same problem using <a> acting as button and I discovered I was missing a workaround by adding attr type="button" makes it behave normally for me at least.

<a type="button" class="btn btn-primary">Release Me!</a>

spcsLrg
  • 340
  • 2
  • 11
0

directly in html tag (in a scenario where you might want to leave the bootstrap theme in place elsewhere in your design)..

examples to try..

<button style="border: transparent;">
<button style="border: 1px solid black;">

..ect,. depending on the desired effect.

None
  • 572
  • 1
  • 5
  • 12
0

For sass users, Bootstrap 4 should be manipulated by overriding variables.

If you want to disable the box-shadow on focus around buttons:

$btn-focus-box-shadow: none;

You can also disable the box-shadow on focus around inputs with:

$input-focus-box-shadow: none;

Or both with one variable:

$input-btn-focus-box-shadow: none;
TimothePearce
  • 1,128
  • 1
  • 11
  • 22
0

Here are two possible solutions.

1.) button type="button" className="btn-cart"onClick{(event)=>this.blur(event)}

2.) button type="button" className="btn-cart" onclick={this.blur}

Both of the solutions will remove the highlighted part around the button i.e -> blur() has its own specification in it of removing highlighted part around.

Eric Jin
  • 3,836
  • 4
  • 19
  • 45
Suren
  • 1
0

If the button:focus {box-shadow: none} didn't work out for you there might be some library adding the border as in my case with the pseudo-selector ::after.

So I removed the border showing up on focus with the following solution:

button:focus::after {
    outline: none;
    box-shadow: none;
}
Kia Kaha
  • 1,565
  • 1
  • 17
  • 39
-1

If you dont want the outline for button with all the status, you can override the css with below code

.btn.active.focus, .btn.active:focus, 
.btn.focus, .btn:active.focus, 
.btn:active:focus, .btn:focus{
  outline: none;
}
Sabrina Luo
  • 3,810
  • 4
  • 16
  • 35
-1

Add this in script

$(document).ready(function() {
    $('#addBtn').focus(function() {
        this.blur();
    });
});
Renish Gotecha
  • 2,232
  • 22
  • 21
-2

For AngularJS developers I use from the solution:

var element = $window.document.getElementById("export-button");
    if (element){
        element.blur();
    }

Remember to inject $window.

Tomasz Waszczyk
  • 2,680
  • 5
  • 35
  • 73
-3

Just try one of these:

outline:0; or outline:0px; or outline: none;

and $(this).trigger("blur");

Mostafa
  • 815
  • 1
  • 13
  • 23
-3

You can use focus event of button. This worked in case of angular

<button (focus)=false >Click</button
ritesh
  • 177
  • 2
  • 4
-3

I am curious as to why someone would want to remove the outline. Like many have mentioned, there are accessibility concerns if you remove the outline. Users using Assistive Technologies such as screen readers need to know where the focus is when visiting websites/applications. I know the blue color around the button can "mess up" designs, however, you can change the color with CSS using:

outline-color: your color hex here;

Another way you can edit this is by adding a boarder on focus.

dacey
  • 145
  • 1
  • 4