Content deleted Content added
Remove hatnote: no target (WP:HATNOTE) |
|||
(32 intermediate revisions by 25 users not shown) | |||
Line 1:
{{Short description|Coding technique}}
A '''CSS hack''' is a [[Computer programming|coding]] technique used to hide or show [[Cascading Style Sheets|CSS]] [[Markup language|markup]] depending on the [[Web browser|browser]], version number, or capabilities. Browsers have different interpretations of CSS behavior and different levels of support for the [[W3C]] [[World Wide Web Consortium#Standards|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.
Line 5 ⟶ 6:
=== Invalid or non-compliant CSS ===
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 <code>_width</code>) to target Internet Explorer 6—other browsers will ignore the line, allowing it to be used to write code specific to one [[Web browser|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 <code>!important</code> declaration is recognized as such with any string after the exclamation mark, e.g. <code>!ie</code>.<ref>{{cite web |title=Browser CSS hacks |author=Paul Irish |url=https://www.paulirish.com/2009/browser-specific-css-hacks/ |website=www.paulirish.com |access-date=8 June 2022 |date=2009-04-15}}</ref>
=== Unsupported CSS ===
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.
=== Conditional comments ===
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.
<
<head>
<title>Test</title>
Line 19 ⟶ 24:
<!--[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]-->
</head>
</syntaxhighlight>
===
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.
Line 29 ⟶ 34:
===Browser prefixes===
Each of the most popular browser [[Rendering (computer graphics)|rendering]] engines has its own [[vendor
==== List of prefixes ====
The following
{| class="wikitable"
|-
! Vendor Prefix !! In Use !! Layout Engine !! Created by !! Used by
|-
| <code>-ah-</code> ||
|-
| <code>-apple-</code> ||
|-
| <code>-atsc-</code> || || || [[Advanced Television Systems Committee standards]] ||
|-
| <code>-epub-</code> ||
|-
| <code>-fx-</code> ||
|-
| <code>-hp-</code> || || || [[Hewlett-Packard]]
|-
| <code>-khtml-</code> ||
|-
| <code>-moz-</code> ||
|-
| <code>-ms-</code> ||
|-
| <code>mso-</code> || || Office || Microsoft Corporation || Microsoft Office[?]
|-
| <code>-o-</code> ||
|-
| <code>-prince-</code> ||
|-
| <code>-rim-</code> || || WebKit || [[BlackBerry Limited]] || RIM Blackberry Browser
|-
| <code>-ro-</code> ||
|-
| <code>-tc-</code> || || || TallComponents || TallComponents
|-
| <code>-wap-</code> ||
|-
| <code class=nowrap>-webkit-</code> ||
|-
| <code>-xv-</code> ||
|}
==== Example ====
<
/* Cross-browser css3 linear-gradient */
Line 97 ⟶ 102:
}
</syntaxhighlight>
====
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 <
=== Feature
==== JavaScript feature detection ====
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 <code>html</code> element, allowing for CSS rules such as <
==== Feature queries ====
A new feature known as feature queries was introduced in [[CSS|CSS3]], allowing the detection of specific functionality within the CSS (without requiring the use of a JavaScript library for [[feature detection (web development)|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 <code>and</code>, <code>or</code>, and <code>not</code>. Obviously, <
header {
display: block;
}
@supports (display:
header {
display:
}
}
</syntaxhighlight>
===
While JavaScript feature detection and <
==
<references/>
==External links==
* [https://web.archive.org/web/20150311021026/http://browserstrangeness.bitbucket.org/css_hacks.html Browser Strangeness]
* [http://browserhacks.com/ browserhacks.com]
* [https://web.archive.org/web/20120212190720/http://qooxdoo.org/documentation/general/webkit_css_styles Safari/Webkit (webkit) prefix filters] refix filters]
* [https://developer.mozilla.org/Special:Tags?tag=CSS:Mozilla+Extensions Mozilla (moz) prefix filters]
* [https://web.archive.org/web/20161221232725/http://www.opera.com/docs/specs/opera9/css/index.dml Opera (wap) prefix filters]
* [https://web.archive.org/web/20060804012032/http://centricle.com/ref/css/filters/ CSS Filters] – A fairly complete table of CSS hacks which show and hide rules from specific browsers.
* [https://web.archive.org/web/20070715175654/http://www.lipfert-malik.de/webdesign/tutorial/bsp/css-weiche-filter.html Filters and Cross-Over] – CSS filters. Parsing errors marked red.
* [http://rafael.adm.br/css_browser_selector
* [https://web.archive.org/web/20110720143842/http://www.positioniseverything.net/articles/cc-plus.html
* [https://stackoverflow.com/questions/28417056/how-to-target-only-ie-any-version-within-a-stylesheet How to target only IE (any version) within a stylesheet?] – discussion on [[StackOverflow]]
▲{{Use dmy dates|date=September 2011}}
* [https://stackoverflow.com/questions/11173106/apply-style-only-on-ie Apply style ONLY on IE] – discussion on [[StackOverflow]]
* [https://www.rareprogrammer.com/css-comments CSS Comments] - How to add comments in CSS
{{DEFAULTSORT:Css Filter}}
|