CSS filter

From Seo Wiki - Search Engine Optimization and Programming Languages

Jump to: navigation, search
This article is about a CSS hacking technique. Not to be confused with the proprietary Microsoft-specific CSS property.

A CSS filter or hack is a coding technique used to hide or show CSS markup depending on the browser, version number, or capabilities. Browsers have different interpretations of CSS behavior and different levels of support for the W3C standards. CSS filters are sometimes used to achieve consistent layout appearance in multiple browsers that do not have compatible rendering.

Contents

Commented backslash

This hack exploits a bug in Internet Explorer for Mac related to comment parsing. A comment ending in \*/ is not properly closed in IE Mac, so rules that need to be ignored in IE Mac can be placed after such a comment. Another comment is needed after the rule to close the comment for IE Mac.[1]

/* Ignore the next rule in IE mac \*/
selector { ...styles... }
/* Stop ignoring in IE mac */

Box model hack

Called the "box model hack" because the bug it is most often used to work around is the Internet Explorer box model bug, this hack provides a different set of properties to Internet Explorer and other browsers. As of version 6, IE has corrected the box model bug in documents which include certain Document Type Declarations (required by the HTML specifications) in certain ways.

#elem { 
 width: [IE width]; 
 voice-family: "\"}\""; 
 voice-family:inherit;
 width: [Other browser width];
}
html>body #elem {
 width:[Other browser width];
} 

The first voice-family statement is set to the string "}", but an IE parser bug will interpret it as a string with a single backslash followed by a closing brace for the end of the rule. voice-family is chosen because it will not affect rendering on a screen style sheet. The second rule uses the html>body hack for browsers such as Opera 5 that have the parsing bug but do not have the box model bug (and, additionally, which support the child selector).[2]

Underscore hack

Versions 6 and below of Internet Explorer recognize properties with this prefix (after discarding the prefix). All other browsers ignore such properties as invalid. Therefore, a property that is preceded by an underscore or a hyphen is applied exclusively in Internet Explorer 6 and below.

#elem {
 width: [W3C Model Width];
 _width: [BorderBox Model];
}

This hack uses invalid CSS[3], relies on a "bug" in the browser, and there are valid CSS directives to accomplish the same thing, so this hack is not recommended.

Star hack

Versions 7 and below of Internet Explorer recognize properties which are preceded by non-alphanumeric characters except an underscore or a hyphen (after discarding the prefix). All other browsers ignore such properties as invalid. Therefore, a property that is preceded by an non-alphanumeric character other than an underscore or a hyphen, such as an asterisk or star, is applied exclusively in Internet Explorer 7 and below.

#elem {
 width: [W3C Model Width];
 *width: [BorderBox Model];
}

This hack uses invalid CSS[3], relies on a "bug" in the browser, and there are valid CSS directives to accomplish the same thing, so this hack is not recommended.

Star HTML hack

The html element is the root element of the W3C standard DOM, but Internet Explorer versions 4 through 6 include a mysterious parent element.[4] Fully-compliant browsers will ignore the * html selector, while IE4-6 will process it normally. This enables rules to be specified for these versions of Internet Explorer which will be ignored by all other browsers. For example, this rule specifies text size in Internet Explorer 4-6, but not in any other browsers.

* html p {font-size: 5em; }

This hack uses fully valid CSS.[3]

Star plus hack

Although Internet Explorer 7 no longer recognizes the classic star HTML hack[5], it has introduced a similar hack using selectors new to IE7:

*:first-child+html p { font-size: 5em; }

This code will be applied in Internet Explorer 7, but not in any other browser. Note that this hack only works in IE7 standards mode; it does not work in quirks mode. This hack is also supported by Internet Explorer 8's compatibility view (IE7 standards mode), but not in IE8 standards mode. Like the star HTML hack, this uses valid CSS.[3]

Child selector hack

Internet Explorer 6 and earlier do not support the "child selector" (>), allowing rules to be specified for all other browsers. For example, this rule will turn paragraph text blue in Firefox, but not in IE before version 7.[3]

html > body p { color: blue; }

Although IE7 added support for the child selector, a variation of the hack has been discovered which allows Internet Explorer 7 to be excluded as well. When an empty comment occurs immediately after the child selector, IE7 will drop the rule that follows, as will earlier versions of IE.

html >/**/ body p { color: blue; }

Negation pseudo-class hack

No version of Internet Explorer supports the CSS3 :not() pseudo-class.[6] A variation of this hack uses the :root pseudo-class, which is also unrecognized by Internet Explorer.

.yourSelector {
color: black;
} /* values for IE */

html:not([dummy]) .yourSelector {
color: red;
} /* values for Safari, Opera and Firefox */

The negation selector accepts as a parameter any type, attribute, universal, class or ID selector, or pseudo-class (excluding the negation selector itself). It then applies the following properties to all elements which do not match this argument.

body:empty hack

The :empty pseudo-class, introduced in CSS3, is supposed to select only elements which do not contain any content. However, Gecko 1.8.1 and below (used in Firefox 2.0.x and below) incorrectly selects body:empty even when the body element contains content (which it usually should). This can be taken advantage of to feed exclusive CSS rules to Firefox 2.0.x and below, along with other browsers using the same rendering engine.[3]

/* Make p elements disappear in Firefox 2.0.x and below */
body:empty p {
 display:none;
}

This hack uses valid CSS.

!important quirks

Internet Explorer 7 and below have a few quirks related to the !important declaration, which is supposed to give a value higher importance than normal.[3] IE7 and earlier accept virtually any string in place of important and process the value normally, while other browsers will ignore it. This can be used to specify values exclusively for these browsers.

/* Make text blue in IE7 and below, black in all other browsers */
body {
 color: black;
 color: blue !ie;
}

Similarly, IE7 and earlier accept non-alphanumeric characters after an !important declaration, while other browsers will ignore it.

body {
 color: black;
 color: blue !important!;
}

Both of these hacks use invalid CSS. Internet Explorer 6 and below also have a problem with !important declarations when the same property of the same element has another value specified within the same code block, without another !important declaration. This should result in the second value being overridden by the first, but IE6 and lower do not honor this.

/* Make text blue in IE6 and lower */
body {
 color: black !important;
 color: blue;
}

This hack uses valid CSS.

Dynamic properties

Between versions 5 and 7, Internet Explorer has supported a proprietary syntax for applying CSS properties which change dynamically, sometimes referred to as CSS expressions.[7] Dynamic properties are typically combined with other hacks to compensate for unsupported properties in older versions of Internet Explorer.

div {
 min-height: 300px;

 /* simulates min-height in IE6 */
 _height: expression(document.body.clientHeight < 300 ? "300px" : "auto");
}

Conditional comment

Conditional comments are conditional statements interpreted by Microsoft Internet Explorer in HTML source code.

<head>
 <title>Test</title>
 <link href="all_browsers.css" rel="stylesheet" type="text/css">
 <!--[if IE]> <link href="ie_only.css" rel="stylesheet" type="text/css"> <![endif]-->
 <!--[if lt IE 7]> <link href="ie_6_and_below.css" rel="stylesheet" type="text/css"> <![endif]-->
 <!--[if !lt IE 7]><![IGNORE[--><![IGNORE[]]> <link href="recent.css" rel="stylesheet" type="text/css"> <!--<![endif]-->
 <!--[if !IE]>--> <link href="not_ie.css" rel="stylesheet" type="text/css"> <!--<![endif]-->
</head>

Criticism

Hiding code using hacks often leads to pages being incorrectly displayed when browsers are updated. Many hacks that used to hide CSS from Internet Explorer 6 and lower no longer work in version 7 due to its improved support for CSS standards. The Microsoft Internet Explorer development team have asked that people use conditional comments instead of hacks.[8]

See also

Notes

  1. QuirksMode - CSS Hacks
  2. "Box Model Hack". http://tantek.com/CSS/Examples/boxmodelhack.html. 
  3. 3.0 3.1 3.2 3.3 3.4 3.5 3.6 "WebDevout - CSS Hacks". WebDevout. http://www.webdevout.net/css-hacks. 
  4. "IEBlog". Improving the CSS 2.1 strict parser for IE 7. Microsoft. https://blogs.msdn.com/ie/archive/2005/09/02/460115.aspx. 
  5. The IEBlog
  6. "Sitepoint CSS Reference". SitePoint. http://reference.sitepoint.com/css/pseudoclass-not. Retrieved 2009-01-07. 
  7. About Dynamic Properties
  8. IEBlog – Call to action: The demise of CSS hacks and broken pages

External links

Personal tools

Served in 0.235 secs.