简体   繁体   English

CSS:Inte.net Explorer 中的简单渐变 <= 8

[英]CSS: Simple Gradients in Internet Explorer <= 8

As I'm sure you all know, Inte.net Explorer can handle simple gradients.我相信你们都知道,Inte.net Explorer 可以处理简单的渐变。 Here is a snippet from Twitter Bootstrap, for example:这是来自 Twitter Bootstrap 的片段,例如:

filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#e6e6e6', GradientType=0);

However, I've seen some people use two CSS rules (one for IE < 8 and one for IE 8), like so:但是,我看到有些人使用两个 CSS 规则(一个用于 IE < 8,一个用于 IE 8),如下所示:

filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#7fbf4d', endColorstr='#63a62f'); /* For Internet Explorer 5.5 - 7 */
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#7fbf4d', endColorstr='#63a62f')"; /* For Internet Explorer 8 */

My question is, is the second rule really necessary?我的问题是,第二条规则真的有必要吗? Twitter Bootstrap is quite thorough, yet it doesn't use any "-ms-filter" rules. Twitter Bootstrap 非常彻底,但它不使用任何“-ms-filter”规则。 According to this page , the -ms-filter attribute is an extension to CSS, and can be used as a synonym for filter in IE8 Standards mode.根据此页面-ms-filter属性是对 CSS 的扩展,可以用作 IE8 标准模式下过滤器的同义词。

My suggestion:我的建议:

Use CSS3Pie -- http://css3pie.com/使用 CSS3Pie -- http://css3pie.com/

This is a JavaScript library for IE6, IE7 and IE8 that patches in support for several CSS3 features, including gradients.这是一个用于 IE6、IE7 和 IE8 的 JavaScript 库,其补丁支持多个 CSS3 功能,包括渐变。

Yes, you can use the horrible IE-specific filter styles if you want, but CSS3Pie allows you to use standard CSS styles for these features.是的,如果你愿意,你可以使用可怕的 IE 特定filter styles,但 CSS3Pie 允许你使用标准 CSS styles 来实现这些功能。 Much easier.容易多了。

To actually answer your direct question:要实际回答您的直接问题:

Yes, the -ms-filter style is usually required.是的,通常需要-ms-filter样式。 IE8 will always us it instead of filter , which is used primarily for IE6 and IE7. IE8 将始终使用它而不是filter ,后者主要用于 IE6 和 IE7。 In some cases filter will work in IE8, but not all, so it's best to use -ms-filter to ensure IE8 compatibility.在某些情况下, filter会在 IE8 中工作,但不是全部,所以最好使用-ms-filter来确保 IE8 兼容性。

[EDITED] Why did they change it? [编辑] 他们为什么要改变它? Because when they released IE8, Microsoft made a big point of trying to be "standards compliant".因为当他们发布 IE8 时,Microsoft 非常重视尝试“符合标准”。

For a browser to be standard-compliant, it should use a vendor prefix for any CSS property it supports that is not a finalized W3C standard.对于符合标准的浏览器,它应该为它支持的任何 CSS 属性使用供应商前缀,这不是最终的 W3C 标准。 So by adding the -ms- prefix, Microsoft were trying to (belatedly) undo their pollution of the global CSS namespace.因此,通过添加-ms-前缀,Microsoft 试图(迟来的)消除他们对全局 CSS 命名空间的污染。

In addition, the quotes were added, because the old filter syntax without the quotes was invalid CSS (mainly because of the colon after progid ), and caused issues with some browsers.此外,添加了引号,因为没有引号的旧filter语法无效 CSS(主要是因为progid后面的冒号),并导致某些浏览器出现问题。 (I had an instance some time ago with Firefox 3.6 where it was dropping all styles following a filter style that rotated an element - took ages to work out what was happening). (前段时间我有一个 Firefox 3.6 的实例,它按照旋转元素的filter样式丢弃所有 styles - 花了很长时间才弄清楚发生了什么)。

The fact that Microsoft retained backward compatibility with the original filter syntax, was largely a matter of pragmatism. Microsoft 保留与原始filter语法的向后兼容性这一事实在很大程度上是实用主义的问题。 MS couldn't afford to break all the sites using the old syntax. MS 无法承受使用旧语法破坏所有站点的后果。 But there was a strong implication from Microsoft that developers should use both because they would drop support for the old filter style in subsequent versions of IE.但是微软强烈暗示开发人员应该同时使用这两者,因为他们会在后续版本的 IE 中放弃对旧filter样式的支持。 As it turned out, they dropped support for both filter and -ms-filer in one fell swoop, but the implications given at the release of IE8 were sufficient for it to become recommended practice to provide both syntaxes in your stylesheet.事实证明,他们一举放弃了对filter-ms-filer的支持,但 IE8 发布时给出的含义足以使其成为在样式表中提供这两种语法的推荐做法。

At the time when IE8 was released, XHTML was the new flavor of the month, and writing code that validated perfectly was top of the list for a lot of developers.在 IE8 发布时,XHTML 是当月的新风尚,编写能够完美验证的代码是许多开发人员的首要任务。 This was probably a strong driving force in the change of syntax to include the quotes.这可能是语法更改以包含引号的强大推动力。 Because of the stray colons, it is impossible to write CSS that validates using the old filter style.由于杂散的冒号,不可能编写 CSS 来验证使用旧的filter样式。 Using the new -ms-filter styles instead allowed people to write valid CSS. As good ideas go, this one didn't really work out because of course people had to keep using the old syntax anyway, but the intent was good.使用新的-ms-filter styles 而不是允许人们编写有效的 CSS。作为好主意 go,这个并没有真正奏效,因为当然人们不得不继续使用旧语法,但意图是好的。

It's worth pointing out that other proprietary styles were given the same treatment.值得指出的是,其他专有 styles 也得到了同样的待遇。 For example, you can use -ms-behavior in IE8 instead of the old behavior style.例如,您可以在 IE8 中使用-ms-behavior来代替旧的behavior样式。 No-one uses it.没有人使用它。 Why?为什么? I don't really know.我真的不知道。

Another fact worth knowing is that the W3C are in the process of standardizing a CSS property called filter .另一个值得了解的事实是 W3C 正在标准化一个名为filter的 CSS 属性 It will do a completely different job to the Microsoft filter style, and work completely differently.它将完成与 Microsoft filter样式完全不同的工作,并且工作方式完全不同。 If/when it is standardized and browsers start supporting it, there will be an explicit clash between the two syntaxes.如果/当它被标准化并且浏览器开始支持它时,这两种语法之间将会发生明显的冲突。

Seems like you answered your own question.好像你回答了你自己的问题。 Yes they're needed.是的,他们是需要的。 Microsoft trying to get more in line with the standards means that some versions of IE have their own slightly different syntax rules for the filter property.微软试图让标准更加符合意味着某些版本的 IE 有自己的过滤属性略有不同的语法规则。

In IE7 just filter: followed by the progid:DXIma... etc will work.在 IE7 中,只需filter:后跟progid:DXIma...等即可。 But for IE8+ there is the IE7 fallback it may use in compatibility mode, and the more proper -ms- prefixed property for filter, denoting a vendor specific css property, and its value inside qoutes.但是对于 IE8+,它可能会在兼容模式下使用 IE7 回退,并且过滤器的更合适的-ms-前缀属性表示供应商特定的 css 属性,以及它在 qoutes 中的值。

What harm would it do to leave it in?留在里面会有什么危害?

If the browser doesn't understand the line of CSS it will just ignore it.如果浏览器不理解 CSS 这一行,它就会忽略它。

Those two lines of CSS are almost identical. CSS 的这两行几乎完全相同。 I would hazard a guess that Microsoft decided to change the syntax for proprietary CSS from IE8 onwards utilising the - (hyphen) prefix which other browsers have been using for yonks.我猜测微软决定从 IE8 开始更改专有 CSS 的语法,使用其他浏览器一直使用的 -(连字符)前缀。

Technically the CSS isn't W3C compliant either way, so another line of proprietary CSS can do no harm.从技术上讲,CSS 无论如何都不符合 W3C,因此专有的 CSS 的另一行不会造成伤害。

It's not worth trying to understand IE at the best of times!即使在最好的时候,也不值得尝试去了解 IE!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM