我在一个网页上工作,我想自定义样式的<按钮>标签。在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;
}

当前回答

我遇到了同样的问题,所以我使用简单的CSS-

.custom-button {
    outline: none
}

其他回答

不建议这样做,因为这会降低网站的可访问性;更多信息,请看这篇文章。

也就是说,如果你坚持,这个CSS应该工作:

button:focus {outline:0;}

查看它或JSFiddle: http://jsfiddle.net/u4pXu/

或者在这个片段中:

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.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; } button:focus {outline:0;} <button class="launch">Launch with these ads</button> <button class="change">Change</button>

直到所有现代浏览器都开始支持css-selector:focus-visible, 保存可访问性的最简单,可能也是最好的方法是将这个棘手的焦点仅留给鼠标用户,而留给键盘用户:

1.使用这个小的填充(大约10kb): https://github.com/WICG/focus-visible 2.在css中添加下一个代码:

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

浏览器对css4-selector:focus-visible的支持现在非常弱: https://caniuse.com/#search=focus-visible

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

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

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

button:focus {outline:0 !important;}

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

等等!那丑陋的轮廓是有原因的!

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.

可能的解决方案:对焦时使按钮变暗

对于下面的例子,Chrome的蓝色轮廓首先被删除使用按钮:focus {outline:0 !}

下面是你的基本引导按钮,因为他们通常出现:

下面是接收焦点时的按钮:

下面是按下按钮时的情况:

正如您所看到的,当按钮获得焦点时,它们会稍微暗一些。就我个人而言,我会建议将焦点按钮设置得更暗,这样在焦点状态和正常状态之间就会有非常明显的区别。

它不仅适用于残疾用户

让你的网站更容易访问是经常被忽视的事情,但它可以帮助你在网站上创造更富有成效的体验。有许多普通用户使用键盘命令浏览网站,以保持双手在键盘上。