In media queries, I've seen max-width
, min-width
, max-device-width
and min-device-width
and orientation
.
From a JavaScript standpoint, are these referring to document.body.clientWidth
? Or window.outerWidth
? Also I see there is document.body.offsetWidth
.
Is there a resource out there that list out all the valid css media query parameters along with what JavaScript attributes that match up with them?
So, you want a list of all the valid css media query parameters equivalent in JavaScript.
Let's try to do it, relying on the media queries W3C specification.
It doesn't seem to be possible to retrieve the media type (screen, print, etc) directly with a JavaScript property/method, so you must rely on workarounds, scripts or plugins.
I've found:
window.innerWidth
/ document.body.clientWidth
/ document.documentElement.clientWidth
(see below)
window.innerHeight
/ document.body.clientHeight
/ document.documentElement.clientHeight
(see below)
screen.width
screen.height
window.orientation
(see below)
screen.colorDepth
screen.pixelDepth
/ window.devicePixelRatio
(see below)
Given the differences between browsers, you need a function to get width
and height
. Some time ago i found this snippet (can't remember where) that works cross-browser:
var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0];
var width = w.innerWidth||e.clientWidth||g.clientWidth;
var height = w.innerHeight||e.clientHeight||g.clientHeight;
the ratio of the value of the ‘width’ media feature to the value of the ‘height’ media feature
width
/ height
(see above)
the ratio of the value of the ‘device-width’ to the value of the ‘device-height’
screen.width
/ screen.height
the density of the pixels of the output device. The ‘dpi’ and ‘dpcm’ units describe the resolution of an output device, i.e., the density of device pixels.
So it's not the screen size (width x height) as many think!
var resolution = window.devicePixelRatio||screen.pixelDepth||screen.colorDepth;
screen.pixelDepth
is for Firefox only, and you can find window.devicePixelRatio
compatibility on quirksmode.org.
Here i read that screen.colorDepth
is ok as a fallback.
the number of bits per pixel in a monochrome frame buffer. If the device is not a monochrome device, the output device value will be 0
if ( screen.colorDepth == 2) {
var monochrome = /* retrieve resolution here, see above */;
}else{
var monochrome = 0;
}
is ‘portrait’ when the value of the ‘height’ media feature is greater than or equal to the value of the ‘width’ media feature. Otherwise ‘orientation’ is ‘landscape’.
if ( width > height ) {
var orientation = 'landscape';
}else{
var orientation = 'potrait';
}
The window.orientation
property is not supported by all browsers, and it returns a numeric value, so it's not directly related to orientation as intended by W3C. Check this answer on SO for more information.
I couldn't find a way to detect the following media features with JavaScript (and i don't think it's possible):
screen.availHeight
= height of the screen minus interface features (such as the taskbar)screen.availWidth
= width of the screen minus interface features (such as the taskbar)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With