How to remove the border highlight on an input text element

前端 未结 18 1043
庸人自扰
庸人自扰 2020-11-22 05:49

When an HTML element is \'focused\' (currently selected/tabbed into), many browsers (at least Safari and Chrome) will put a blue border around it.

For the layout I a

18条回答
  •  花落未央
    2020-11-22 06:44

    Update 2020 - :focus-visible

    Good news for accessibility - Chrome & Firefox just added support for :focus-visible.

    Hiding focus styles is bad practice due to accessibility requirements (keyboard navigation) which makes your websites less accessible.

    Use :focus-visible pseudo-class and let the browser to determinate when to apply focus.

    :focus-visible /* Chrome */
    

    Note that Firefox supports similar functionality through an older, prefixed pseudo-class:

    :-moz-focusring /* Firefox */
    

    button {
      color: #000;
      background-color: #fff;
      padding: 10px 16px;
      margin: 10px 0;
      border-radius: 4px;
    }
    
    button:focus {
      box-shadow: 0 0 0 2px #E59700;
      outline: 0;
    }
    
    button:hover {
      background-color: #eee;
    }
    
    button.with-focus-visible:focus:not(:focus-visible) {
      box-shadow: none;
      outline: 0;
    }
    
    button.with-focus-visible:focus-visible, 
    button.with-focus-visible:moz-focusring {
      box-shadow: 0 0 0 2px #E59700;
      outline: 0;
    }

    Click on the button using a mouse. Then try tabbing to the button.

    docs: https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible

    w3 specifications: https://www.w3.org/TR/selectors-4/#the-focus-visible-pseudo

提交回复
热议问题