How do I remove blue “selected” outline on buttons?

前端 未结 4 1863
迷失自我
迷失自我 2020-11-30 10:33

I have some buttons using

4条回答
  •  北海茫月
    2020-11-30 11:29

    This is an issue in the Chrome family and has been there forever.

    A bug has been raised https://bugs.chromium.org/p/chromium/issues/detail?id=904208

    It can be shown here: https://codepen.io/anon/pen/Jedvwj as soon as you add a border to anything button-like (say role="button" has been added to a tag for example) Chrome messes up and sets the focus state when you click with your mouse. You should see that outline only on keyboard tab-press.

    I highly recommend using this fix: https://github.com/wicg/focus-visible.

    Just do the following

    npm install --save focus-visible
    

    Add the script to your html:

    
    

    or import into your main entry file if using webpack or something similar:

    import 'focus-visible/dist/focus-visible.min';
    

    then put this in your css file:

    // hide the focus indicator if element receives focus via mouse, but show on keyboard focus (on tab).
    .js-focus-visible :focus:not(.focus-visible) {
      outline: none;
    }
    
    // Define a strong focus indicator for keyboard focus.
    // If you skip this then the browser's default focus indicator will display instead
    // ideally use outline property for those users using windows high contrast mode
    .js-focus-visible .focus-visible {
      outline: magenta auto 5px;
    }
    

    You can just set:

    button:focus {outline:0;}
    

    but if you have a large number of users, you're disadvantaging those who cannot use mice or those who just want to use their keyboard for speed.

提交回复
热议问题