You need to enable JavaScript to run this app. 输入框设置绿色背景后聚焦输入时恢复默认白色的问题求助
优惠活动
大模型
产品
解决方案
定价
更多

输入框设置绿色背景后聚焦输入时恢复默认白色的问题求助

问题核心诱因
  • 浏览器对原生表单控件内置了默认样式规则,聚焦状态下的背景色重置逻辑,会在状态匹配时覆盖普通全局input选择器定义的背景值,最终导致预设的绿色在聚焦时失效,被替换为系统默认白色。
修复步骤
  1. 显式为输入框的聚焦状态声明一致的绿色背景,不要仅依赖全局input的基础样式规则。如果全局input规则权重不足,可以直接针对当前组件下的输入框类名补充聚焦规则,提升选择器权重,避免被其他通用样式覆盖:
/* 保留原有全局input基础样式 */
input {
    width: 100%;
    color: $white;      
    border: none;
    border-bottom: 1px solid $grey-light;
    outline: none;
    font-size: 1rem;
    margin-bottom: 1.25rem;
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
    background-color: $green;
}

/* 新增聚焦状态的背景声明 */
input:focus,
.form__name:focus,
.form__email:focus {
    /* 强制将聚焦态背景设为和基础态一致的绿色 */
    background-color: $green;
}

/* 兜底兼容Chrome等Webkit内核浏览器的自动填充、聚焦默认样式覆盖问题 */
input:-webkit-autofill,
input:-webkit-autofill:focus {
    -webkit-box-shadow: 0 0 0 1000px $green inset;
    -webkit-text-fill-color: $white;
    transition: background-color 5000s ease-in-out 0s;
}
  1. 优先级排查:确认项目内不存在其他针对input:focus的样式规则,其选择器权重高于上述规则,把背景色重置为白色。
  2. 预编译样式校验:如果使用了Scss/Less等预处理器,检查$green变量是否被正确定义、编译为有效色值,避免变量未定义导致的样式失效。

内容的提问来源于stack exchange,提问作者JGarnie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.12.03 18:45:04