If I use max-device-width
does that include landscape mode?
Say I use it to detect iPhone 4, should I use max-device-width: 640px
or 960px
? I need to get 3 resolutions: 480x320
, 960x640
and 854x480
. What would be the best way to query for them using @media
My take was:
@media screen and (max-device-width: 480px) {}
@media screen and (min-device-width: 481px) and (max-device-width: 854px) {}
@media screen and (min-device-width: 855px) and (max-device-width: 960px) {}
Here is some Css to help you:
Have a read through this link it should solve your problem
http://davidbcalhoun.com/2010/using-mobile-specific-html-css-javascript
The
device-width
anddevice-height
media query features (along with theirmin/max
prefixes) essentially provide the screen resolution of a mobile device.On an iPhone, regardless of the generation or presence of a Retina display
device-width
:320px
in portrait mode480px
in landscape modeUsing JavaScript, we can get the viewport's width:
Media queries targeting 320px screens still work on iPhone 4, due to Mobile Safari utilizing a 'device to pixel ratio' density of 2.
For example, when the iPhone 4 browser viewport is equal to the device width, 1 CSS pixel actually translates to 2x2 device pixels.
This is largely good news for Mobile Web Developers, as very little needs to be done to mobile stylesheets for iPhone 4 and Retina displays — since device pixels are translated to CSS pixels automatically.
On a related note, iOS 4 (and iPad with iOS 3.2) introduced orientation to help with detecting orientation change. As soon as the iPhone/iPad detects the orientation changing, it will execute any CSS matching the respective media query.
Consider the following orientation detection example:
The same functionality can be accomplished (or combined with) using
width
anddevice-width
to trigger portrait vs. landscape modes:The orientation media feature is
portrait
when the value of theheight
is greater than or equal to the value of thewidth
— otherwiseorientation
islandscape
.As illustrated,
max-width
andmin-width
are updated when you rotate the screen — somax-width
in portrait mode becomesmax-height
in landscape mode.For reference, here's the screen size and resolutions of the iPhone and iPad: