How to remove focus around buttons on click

后端 未结 30 914
遇见更好的自我
遇见更好的自我 2020-12-02 04:24

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

\"Unselected\"

相关标签:
30条回答
  • 2020-12-02 04:48

    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.

    0 讨论(0)
  • 2020-12-02 04:49

    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.

    0 讨论(0)
  • 2020-12-02 04:50

    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

    0 讨论(0)
  • 2020-12-02 04:52

    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

    0 讨论(0)
  • 2020-12-02 04:53

    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.

    0 讨论(0)
  • 2020-12-02 04:53

    This works best

    .btn-primary.focus, .btn-primary:focus {
    -webkit-box-shadow: none!important;
    box-shadow: none!important;
    }
    
    0 讨论(0)
提交回复
热议问题