7

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"> 
Dragonseer
  • 2,874
  • 7
  • 29
  • 42
  • 1
    Do you have a viewport set..? `` – Josh Crozier Sep 25 '13 at 00:17
  • Now that you mention it, I got the below markup from a tutorial. Not sure what it does - the tutorial just said it was necessary for iPhones. Removing it does rescale the website to the desktop view. – Dragonseer Sep 25 '13 at 00:18
  • I updated my question after JoshC's response; see above edit. How does the width=device-width determine the resolution from the physical width of the device? How can I calculate the viewport resolution of my phone using its dimensions? – Dragonseer Sep 27 '13 at 20:31

2 Answers2

6

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

Community
  • 1
  • 1
Dragonseer
  • 2,874
  • 7
  • 29
  • 42
2

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 :)

Riskbreaker
  • 4,621
  • 1
  • 23
  • 31
  • Thanks Riskbreaker. I've updated my questions a bit after realizing that I had the viewport markup in my HTML. Now I'm trying to determine how to calculate the resulting resolution from a devices physical size when using the viewport metatag. – Dragonseer Sep 25 '13 at 04:29