Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect screen size for responsive web design?

I googled this and got a quirksmode site that gives you your screen size. Pulling up the console I see that screen.width and screen.height are directly available from the window object.

I want to do the detection in pure JavaScript to get started. Can I use this property across all devices and browsers - mobile, tablet, PC and Chrome, Safari, IE, Firefox.

I don't care about the view port changing due to re-sizing etc. Just the actual size of the screen which does not change.

I was hoping there was a single property I could check across devices and browsers.

Here is some general info by wikipedia on responsive web design.

like image 629
Zen-Lee Chai Avatar asked Jul 01 '15 13:07

Zen-Lee Chai


People also ask

How do I find my ideal screen size for responsive design?

Check the browser window from 360×640 to 1920×1080 screen resolutions. Your page should score high on all criteria throughout the entire resolution range. Your page should also work at even smaller and bigger sizes, though such extremes are less important.

How do I know if my website is responsive in different screen sizes?

Screenfly is a free tool for testing a website on different screen sizes and different devices. It's been around for a few years now, but it's still popular and does its job extremely well. Just enter your URL, pick your device and screen size from the menus and you'll see how well your website is working on it.

What are the different screen sizes for responsive design?

Small (smaller than 640px) Medium (641px to 1007px) Large (1008px and larger)

How do I find my screen size?

The size of a 16:9 screen depends on how long the screen's diagonal is, as 16:9 is merely the ratio of the screen's width to its height. If you have the screens diagonal, you can multiply that measurement by 0.872 to get the screen's width. You can also multiply the diagonal by 0.49 to get the screen's height.


1 Answers

Take a look at Get the device width in javascript

Media Queries work in js too:

if (window.matchMedia('screen and (max-width: 768px)').matches) {}

Another way would be:

var width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
like image 193
l0w_skilled Avatar answered Oct 23 '22 03:10

l0w_skilled