You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让CSS :focus伪类在表单输入框上生效?

解决输入框:focus伪类边框不生效的问题

问题核心:浏览器会为获得焦点的输入框添加默认的outline样式,该样式层级优先于你自定义的border设置,导致红色边框无法正常显示。

解决方案

在:focus伪类规则中添加outline: none;移除默认轮廓,同时保留自定义边框样式即可:

修改后的完整CSS代码

form.customNewsletter-form {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  align-items: flex-start;
  margin: 0;
  padding: 0;
  gap: 16px;
}

form.customNewsletter-form input[type="text"] {
  font-family: "Roboto", sans-serif;
  font-size: 16px;
  font-weight: 300;
  color: #ffffff;
  background-color: #0d0d0d;
  border: 1px solid #404040;
  width: 70%;
  height: 48px;
  padding: 0 16px;
  letter-spacing: 0.8px;
  border-radius: 4px;
  margin: 0;
}

form.customNewsletter-form input[type="text"]:focus {
  border: 1px solid #cd0d0d;
  outline: none; /* 移除浏览器默认焦点轮廓 */
}

form.customNewsletter-form input::placeholder {
  color: #8d8d8d;
}

form.customNewsletter-form input[type="submit"] {
  color: #ffffff;
  font-family: "Roboto Condensed", sans-serif;
  font-size: 16px;
  font-weight: 300;
  background-color: #cd0d0d;
  border: none;
  border-radius: 4px;
  height: 48px !important;
  width: 30%;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  margin: 0 auto !important;
  transition: 0.3s all ease;
}

form.customNewsletter-form input[type="submit"]:hover {
  background-color: #8e0000;
  color: #ffffff;
  -webkit-transform: scale(0.99);
  transform: scale(0.99);
}

可访问性补充提示

移除默认outline可能会影响键盘导航用户识别焦点位置,若需兼顾可访问性,可自定义outline样式替代默认值,示例:

form.customNewsletter-form input[type="text"]:focus {
  border: 1px solid #cd0d0d;
  outline: 2px solid #cd0d0d;
  outline-offset: 2px;
}

内容的提问来源于stack exchange,提问作者デンズドネザ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 02:40:25