jQuery Mobile adds classes to the HTML
element that mimic browser orientation and common min/max-width CSS media queries. These classes are updated on load, resize and orientationchange, allowing you to key off these classes in your CSS, to create responsive layouts - even in browsers that don't support media queries!
The HTML element will always have a class of either "portrait"
or "landscape"
, depending on the orientation of the browser or device. You can utilize these in your CSS like this:
.portrait {
/* portrait orientation changes go here! */
}
.landscape {
/* landscape orientation changes go here! */
}
By default, we create min and max breakpoint classes at the following widths: 320
,480
,768
,1024
. These translate to classes that look like this: "min-width-320px"
, "max-width-480px"
, and are meant to be used as a replacement of (or in addition to) the media query equivalents they mimic.
.myelement {
float: none;
}
.min-width-480px .myelement {
float: left;
}
Many plugins in jQuery Mobile leverage these width breakpoints. For example, form elements float beside their labels when the browser is wider than 480 pixels. The CSS to support this behavior for form text inputs looks like this:
label.ui-input-text {
display: block;
}
.min-width-480px label.ui-input-text {
display: inline-block;
}
To utilize width breakpoints of your own, jQuery Mobile exposes the $.mobile.addResolutionBreakpoints
function, which accepts either a single number or array of numbers that will be added to the min/max breakpoints whenever they apply.
//add a min/max class for 1200 pixel widths
$.mobile.addResolutionBreakpoints(1200);
//add min/max classes for 1200, and 1440 pixel widths
$.mobile.addResolutionBreakpoints([1200, 1440]);
jQuery Mobile provides a function that allows you to test whether a particular CSS Media Query applies. Simple call $.mobile.media()
and pass a media type or query. If the browser supports that type or query, and it currently applies, the function will return true. If not, it'll return false.
//test for screen media type
$.mobile.media("screen");
//test a min-width media query
$.mobile.media("screen and (min-width: 480px)");
//test for iOS retina display
$.mobile.media("screen and (-webkit-min-device-pixel-ratio: 2)");