Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actual Mobile Resolution

Tags:

html

css

mobile

I'm new to HTML5 & CSS3 so I'm working on a simple project to learn it better. I recently discovered responsive design so I am creating a page that scales to my phone. I using media queries, I created two states, one for screens 640px (mobile) and smaller and one for screen 641px and bigger (desktop). The site works fine, showing the correct styles on my desktop when I scale the window and showing the correct style when I view it on my phone.

However, after running the site, I decided to check the resolution of my HTC One - it's 1080p. My phone displays the 640px site regardless of orientation. I'm wondering why this happens; the phone's higher resolution shouldn't trigger the mobile state, but it does. I'm thinking I might be confused on how resolutions actually work.

So, how do I determine the true resolution of phones (and tablets) so that I can create appropriately scaled sites for them?

//Mobile
@media screen and (max-width: 640px) {
    ...
}

//Desktop
@media screen and (min-width: 641px) {
    ...
}

Edit Thanks to JoshCs comment below, I realized that I had the viewport meta tag in use from a tutorial. I'd like to revise my question to ask: how does the viewport tag determine what resolution to display for any given device (how do I determine the viewported resolution based on the dimensions of a device)?

<meta name="viewport" content="width=device-width; initial-scale=1.0"> 
like image 349
Dragonseer Avatar asked Sep 25 '13 00:09

Dragonseer


People also ask

What is normal mobile resolution?

Screen Resolution Stats Worldwide: Jan 2020 – Jan 2021 According to the Worldwide Screen Resolution Stats (Jan 2020 – Jan 2021), the most commonly used resolutions across mobile, desktop, and tablet are: 1920×1080 (8.89%)

What resolution is 320px?

This means the screen is 1440 pixels across, so it has a device-width of 1440px. Most mobile phones have a device-width of 480px or lower, including the popular iPhone 4 (with device-width: 320px), despite it technically having a 640 x 960 resolution.


2 Answers

A CSS pixel is an abstract concept that is independent from actual pixels on screen. Specifically, it's a density independent pixel. Depending on the devices physical pixel density, a logical CSS pixel scales in size relative to physical pixels. For example, in many devices, a screen width of 640 has the CSS pixel ratio of 2. So two pixels are used to draw a single CSS pixel.

When you use the viewport metatag, you instruct the browser to scale the CSS pixel using the pixel ratio.

You can determine the device's pixel ration in webkit browsers using window.devicePixelRatio. That couple with the physical pixel count can be used to determine the viewport resolution.


References

  • How can you get the CSS pixel / device pixel ratio?
  • http://www.quirksmode.org/blog/archives/2012/07/more_about_devi.html
  • What is Device Pixel Ratio for?
  • what exactly is device pixel ratio?
  • http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density
  • http://gamon.org/blog/2013/04/26/css-pixel-ratios-or-why-all-mobile-sites-are-320-pixels/
like image 116
Dragonseer Avatar answered Oct 27 '22 15:10

Dragonseer


This is how i have it set up:

<meta name="viewport" content="width=device-width">

CSS:

/********** Desktop - 920px breakpoint ***************************/
@media screen and (max-width: 60em) {}

/*iPAD SUPPORT=====================================--*/
@media (max-width: 56.250em) {}

/*NEXUS SUPPORT=====================================--*/
@media (max-width: 44.063em) {}

/*iPhone SUPPORT=====================================--*/
@media screen and (max-width: 22.500em) {}

You can use jquery to change port base on view size:

// Check for device screen size
if($.mobile.media("screen and (max-device-width: 640px)")) {
    // Change viewport for smaller devices
    $('meta[name=viewport]').attr('content','width=device-width, initial-scale=1');
}

Hope this helps :)

like image 43
Riskbreaker Avatar answered Oct 27 '22 15:10

Riskbreaker