You need to enable JavaScript to run this app. 如何设置input输入框文字为font-weight:bold且不改变placeholder字重
优惠活动
大模型
产品
解决方案
定价
更多

如何设置input输入框文字为font-weight:bold且不改变placeholder字重

解决方案

你可以通过单独为 input 的 ::placeholder 伪元素设置字重,覆盖从父元素继承的粗体样式,实现仅输入文本加粗的效果:

  • 第一步:给 input 元素本身设置 font-weight: bold,作用于用户输入的文本
  • 第二步:用各浏览器兼容的 placeholder 伪元素选择器,单独将占位符的字重设为 normal

代码示例

<input type="text" placeholder="请输入内容">
/* 输入文本加粗 */
input {
  font-weight: bold;
}

/* 占位符保持常规字重 */
/* Chrome、Safari、新版Edge */
input::-webkit-input-placeholder {
  font-weight: normal;
}
/* 火狐浏览器 */
input::-moz-placeholder {
  font-weight: normal;
}
/* IE10、IE11 */
input:-ms-input-placeholder {
  font-weight: normal;
}
/* W3C 标准语法 */
input::placeholder {
  font-weight: normal;
}

原理解释

placeholder 伪元素默认会继承 input 元素的字体相关样式,单独为该伪元素指定样式后,不会影响用户实际输入的文本样式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 03:45:05