Dav*_*och 3876 html css html5 placeholder html-input
Chrome支持占位符属性的input[type=text]
元素(别人可能做太多).
但以下CSS
内容对占位符的值没有任何作用:
input[placeholder], [placeholder], *[placeholder] {
color: red !important;
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" placeholder="Value">
Run Code Online (Sandbox Code Playgroud)
Value
仍然会留下grey
而不是red
.
有没有办法改变占位符文本的颜色?
fux*_*xia 4745
有三种不同的实现:伪元素,伪类和什么都没有.
::-webkit-input-placeholder
.[ 参考 ]:-moz-placeholder
(一个冒号).[ 参考 ]::-moz-placeholder
但旧的选择器仍然可以使用一段时间.[ 参考 ]:-ms-input-placeholder
.[ 参考 ]::placeholder
[ Ref ]Internet Explorer 9及更低版本根本不支持该placeholder
属性,而Opera 12及更低版本不支持占位符的任何CSS选择器.
关于最佳实施的讨论仍在继续.请注意,伪元素就像Shadow DOM中的真实元素一样.A padding
上的a input
将不会获得与伪元素相同的背景颜色.
用户代理需要忽略具有未知选择器的规则.请参阅选择器级别3:
包含无效选择器的一组选择器无效.
所以我们需要为每个浏览器单独的规则.否则,所有浏览器都会忽略整个组.
::-webkit-input-placeholder { /* WebKit, Blink, Edge */
color: #909;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: #909;
opacity: 1;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #909;
opacity: 1;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
color: #909;
}
::-ms-input-placeholder { /* Microsoft Edge */
color: #909;
}
::placeholder { /* Most modern browsers support this now. */
color: #909;
}
Run Code Online (Sandbox Code Playgroud)
<input placeholder="Stack Snippets are awesome!">
Run Code Online (Sandbox Code Playgroud)
opacity: 1
此处使用.em
并使用较大的最小字体大小设置对其进行测试.不要忘记翻译:某些语言需要更多空间来存放同一个单词.placeholder
应该测试支持HTML 但没有CSS支持的浏览器(如Opera).input
类型(email
,search
)使用其他默认CSS .这些可能会以意想不到的方式影响渲染.使用属性 -webkit-appearance
并-moz-appearance
更改它.例: [type="search"] {
-moz-appearance: textfield;
-webkit-appearance: textfield;
appearance: textfield;
}
Run Code Online (Sandbox Code Playgroud)
bri*_*out 707
/* do not group these rules */
*::-webkit-input-placeholder {
color: red;
}
*:-moz-placeholder {
/* FF 4-18 */
color: red;
opacity: 1;
}
*::-moz-placeholder {
/* FF 19+ */
color: red;
opacity: 1;
}
*:-ms-input-placeholder {
/* IE 10+ */
color: red;
}
*::-ms-input-placeholder {
/* Microsoft Edge */
color: red;
}
*::placeholder {
/* modern browser */
color: red;
}
Run Code Online (Sandbox Code Playgroud)
<input placeholder="hello"/> <br />
<textarea placeholder="hello"></textarea>
Run Code Online (Sandbox Code Playgroud)
这将为所有input
和textarea
占位符设置样式.
重要说明:请勿对这些规则进行分组.相反,为每个选择器创建一个单独的规则(组中的一个无效选择器使整个组无效).
Mat*_*att 270
您可能还想要textareas样式:
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
color: #636363;
}
input:-moz-placeholder, textarea:-moz-placeholder {
color: #636363;
}
Run Code Online (Sandbox Code Playgroud)
EII*_*PII 104
对于Bootstrap和Less用户,有一个mixin .placeholder:
// Placeholder text
// -------------------------
.placeholder(@color: @placeholderText) {
&:-moz-placeholder {
color: @color;
}
&:-ms-input-placeholder {
color: @color;
}
&::-webkit-input-placeholder {
color: @color;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 74
对于Sass用户:
// Create placeholder mixin
@mixin placeholder($color, $size:"") {
&::-webkit-input-placeholder {
color: $color;
@if $size != "" {
font-size: $size;
}
}
&:-moz-placeholder {
color: $color;
@if $size != "" {
font-size: $size;
}
}
&::-moz-placeholder {
color: $color;
@if $size != "" {
font-size: $size;
}
}
&:-ms-input-placeholder {
color: $color;
@if $size != "" {
font-size: $size;
}
}
}
// Use placeholder mixin (the size parameter is optional)
[placeholder] {
@include placeholder(red, 10px);
}
Run Code Online (Sandbox Code Playgroud)
Lov*_*edi 59
这样可以正常工作.在这里演示:
input::-webkit-input-placeholder,
textarea::-webkit-input-placeholder {
color: #666;
}
input:-moz-placeholder,
textarea:-moz-placeholder {
color: #666;
}
input::-moz-placeholder,
textarea::-moz-placeholder {
color: #666;
}
input:-ms-input-placeholder,
textarea:-ms-input-placeholder {
color: #666;
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" placeholder="Value" />
Run Code Online (Sandbox Code Playgroud)
Dio*_*tis 47
在Firefox和Internet Explorer中,普通输入文本颜色会覆盖占位符的颜色属性.所以,我们需要
::-webkit-input-placeholder {
color: red; text-overflow: ellipsis;
}
:-moz-placeholder {
color: #acacac !important; text-overflow: ellipsis;
}
::-moz-placeholder {
color: #acacac !important; text-overflow: ellipsis;
} /* For the future */
:-ms-input-placeholder {
color: #acacac !important; text-overflow: ellipsis;
}
Run Code Online (Sandbox Code Playgroud)
Kri*_*ian 40
跨浏览器解决方案:
/* all elements */
::-webkit-input-placeholder { color:#f00; }
::-moz-placeholder { color:#f00; } /* firefox 19+ */
:-ms-input-placeholder { color:#f00; } /* ie */
input:-moz-placeholder { color:#f00; }
/* individual elements: webkit */
#field2::-webkit-input-placeholder { color:#00f; }
#field3::-webkit-input-placeholder { color:#090; background:lightgreen; text-transform:uppercase; }
#field4::-webkit-input-placeholder { font-style:italic; text-decoration:overline; letter-spacing:3px; color:#999; }
/* individual elements: mozilla */
#field2::-moz-placeholder { color:#00f; }
#field3::-moz-placeholder { color:#090; background:lightgreen; text-transform:uppercase; }
#field4::-moz-placeholder { font-style:italic; text-decoration:overline; letter-spacing:3px; color:#999; }
Run Code Online (Sandbox Code Playgroud)
图片来源:David Walsh
Mik*_*red 39
使用新的::placeholder
,如果你使用autoprefixer.
请注意,不推荐使用Bootstrap中的.placeholder mixin来支持这一点.
例:
input::placeholder { color: black; }
Run Code Online (Sandbox Code Playgroud)
使用自动修复器时,上述内容将转换为所有浏览器的正确代码.
Bur*_*kak 35
我刚刚意识到Mozilla Firefox 19+的一些东西,浏览器为占位符提供了不透明度值,因此颜色将不是您真正想要的颜色.
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
color: #eee; opacity:1;
}
input:-moz-placeholder, textarea:-moz-placeholder {
color: #eee; opacity:1;
}
input::-moz-placeholder, textarea::-moz-placeholder {
color: #eee; opacity:1;
}
input:-ms-input-placeholder, textarea:-ms-input-placeholder {
color: #eee; opacity:1;
}
Run Code Online (Sandbox Code Playgroud)
我覆盖了opacity
1,所以它会很好.
Dra*_*scu 34
我不记得我在互联网上找到这段代码片段的地方(它不是我写的,不记得我找到它的地方,也不记得是谁写的).
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
$('[placeholder]').parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
Run Code Online (Sandbox Code Playgroud)
只需加载此JavaScript代码,然后通过调用此规则使用CSS编辑占位符:
form .placeholder {
color: #222;
font-size: 25px;
/* etc. */
}
Run Code Online (Sandbox Code Playgroud)
Ali*_*ese 33
我认为这段代码可行,因为只需要输入类型文本占位符.所以这一行CSS足以满足您的需求:
input[type="text"]::-webkit-input-placeholder {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
gfa*_*ess 31
对于Bootstrap用户,如果您使用class="form-control"
,可能存在CSS特异性问题.你应该获得更高的优先级:
.form-control::-webkit-input-placeholder {
color: red;
}
//.. and other browsers
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用Less:
.form-control{
.placeholder(red);
}
Run Code Online (Sandbox Code Playgroud)
ste*_*ddy 18
如果您正在使用Bootstrap并且无法使其工作,那么可能您错过了Bootstrap本身添加这些选择器的事实.这是我们正在谈论的Bootstrap v3.3.
如果您尝试更改.form-control CSS类中的占位符,那么您应该像这样覆盖它:
.form-control::-webkit-input-placeholder { /* WebKit, Blink, Edge */
color: #777;
}
.form-control:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: #777;
opacity: 1;
}
.form-control::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #777;
opacity: 1;
}
.form-control:-ms-input-placeholder { /* Internet Explorer 10-11 */
color: #777;
}
Run Code Online (Sandbox Code Playgroud)
use*_*061 16
这个怎么样
<input type="text" value="placeholder text" onfocus="this.style.color='#000';
this.value='';" style="color: #f00;" />
Run Code Online (Sandbox Code Playgroud)
没有CSS或占位符,但您获得相同的功能.
Ban*_*hur 16
这个简短而干净的代码:
::-webkit-input-placeholder {color: red;}
:-moz-placeholder {color: red; /* For Firefox 18- */}
::-moz-placeholder {color: red; /* For Firefox 19+ */}
:-ms-input-placeholder {color: red;}
Run Code Online (Sandbox Code Playgroud)
小智 14
我在这里尝试了各种组合来改变颜色,在我的移动平台上,最终它是:
-webkit-text-fill-color: red;
Run Code Online (Sandbox Code Playgroud)
这诀窍.
对于使用Bourbon的 SASS/SCSS用户,它具有内置功能.
//main.scss
@import 'bourbon';
input {
width: 300px;
@include placeholder {
color: red;
}
}
Run Code Online (Sandbox Code Playgroud)
CSS输出,你也可以抓住这部分并粘贴到你的代码中.
//main.css
input {
width: 300px;
}
input::-webkit-input-placeholder {
color: red;
}
input:-moz-placeholder {
color: red;
}
input::-moz-placeholder {
color: red;
}
input:-ms-input-placeholder {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
尝试这个代码为不同的输入元素不同的风格
your css selector::-webkit-input-placeholder { /*for webkit */
color:#909090;
opacity:1;
}
your css selector:-moz-placeholder { /*for mozilla */
color:#909090;
opacity:1;
}
your css selector:-ms-input-placeholder { /*for for internet exprolar */
color:#909090;
opacity:1;
}
Run Code Online (Sandbox Code Playgroud)
例1:
input[type="text"]::-webkit-input-placeholder { /*for webkit */
color: red;
opacity:1;
}
input[type="text"]:-moz-placeholder { /*for mozilla */
color: red;
opacity:1;
}
input[type="text"]:-ms-input-placeholder { /*for for internet exprolar */
color: red;
opacity:1;
}
Run Code Online (Sandbox Code Playgroud)
例2:
input[type="email"]::-webkit-input-placeholder { /*for webkit */
color: gray;
opacity:1;
}
input[type="email"]:-moz-placeholder { /*for mozilla */
color: gray;
opacity:1;
}
input[type="email"]:-ms-input-placeholder { /*for for internet exprolar */
color: gray;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 CSS 更改 HTML5 输入的占位符颜色。如果碰巧你的 CSS 发生冲突,这段代码可以正常工作,你可以使用 (!important) ,如下所示。
::-webkit-input-placeholder { /* WebKit, Blink, Edge */
color:#909 !important;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color:#909 !important;
opacity:1 !important;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
color:#909 !important;
opacity:1 !important;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
color:#909 !important;
}
::-ms-input-placeholder { /* Microsoft Edge */
color:#909 !important;
}
<input placeholder="Stack Snippets are awesome!">
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助。
这对于大多数现代浏览器来说都很好
input::placeholder{
color: red; // css implementation
}
Run Code Online (Sandbox Code Playgroud)
以防万一您正在使用 SCSS
input {
&::placeholder {
color: red; // scss
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
::placeholder{
color: red;
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" placeholder="Value">
Run Code Online (Sandbox Code Playgroud)
这是另一个例子:
.form-control::-webkit-input-placeholder {
color: red;
width: 250px;
}
h1 {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
<div class="col-sm-4">
<input class="form-control" placeholder="Enter text here.." ng-model="Email" required/>
</div>
Run Code Online (Sandbox Code Playgroud)
好的,占位符在不同的浏览器中表现不同,因此您需要在 CSS 中使用浏览器前缀使它们相同,例如 Firefox 默认为占位符提供透明度,因此需要在您的 css 中添加 opacity 1,加上颜色,它不是大多数时候都是一个大问题,但最好让它们保持一致:
*::-webkit-input-placeholder { /* WebKit browsers */
color: #ccc;
}
*:-moz-placeholder { /* Mozilla Firefox <18 */
color: #ccc;
opacity: 1;
}
*::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #ccc;
opacity: 1;
}
*:-ms-input-placeholder { /* Internet Explorer 10-11 */
color: #ccc;
}
Run Code Online (Sandbox Code Playgroud)
您可以将其用于输入和焦点样式:
input::-webkit-input-placeholder { color:#666;}
input:-moz-placeholder { color:#666;}
input::-moz-placeholder { color:#666;}
input:-ms-input-placeholder { color:#666;}
/* focus */
input:focus::-webkit-input-placeholder { color:#eee; }
input:focus:-moz-placeholder { color:#eee } /* FF 4-18 */
input:focus::-moz-placeholder { color:#eee } /* FF 19+ */
input:focus:-ms-input-placeholder { color:#eee } /* IE 10+ */
Run Code Online (Sandbox Code Playgroud)
添加一个实际的非常好的和简单的可能性:css过滤器!
它将设置所有样式,包括占位符。
下面将使用色相滤镜更改颜色,将两个输入都设置在同一调色板上。现在,它在浏览器中的渲染效果非常好(除了...)
input {
filter: sepia(100%) saturate(400%) grayscale(0) contrast(200%) hue-rotate(68deg) invert(18%);
}
Run Code Online (Sandbox Code Playgroud)
<input placeholder="Hello world!" />
<input type="date" /><br>
<input type="range" />
<input type="color" />
Run Code Online (Sandbox Code Playgroud)
要允许用户使用输入类型的颜色进行更改或查找细微差别来动态更改它,请查看以下代码段:
来自:https : //codepen.io/Nico_KraZhtest/pen/bWExEB
function stylElem() {
stylo.dataset.hue = ((parseInt(stylo.value.substring(1), 16))/46666).toFixed(0)
Array.from(document.querySelectorAll('input, audio, video')).forEach(function(e){
e.style.cssText += ";filter:sepia(100%) saturate(400%)grayscale(0)contrast(200%)hue-rotate("+ stylo.dataset.hue+"deg)invert("+(stylo.dataset.hue/3.6)+"%)"
out.innerText = e.style.cssText
})()}
stylElem()
Run Code Online (Sandbox Code Playgroud)
<div id="out"></div> <p>
<input placeholder="Hello world!" />
<input type="date" /><br>
<input type="range" />
<input type="color" />
Colors (change me)->
<input type="color" id="stylo" oninput="stylElem()" /> <br><br>
<audio controls src="#"></audio> <br><br>
<video controls src="#"></video>
Run Code Online (Sandbox Code Playgroud)
CSS过滤器文档:https://developer.mozilla.org/zh-CN/docs/Web/CSS/filter
最简单的方法是:
#yourInput::placeholder {
color: red;/*As an example*/
}
/* if that would not work, you can always try styling the attribute itself: */
#myInput[placeholder] {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1709324 次 |
最近记录: |