我在一个网页上工作,我想自定义样式的<按钮>标签。在CSS中,我说:border: none。现在它在safari中工作得很好,但在chrome中,当我点击其中一个按钮时,它会在它周围放上一个烦人的蓝色边框。我认为button:active {outline:none}或button:focus {outline:none}将工作,但两者都没有。什么好主意吗?

这是它在被点击之前的样子(以及我希望它在被点击之后的样子):

这就是我所说的边界:

这是我的CSS:

button.launch {
    background-color: #F9A300;
    border: none;
    height: 40px;
    padding: 5px 15px;
    color: #ffffff;
    font-size: 16px;
    font-weight: 300;
    margin-top: 10px;
    margin-right: 10px;
}

button.launch:hover {
    cursor: pointer;
    background-color: #FABD44;
}

button.change {
    background-color: #F88F00;
    border: none;
    height: 40px;
    padding: 5px 15px;
    color: #ffffff;
    font-size: 16px;
    font-weight: 300;
    margin-top: 10px;
    margin-right: 10px;
}

button.change:hover {
    cursor: pointer;
    background-color: #F89900;
}

button:active {
    outline: none;
    border: none;
}

当前回答

对于这个问题:

用这个:

   *{
         -webkit-tap-highlight-color: rgba(0,0,0,0);
         -webkit-tap-highlight-color: transparent; /* For some Androids */
    }

结果:

其他回答

为了获得更好的结果,不要忘记!important声明

button:focus {outline:0 !important;}

具有!important属性的规则将始终被应用,无论该规则出现在CSS文档中的哪个位置。

在这个问题的实例中,我必须指定box-shadow: none

button:focus {
  outline:none;
  box-shadow: none;
}

如果您使用Bootstrap 4.1或其他版本,大多数解决方案将无法工作。在敲了很多脑袋之后,我发现你需要应用无影课程:

<button class="btn shadow-none">Bootstrap (4.1) button without shadow</button>

解决可访问性问题的另一种方法是通过一点Javascript。感谢来自hackernoon的这篇深刻的博客文章:https://hackernoon.com/removing-that-ugly-focus-ring-and-keeping-it-too-6c8727fefcd2

这里的方法非常简单而有效:当人们开始使用tab键导航页面时添加一个类(当再次切换到鼠标时可选地删除它)。然后,您可以使用这个类来显示或不显示焦点轮廓。

function handleFirstTab(e) {
    if (e.keyCode === 9) { // the "I am a keyboard user" key
        document.body.classList.add('user-is-tabbing');
        window.removeEventListener('keydown', handleFirstTab);
    }
}

window.addEventListener('keydown', handleFirstTab);

要删除蓝色背景的水龙头,我使用

button {
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}