占位符CSS调用问题(Placeholder CSS Calling Issue)

编程入门 行业动态 更新时间:2024-10-25 18:26:10
占位符CSS调用问题(Placeholder CSS Calling Issue)

通常我们使用这个想法(用逗号分隔的不同类) .Test,.test1 {} ,但是这里只有我们分别调用类才能正常工作。

为什么这个问题? 在这里演示http://jsfiddle.net/6AR8n/

/* seperate classes */ #red input:-moz-placeholder{ color:red; } #red input::-webkit-input-placeholder{ color:red; } /* Classes with coma */ #green input:-moz-placeholder, input::-webkit-input-placeholder{ color:green; } <div id="red"> <input type="text" placeholder="Without Coma" > </div> <br> <div id="green"> <input type="text" placeholder="Coma" > </div>

Normally we use this idea (different classes separating with comma) .Test, .test1 {} , But here it will work fine only we call classes separately.

Why this issue ? Demo here http://jsfiddle.net/6AR8n/

/* seperate classes */ #red input:-moz-placeholder{ color:red; } #red input::-webkit-input-placeholder{ color:red; } /* Classes with coma */ #green input:-moz-placeholder, input::-webkit-input-placeholder{ color:green; } <div id="red"> <input type="text" placeholder="Without Coma" > </div> <br> <div id="green"> <input type="text" placeholder="Coma" > </div>

最满意答案

这是因为浏览器在遇到无法识别的选择器时应该删除整个规则。 从CSS2.1规范 :

选择器总是与声明块一起使用。 当用户代理无法解析选择器(即,它不是有效的CSS 2.1)时,它必须忽略选择器和以下声明块(如果有的话)。

这包括前缀选择器,例如:-moz-placeholder和::-webkit-input-placeholder在你的例子中,因为浏览器不应该尝试解析它不支持的前缀; 对于某个解析器,外部前缀与任何其他语法错误一样无效。

另外,正如评论中所提到的, #green部分需要在逗号分隔组中的两个选择器上进行复制,如下所示:

#green input:-moz-placeholder, #green input::-webkit-input-placeholder{ color:green; }

但这与手头的问题完全无关。

This is because browsers are supposed to drop the entire rule when encountering unrecognized selectors. From the CSS2.1 spec:

A selector always goes together with a declaration block. When a user agent cannot parse the selector (i.e., it is not valid CSS 2.1), it must ignore the selector and the following declaration block (if any) as well.

This includes prefixed selectors, like :-moz-placeholder and ::-webkit-input-placeholder in your example, because a browser isn't supposed to attempt to parse a prefix that it doesn't support; to a certain parser, foreign prefixes are just as invalid as any other syntax error.

Also, as mentioned in the comments the #green part needs to be replicated on both selectors in your comma-separated group, like so:

#green input:-moz-placeholder, #green input::-webkit-input-placeholder{ color:green; }

But this is completely irrelevant to the issue at hand.

更多推荐

本文发布于:2023-08-04 10:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Placeholder   CSS   Issue   Calling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!