简体   繁体   English

真的需要* html选择器吗?

[英]Is *html selector really needed?

I just read this code somewhere. 我刚才在某处读过这段代码。

*html #menu ul{margin: 0px 0px 0px 40px;width:960px;}

I know * means the selector means any element in css. 我知道*意味着选择器意味着css中的任何元素。 But is this needed here? 但这需要吗? Since it works totally the same without it.Or is it some kind of CSS hacks in specific browsers ? 因为它在没有它的情况下完全相同。或者在特定浏览器中它是某种CSS hacks?

It is a CSS hack targeted at Internet Explorer 6 and below. 这是针对Internet Explorer 6及更低版本的CSS hack。 More modern browsers should ignore any of the styles applied using *html . 更现代的浏览器应该忽略使用*html应用的任何样式。

List of browsers which will apply the styles 将应用样式的浏览器列表

  • IE4-6/Win IE4-6 /运
  • IE4-5/Mac IE4-5 / Mac的
  • IE7+, when in backwards compatibility mode (aka quirks mode) IE7 +,处于向后兼容模式(又名怪癖模式)

List of browsers which will ignore the styles 将忽略样式的浏览器列表

  • IE7+, when in Standards Compatibility mode IE7 +,处于标准兼容模式时
  • Firefox 火狐
  • NS6.0-7.2 NS6.0-7.2
  • NS4.x NS4.x
  • Opera 5+ (I don't know about versions 1 through 4.) Opera 5+(我不知道版本1到4)
  • Safari 苹果浏览器
  • Konqueror 3 Konqueror 3
  • iCab 3 iCab 3

Source: * html ("Star HTML") CSS Hack 资料来源: * html(“Star HTML”)CSS Hack

So, provided your document is valid then IE7+ should display the page in standards mode and will ignore the styles. 因此,如果您的文档有效,那么IE7 +应该以标准模式显示页面并忽略样式。 If the page is being displayed in 'quirks mode' then the styles will be applied in IE7+. 如果页面以“怪癖模式”显示,则样式将应用于IE7 +。

The Star Selector Hack 明星选择器黑客

The star selector hack, also known as the star-HTML hack and the Tan hack, because it was first described in detail by Edwardson Tan, is the most widely used filter; 明星选择器hack,也被称为明星HTML黑客和Tan hack,因为它首先由Edwardson Tan详细描述,是最广泛使用的过滤器; it relies on a peculiar behavior in Internet Explorer 5.5 and 6. Even though it's often labeled a hack, I've included it in this section on filters because, despite the fact that it exploits a browser bug, it uses a valid CSS selector. 它依赖于Internet Explorer 5.5和6中的特殊行为。尽管它经常被标记为hack,但我已将其包含在此部分的过滤器中,因为尽管它利用了浏览器错误,但它使用了有效的CSS选择器。 The selector, however, should never match any elements; 但是,选择器永远不应匹配任何元素; all browsers, except Internet Explorer 5.5 and 6, understand this fact and ignore the rule. 除Internet Explorer 5.5和6之外的所有浏览器都了解这一事实并忽略该规则。

The technique is simply to apply a descendant selector that makes use of the universal selector. 该技术只是应用一个使用通用选择器的后代选择器。 The universal selector is, of course, valid CSS, so don't be confused and start thinking that using the universal selector is bad news. 当然,通用选择器是有效的CSS,所以不要混淆并开始认为使用通用选择器是坏消息。 The most common form of the technique (and the origin of its name) is to compose a rule with the * html selector. 最常见的技术形式(及其名称的来源)是使用* html选择器组成规则。 This constitutes valid CSS, but it shouldn't match any elements. 这构成了有效的CSS,但它不应与任何元素匹配。 The selector should apply the rule to any html element that's the descendant of any other element and, as html is the root element, it's never a descendant of any other element. 选择器应该将规则应用于任何其他元素的后代的html元素,并且由于html是根元素,因此它永远不是任何其他元素的后代。

However, while most other browsers ignore it, Internet Explorer 5.5 and 6 will interpret this selector as if there was no universal selector, like the rule below: 但是,虽然大多数其他浏览器忽略它,但Internet Explorer 5.5和6会将此选择器解释为没有通用选择器,如下面的规则:

html {
  ⋮ declarations
}

Thus, the star selector hack is a safe way of applying CSS rules to Internet Explorer 5.5 and 6 without affecting other browsers. 因此,星级选择器hack是一种将CSS规则应用于Internet Explorer 5.5和6而不影响其他浏览器的安全方式。

You'd use it like this: 你会这样使用它:

.test {
  position: fixed;
}
* html .test{
  position: absolute;
}

Only Internet Explorer 6 and earlier versions will apply the latter rule; 只有Internet Explorer 6及更早版本才会应用后一规则; other browsers will ignore it. 其他浏览器会忽略它。

Source : http://reference.sitepoint.com/css/workaroundsfilters#workaroundsfilters__sect_starhtmlselector 资料来源: http//reference.sitepoint.com/css/workaroundsfilters#workaroundsfilters__sect_starhtmlselector

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

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