Remove blue border from css custom-styled button in Chrome

前端 未结 22 946
闹比i
闹比i 2020-11-22 17:10

I\'m working on a web page, and I want custom-styled

相关标签:
22条回答
  • 2020-11-22 17:32

    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.

    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:

    <script src="/node_modules/focus-visible/dist/focus-visible.min.js"></script>
    

    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.

    0 讨论(0)
  • 2020-11-22 17:33

    Until all modern browsers will start support css-selector :focus-visible,
    the simplest and possibly best way to save accessibility is to remove this tricky focus only for mouse users and to save it for keyboard users:

    1.Use this tiny polyfill (about 10kb): https://github.com/WICG/focus-visible
    2.Add next code somewhere in your css:

    .js-focus-visible :focus:not(.focus-visible) {
      outline: none;
    }
    

    Browser-support of css4-selector :focus-visible right now very weak:
    https://caniuse.com/#search=focus-visible

    0 讨论(0)
  • Wait! There's a reason for that ugly outline!

    Before removing that ugly blue outline, you may want to take accessibility into consideration. By default, that blue outline is placed on focusable elements. This is so that users with accessibility issues are able to focus that button by tabbing to it. Some users do not have the motor skills to use a mouse and must use only the keyboard (or some other input device) for computer interaction. When you remove the blue outline, there is no longer a visual indicator on what element is focused. If you are going to remove the blue outline, you should replace it with another type of visual indication that the button is focused.

    Possible Solution: Darken Buttons when focused

    For the examples below, Chrome's blue outline was first removed by using button:focus { outline:0 !important; }

    Here are your basic Bootstrap buttons as they appear normally: Bootstrap Buttons in Normal State

    Here are the buttons when they receive focus: Bootstrap Buttons in Focused State

    Here the buttons when they are pressed: enter image description here

    As you can see, the buttons are a little darker when they receive focus. Personally, I would recommend making the focused buttons even darker so that there is a very noticeable difference between the focused state and the normal state of the button.

    It's not just for disabled users

    Making your site more accessible is something that is often overlooked but can help create a more productive experience in your website. There are many normal users that use keyboard commands to navigate through websites in order to keep hands on the keyboard.

    0 讨论(0)
  • 2020-11-22 17:36

    This is what worked for me:

    button:focus {
        box-shadow:none;
    }
    
    0 讨论(0)
提交回复
热议问题