修改input元素placeholder样式的方法

我们时常需要跟input输入框打交道,很多情况下都要对placeholder的内容进行修改,例子:

{/* React的写法 */}
<input type="text" placeholder={'placeholder'}/>
复制代码

默认情况下的显示如下:

我们是没法直接对input元素设置样式来更改placeholder,需要通过伪元素 ::-webkit-input-placeholder 的方式来修改样式:

::-webkit-input-placeholder {
    color: red;
    font-size: 20px;
    font-weight: bolder;
}
复制代码

显示的效果:

这里补充一下针对不同浏览器的用法:

::-webkit-input-placeholder { /* WebKit, Blink, Edge */
    color: red;
    font-size: 20px;
    font-weight: bolder;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
    color: red;
    font-size: 20px;
    font-weight: bolder;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
    color: red;
    font-size: 20px;
    font-weight: bolder;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
    color: red;
    font-size: 20px;
    font-weight: bolder;
}
复制代码

转载于:https://juejin.im/post/5d03955a6fb9a07eae2a5ab8

猜你喜欢

转载自blog.csdn.net/weixin_34217773/article/details/93173550
今日推荐