A CSS 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 hacks are sometimes used to achieve consistent layout appearance in multiple browsers that do not have compatible rendering. Most of these hacks do not work in modern versions of the browsers, and other techniques, such as feature support detection, have become more prevalent.
Due to quirks in the interpretation of CSS by various browsers, most CSS hacks involve writing invalid CSS rules that are interpreted only by specific browsers, or relying on bugs in specific browsers. An example of this is prefixing rules with an underscore (as in _width
) to target Internet Explorer 6—other browsers will ignore the line, allowing it to be used to write code specific to one browser.
Similar CSS hacks involve inducing syntax errors like asterisks, missing whitespace, and CSS comments around property names. Additionally, in Internet Explorer 6 and 7, the !important
declaration is recognized as such with any string after the exclamation mark, e.g. !ie
.[1]
Although newer CSS rules are correct by current standards, they are ignored by older browsers as "invalid". By writing old rules followed by newer rules that cancel out or modify the old ones, it is possible to only activate certain rules on older browsers.
See main article: Conditional comment. Prior to version 10, Internet Explorer supported a special comment syntax that would allow blocks of HTML to be read only by specific versions of the browser. These comments are mostly used to provide specific CSS and JavaScript workarounds to older versions of the browser. No other browsers interpreted these comments or offered similar functionality.
The following are examples of the different syntax for these comments.
Hiding code using hacks often leads to pages being incorrectly displayed when browsers are updated. These hacks can lead to unexpected behavior in newer browsers that may interpret them differently than their predecessors. Since Internet Explorer 6 and 7 have fallen out of use, CSS hacks have declined as well. Modern methods of feature targeting are less fragile and error-prone.
Each of the most popular browser rendering engines has its own vendor prefix for experimental properties. However, due to the proliferation of these properties in live code, the browser vendors have begun moving away from this practice in favor of feature flags.[2]
The following are a list of prefixes from various layout engines:
Vendor Prefix | In Use | Layout Engine | Created by | Used by | |
---|---|---|---|---|---|
-ah- | Yes | Formatter | Antenna House | Antenna House Formatter | |
-apple- | Yes | WebKit | Apple Inc. | Apple Safari 2.0, Opera Widgets, WebKit-Based Browsers (as legacy prefix) | |
-atsc- | |||||
-epub- | Yes | WebKit | Chromium / Google Chrome, WebKit-Based Browsers | ||
-fx- | Yes | Sun Microsystems (now acquired by Oracle Corporation) | JavaFX applications | ||
-hp- | Hewlett-Packard (now HP Inc. and Hewlett Packard Enterprise) | ||||
-khtml- | Yes / yes | KHTML / WebKit | KDE | KDE Konqueror / Apple Safari 1.1 through Safari 2.0, WebKit-Based Browsers (as a legacy prefix) | |
-moz- | Yes | Gecko | Gecko-Based Browsers[?], Mozilla Firefox | ||
-ms- | Yes | Trident / MSHTML | Microsoft Internet Explorer | ||
mso- | Office | Microsoft Corporation | Microsoft Office[?] | ||
-o- | Yes | Presto | Opera desktop Browser up to version 12.16, Opera Mini, and Opera Mobile up to version 12.1, Nintendo DS & Nintendo DSi Browser, Nintendo Wii Internet Channel | ||
prince- | Yes | Prince | YesLogic | YesLogic Prince | |
-rim- | WebKit | RIM Blackberry Browser | |||
-ro- | Yes | MARTHA | RealObjects | RealObjects PDFreactor | |
-tc- | TallComponents | TallComponents | |||
-wap- | Yes | Presto | Opera Desktop Browser and Opera Mobile, The WAP Forum | ||
-webkit- | yes | WebKit/Blink | Apple Inc. (WebKit)/Google Inc. (Blink) | Apple Safari & Safari for iOS (WebKit), Chromium / Google Chrome desktop and mobile (Blink), Opera desktop and mobile from version 14 (Blink), Android browser (Blink), Nokia MeeGo Browser 8.5, Nokia Symbian Browser 7.0 and later (WebKit), Blackberry Browser 6.0 and later (WebKit). | |
-xv- | No | Presto | Opera Software | Opera Desktop Browser for Windows 2000/XP |
/* Cross-browser css3 linear-gradient */.linear-gradient
Vendor prefixes were designed for features that were under development, meaning that the syntax may not even be final. Also, adding a rule for each browser's implementation of a function does not scale well when you want to support many browsers. Consequently, the major browser vendors are moving away from vendor prefixes in favor of other methods such as
Multiple JavaScript libraries exist to detect what features are available in a particular browser so that CSS rules can be written to target them. Libraries such as Modernizr add classes to the html
element, allowing for CSS rules such as
A new feature known as feature queries was introduced in CSS3, allowing the detection of specific functionality within the CSS (without requiring the use of a JavaScript library for feature detection). This new directive can be used to check for the support or lack of support for a specific feature, and checks can be combined with and
, or
, and not
. Obviously,
@supports (display: flex)
While JavaScript feature detection and