Tiffany B. Brown, Webinista, Inc.
CSS features and their associated JavaScript APIs that let us apply styles or behaviors when a particular condition is met.
@rules@media@supports@viewport (not really conditional)@media: a recapSet styles for a particular media type.
@media print {
body { font-size: 10pt }
}
@media screen, print {
body { line-height: 1.2 }
}
@media and media queriesApplies styles for particular media features.
Safari |
||||||
|---|---|---|---|---|---|---|
| 9+ | 3.5+ | 4.0 | 3.2* | 9.5** | 2.1+ | 7.0 |
*Safari had full support in desktop as of version 4.0.
**Opera gained full support in mobile in version 10. (Source: CanIUse.com)
@media [only | not] <media_type> [and] (expression){
selector{
property: value;
}
}
@media screen and (max-width: 500px){header{position:fixed;}}
@media only print@media not (aspect-ratio: 4/3)@media (min-width:400px) and (device-aspect-ratio: 16/9)@media screen and (min-width:400px),
print and (max-width: 8.5in)<link media="(device-width:480px)" rel="stylesheet"… >
<video> element<video><source src="smallmovie.webm" media="(min-width:480px)"><source src="bigmovie.webm" media="(min-width:1900px)"><video>
Does not work in Internet Explorer 10.
window.matchMedia() APImatchMedia() syntaxArgument must be a valid @media rule.
matchMedia("screen and (min-width:400px)");matchMedia("(orientation: landscape)");Returns a MediaQueryList object.
media: returns list of queries or not all if the argument isn't valid.matches: returns true if the state of the rendered document matches the media query, false otherwiseaddListener(): Adds a media query listener.removeListener(): Removes the media query listener.function mqHandler(mq){if( mq.matches ){// do something}}var mq = matchMedia("(orientation:landscape)");mq.addListener( mqHandler );
matchMedia() browser support Safari |
||||||
|---|---|---|---|---|---|---|
| 10+ | 6+ * | 9+ * | 5.1 | 12.1 | 3+ | 10 |
*Available in Chrome for Android as of version 25 and Firefox for Android as of version 19. (Source: CanIUse.com).
@supports worksand or or.notand and or conditions.@supports syntax@supports [not] ( property: value-to-test ){
/* CSS rules to apply */
}
@supports syntax example@supports ( transform: rotateX(45deg) ){
#myobj{
transform: rotateX(45deg);
}
}
@supports example using not@supports not ( transform: rotateX(45deg) ){
#myobj{
transform: skewX(30deg);
}
}
@supports using and & or@supports (
( transform: scale(1) ) or ( -x-transform: scale(1) )
) and (
( transition: transform 500ms linear )
or ( -x-transition: transform 500ms linear )
)
(Where -x- is a vendor prefix.)
window.CSS.supports()@supportswindow.supportsCSS();CSS object attached to the window with a supports() method.CSS.supports() syntaxvar hasProp = CSS.supports('property','value');if( hasProp ){// do something}
Returns true if both property is supported, and the value is valid and supported. Returns false otherwise.
@supports & CSS.supports() browser support Safari |
||||||
|---|---|---|---|---|---|---|
| - | 20.0a2+* | - | - | 12.1 | - | - |
* Firefox support is available in version 17+, but behind a flag. Turned on by default in Firefox 20+ (Aurora and Firefox nightly builds). (Source: CanIUse.com).
@supports support!!window.CSSRule.SUPPORTS_RULE: tests whether support is available at all'CSS' in window: tests whether the API is available as the CSS object.'supportsCSS' in window: tests whether the API is available using supportsCSS() instead<meta name="viewport">Left: no viewport set. Right width=device-width
@viewport@viewport syntax@viewport{descriptor: value;}
Used inside of a media query to reset the viewport size or zoom level.
@viewport syntax@media screen and (device-aspect-ratio: 8/5){@viewport{zoom: 0.5}}
@viewport descriptorsMostly the same values you use with <meta name="viewport">.
@viewport browser support Safari |
||||||
|---|---|---|---|---|---|---|
| 10+* | - | - | - | 11+* | - | - |
Sizes relative to the viewport's dimensions.
vw: 1% of viewport widthvh: 1% viewport heightvmin: 1% of viewport’s smaller dimensionvmax: 1% of viewport’s larger dimension Safari |
||||||
|---|---|---|---|---|---|---|
| 9.0+* | 19+ | 20+** | 6+ | -† | - | 10 |
* Partial support in IE9. **Chrome for Android added support in version 25. †Presto-based Opera. Oprium will inherit Chrome's support. Source