Django - 如何为表单字段的占位符添加样式?

3

好的,我已经安装了django-registration应用程序,并为注册功能添加了一个小部件,以便在电子邮件中添加占位符:

email = forms.EmailField(label=_("E-mail"),
                         widget=forms.TextInput(attrs={'placeholder': 'Your Email'}))

我更喜欢使用占位符而不是标签。

现在,我的问题是:如何样式化占位符?例如添加一些颜色,改变占位符的大小和字体等等。谢谢!


可能是使用CSS更改输入的HTML5占位符颜色的重复问题。 - Ludwik Trammer
你可以在attrs中添加class='somestyleclass'。 - Victor 'Chris' Cabral
2个回答

2

您可以这样做。

email = forms.EmailField(label=_("E-mail"),
                     widget=forms.TextInput(attrs={'placeholder': 'Your Email', 'class':'your_css_code'}))

1

我也遇到了类似的问题,但后来意识到你不需要在Django表单中对占位符进行样式设置。只需将以下CSS添加到样式表中即可解决问题。

::-webkit-input-placeholder {
   color: rgba(255, 255, 255, .5);
}

:-moz-placeholder { /* Firefox 18- */
   color: rgba(255, 255, 255, .5);
}

::-moz-placeholder {  /* Firefox 19+ */
   color: rgba(255, 255, 255, .5);
}

:-ms-input-placeholder {
   color: rgba(255, 255, 255, .5);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接