Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

CSS hack

From Wikipedia, the free encyclopedia
(Redirected fromCSS hacks)
Coding technique
This article is about a CSS technique. Not to be confused with the proprietary Microsoft-specific CSS property.

ACSS hack is acoding technique used to hide or showCSSmarkup depending on thebrowser, version number, or capabilities. Browsers have different interpretations of CSS behavior and different levels of support for theW3Cstandards. 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.

Types of hacks

[edit]

Invalid or non-compliant CSS

[edit]

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 onebrowser.

Similar CSS hacks involve inducing syntax errors like asterisks, missing whitespace, and CSS comments around property names. Additionally, inInternet Explorer 6 and 7, the!important declaration is recognized as such with any string after the exclamation mark, e.g.!ie.[1]

Unsupported CSS

[edit]

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

[edit]
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.

<head><title>Test</title><linkhref="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[]]><linkhref="recent.css"rel="stylesheet"type="text/css"><!--<![endif]--><linkhref="not_ie.css"rel="stylesheet"type="text/css"><!--<![endif]--></head>

Critics

[edit]

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.

Alternatives

[edit]

Browser prefixes

[edit]

Each of the most popular browserrendering engines has its ownvendor prefix for experimental properties. However, due to the proliferation of these properties in live code, the browservendors have begun moving away from this practice in favor of feature flags.[2]

List of prefixes

[edit]

The following are a list of prefixes from various layout engines:

Vendor PrefixIn UseLayout EngineCreated byUsed by
-ah-YesFormatterAntenna HouseAntenna House Formatter
-apple-YesWebKitApple Inc.Apple Safari 2.0, Opera Widgets,WebKit-Based Browsers (as legacy prefix)
-atsc-Advanced Television Systems Committee standards
-epub-YesWebKitEPUB Working GroupChromium /Google Chrome, WebKit-Based Browsers
-fx-YesSun Microsystems (now acquired byOracle Corporation)JavaFX applications
-hp-Hewlett-Packard (nowHP Inc. andHewlett Packard Enterprise)
-khtml-YesKHTML / WebKitKDEKDEKonqueror / Apple Safari 1.1 through Safari 2.0, WebKit-Based Browsers (as a legacy prefix)
-moz-YesGeckoMozilla FoundationGecko-Based Browsers[?], Mozilla Firefox
-ms-YesTrident / MSHTMLMicrosoft CorporationMicrosoft Internet Explorer
mso-OfficeMicrosoft CorporationMicrosoft Office[?]
-o-YesPrestoOpera SoftwareOpera desktop Browser up to version 12.16,Opera Mini, andOpera Mobile up to version 12.1,Nintendo DS &Nintendo DSi Browser, Nintendo Wii Internet Channel
-prince-YesPrinceYesLogicYesLogic Prince
-rim-WebKitBlackBerry LimitedRIM Blackberry Browser
-ro-YesMARTHARealObjectsRealObjects PDFreactor
-tc-TallComponentsTallComponents
-wap-YesPrestoThe WAP ForumOpera Desktop Browser and Opera Mobile, The WAP Forum
-webkit-YesWebKit/BlinkApple 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, NokiaSymbian Browser 7.0 and later (WebKit), Blackberry Browser 6.0 and later (WebKit).
-xv-NoPrestoOpera SoftwareOpera Desktop Browser for Windows 2000/XP

Example

[edit]
/* Cross-browser css3 linear-gradient */.linear-gradient{/* Gecko browser (Firefox) */background-image:-moz-linear-gradient(top,#D7D0%,#068100%);/* Opera */background-image:-o-linear-gradient(top,#D7D0%,#068100%);/* older Webkit syntax */background-image:-webkit-gradient(linear,lefttop,leftbottom,color-stop(0,#D7D),color-stop(1,#068));/* Webkit (Safari, Chrome, iOS, Android) */background-image:-webkit-linear-gradient(top,#D7D0%,#068100%);/* W3C */background-image:linear-gradient(tobottom,#D7D0%,#068100%);}

Limitation

[edit]

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@supports feature queries.

Feature deletion

[edit]

JavaScript feature detection

[edit]

MultipleJavaScript 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 thehtml element, allowing for CSS rules such as.cssgradients.header.

Feature queries

[edit]

A new feature known as feature queries was introduced inCSS3, allowing the detection of specific functionality within the CSS (without requiring the use of a JavaScript library forfeature 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 withand,or, andnot. Obviously,@supports rules will only work on browsers that support@supports.

header{display:block;}@supports(display:flex){header{display:flex;}}

Script polyfills

[edit]

While JavaScript feature detection and@supports rules can help to target browsers that require fallback functionality, they will not address bugs in specific browsers or enable that advanced functionality.Polyfills, scripts that make behavior consistent across all browsers, can be used to add support for new CSS rules (for example,media queries in IE 8) as well as fix bugs in specific browsers. Since polyfills add or fix functionality in browsers that do not have it, they serve a different purpose than feature queries, but can be used in combination with them.

References

[edit]
  1. ^Paul Irish (15 April 2009)."Browser CSS hacks".www.paulirish.com. Retrieved8 June 2022.
  2. ^"Vendor Prefix".Mozilla Developer Network. Retrieved12 October 2016.

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=CSS_hack&oldid=1281533301"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp