Difference between aspect-ratio and device-aspect-ratio in CSS media queries - css

What is the difference in simple terms between aspect-ratio and device-aspect-ratio?

For everyone who is confused about the difference between targeted display area and output device regarding the aspect-ratio:
targeted display area
Aspect ratio of your browser window or the area your website is displayed on
(a special case would for example be an embedded website)
output device
Physical aspect ratio of the screen. E.g. of your smartphone or desktop display
As smartphones and tablets usually display apps in fullscreen mode only, aspect-ratio and device-aspect-ratio are the same. On a desktop computer this surely is not always the case as the user can resize the browser window and thus the aspect-ratio changes.
I hope it helps.

A functional difference on mobile is that having the soft keyboard come up changes aspect-ratio but does not change device-aspect-ratio.

aspect-ratio
Describes the aspect ratio of the targeted display area of the output device. This value consists of two positive integers separated by a slash ("/") character. This represents the number of horizontal pixels over the number of vertical pixels.
Source.
device-aspect-ratio
Describes the aspect ratio of the output device. This value consists of two positive integers separated by a slash ("/") character. This represents the number of horizontal pixels over the number of vertical pixels.
Source.

aspect-ratio measures viewport area.
device-aspect-ratio measures device screen area.

ASPECT RATIO
The viewport aspect ratio or device aspect ratio is the ratio of the width
to the height. So, if a screen were 1,000 pixels wide and 500 pixels high,
the device-aspect-ratio would be 2:1, because 1,000 is twice 500.
The ratios of screens vary widely, even though at first glance they pretty
much all just look like a similar rectangle.
Common monitor aspect ratios are 16:9 (such as 1920 × 1080 or 1366 ×
768 pixels) or 16:10 (1280 × 800). The iPhone 3 and 4S are 3:2 (480 × 320
and 960 × 640) and the iPhone 5 is 16:9 (1136 × 640). Android phones
are commonly 4:3, 3:2, 16:10, or 16:9.
Examples:
#media only screen and (device-aspect-ratio: 16/9)
{ ... }
#media only screen and (min-device-aspect-ratio:
1920/1080) { ... }

Related

How does CSS media queries based on pixels work with modern smart phones?

Consider a smart phone with screen resolution 1440 x 2960 pixels. Now if I do a CSS mobile query like #media(max-width: 500px), then even though it is a mobile device it won't satisfy the media query.
But it does. Consider this website Loruki, it has the same media query of max-width of 500px, yet it works like a mobile based web page when opened from 1440 x 2960 device.
How does pixel and media query work?
You are confusing screen resolution with screen width
i.e. A phone's screen width is usually around 200 to 500 CSS pixels.
This famous blog post explains why there was the need for this being introduced: a pixel is not a pixel
Medium also has an excellent explanatory article on the topic.
For high resolutions screens, the so-called device-to-pixel ratio is greater than 1. To calculate the CSS pixel width, this is the formula:
CSSPixelWidth = DevicePixelWidth / DevicePixelRatio

Responsive web design breakpoint in inches?

Because of increasing pixel density, especially in handheld/mobile devices but in laptops as well, does it make sense to define breakpoints for responsive CSS using inches?
For example, if I have an existing mobile-first layout by default and want to separately specify styles for a 15.6 inch laptop and larger displays, does it make sense to use #media (min-width: 13in)? (15.6in diagonal screen = 13in wide). Will this cause any problems, is it well supported? Intuitively it makes more sense to define overall layout based on the absolute size of the screen than on the number of pixels.
Unfortunately, it turns out that the CSS specification defines inches and millimeters based on pixels. In CSS, 1in = 2.54cm = 96px, so this will display as 1 physical inch only on screens with 96 ppi, but for example, the iPhone X has 463 ppi and if you specify something in CSS to be width=1in; the physical screen display will be only 0.2 inches wide!

CSS media feature "resolution" defining pixel density?

'Resolution' seems to be often used as a term to describe viewport dimensions/device pixels expressed in terms of a x b:
e.g. 960px x 640px (for iPhone 4)
However from what I understand that's a bit of a misappropriation as used in media queries, at least, resolution denotes the density of pixels of an output device.
Can I confirm that the media feature 'resolution' is essentially expressing pixel density?
i.e. the diagonal pixel resolution of the display divided by diagonal size (in inches)
So, taking the example of the iPhone 4 again, the resolution would be defined as 330 ppi? (or 330dpi)
I'm essentially interested to know whether the resolution feature could be used to target a device(s) with specific pixel density.
In Brian LePore's
article he suggests mobile devices round the actual dpi value to "120 DPI for a low density screen, 160 DPI for a medium density screen, 240 DPI for high density, and finally 320 DPI for extra high density".
Is this correct and would that mean that you can't actually target a specific dpi?
i.e.
#media screen and (resolution: 330dpi) {}
and
#media screen and (resolution: 311dpi) {}
will ultimately both be treated as /rounded to
#media screen and (resolution: 320dpi) {}
Yes, resolution is definitely expressing pixel density.
If you want to be as targeted as possible for iPhones, you might try using several of the available queries, and setting the values specifically to iPhone specs.
iPhone 5 would be something like this:
#media screen
and (-webkit-min-device-pixel-ratio : 2)
and (device-aspect-ratio : 40/71)
and (device-height : 568px)
and (device-width : 320px)
You can test media queries of different devices by going to the page http://pieroxy.net/blog/pages/css-media-queries/test-features.html on that device. The only strange result I'm getting is that it doesn't return a resolution value for iPhone 5. However, I'd be surprised if the above query targeted anything other than iPhone 5. (Sorry I don't know more specifics about resolution to answer your second question.)
More info at:
MDN: Media Queries
iPhone 5 CSS media query (device-aspect-ratio for iPhone)
Media features for common devices

CSS pixel and optical sizes with new screen resolutions (such as Retina)

I am looking to make my websites appear the same size on retina and other higher resolution screens that they do on standard screens. That is to say make them optically look the same but with more detail on the higher resolution screens.
So if we had a screen with four times the number of pixels per inch then I would want the height and width of elements to be twice the normal CSS pixel measurements as well as doubling the font size.
I looked into this and it appears that the solution detects the DPI and then loads different CSS.
#media
(-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi) {
/* Retina-specific stuff here */
}
The thing is these screens all have different DPIs.
iPhone 4/4S and iPod Touch (4th generation) -- 326
iPad (3rd)/4th generation) -- 264
MacBook Pro with Retina Display 15" -- 220
MacBook Pro with Retina Display 13" -- 227
So if we had a an element with a height of say 24px. I would like it to adjust its height to accurately fit whatever the pixel ratio is. IE. do the Maths and do it for all elements.
You leave the image dimensions untouched, you just give it the appropriate source.
You can also put different queries in, see here:
http://css-tricks.com/snippets/css/retina-display-media-query/
I would recommend that you read about both dpi and dppx. When you look at Retina displays you need to think about both CSS-pixels and physical pixels, as I'm sure you know.
If you want to have the componenets of the website appear in the same physical size, then I think dpi is the way to go but and add different CSS for the different dpi levels you want to cover bu if you want to differentiate between Retina and normal display you need differentiate between dpi and dppx (dots-per-physcial-inch)
These articles both helped me:
https://developer.mozilla.org/en-US/docs/Web/CSS/resolution
http://drewwells.net/blog/2013/working-with-dppx/

what exactly is device pixel ratio?

this is mentioned every article about mobile web, but nowhere I can found an explanation of what exactly does this attribute measure.
Can anyone please elaborate what does queries like this check?
#media only screen and (-webkit-min-device-pixel-ratio: 1.5),
only screen and (min--moz-device-pixel-ratio: 1.5),
only screen and (-o-device-pixel-ratio: 3/2),
only screen and (min-device-pixel-ratio: 1.5) {
//high resolution images go here
}
Short answer
The device pixel ratio is the ratio between physical pixels and logical pixels. For instance, the iPhone 4 and iPhone 4S report a device pixel ratio of 2, because the physical linear resolution is double the logical linear resolution.
Physical resolution: 960 x 640
Logical resolution: 480 x 320
The formula is:
Where:
is the physical linear resolution
and:
is the logical linear resolution
Other devices report different device pixel ratios, including non-integer ones. For example, the Nokia Lumia 1020 reports 1.6667, the Samsumg Galaxy S4 reports 3, and the Apple iPhone 6 Plus reports 2.46 (source: dpilove). But this does not change anything in principle, as you should never design for any one specific device.
Discussion
The CSS "pixel" is not even defined as "one picture element on some screen", but rather as a non-linear angular measurement of viewing angle, which is approximately of an inch at arm's length. Source: CSS Absolute Lengths
This has lots of implications when it comes to web design, such as preparing high-definition image resources and carefully applying different images at different device pixel ratios. You wouldn't want to force a low-end device to download a very high resolution image, only to downscale it locally. You also don't want high-end devices to upscale low resolution images for a blurry user experience.
If you are stuck with bitmap images, to accommodate for many different device pixel ratios, you should use CSS Media Queries or the HTML picture Element to provide different sets of resources for different groups of devices. Combine this with nice tricks like background-size: cover or explicitly set the background-size to percentage values.
Example
#element { background-image: url('lores.png'); }
#media only screen and (min-device-pixel-ratio: 2) {
#element { background-image: url('hires.png'); }
}
#media only screen and (min-device-pixel-ratio: 3) {
#element { background-image: url('superhires.png'); }
}
This way, each device type only loads the correct image resource. Also keep in mind that the px unit in CSS always operates on logical pixels.
A case for vector graphics
As more and more device types appear, it gets trickier to provide all of them with adequate bitmap resources. In CSS, media queries is currently the only way, and in HTML5, the picture element lets you use different sources for different media queries, but the support is still not 100 % since most web developers still have to support IE11 for a while more (source: caniuse).
If you need crisp images for icons, line-art, design elements that are not photos, you need to start thinking about SVG, which scales beautifully to all resolutions.
Device Pixel Ratio == CSS Pixel Ratio
In the world of web development, the device pixel ratio (also called CSS Pixel Ratio) is what determines how a device's screen resolution is interpreted by the CSS.
A browser's CSS calculates a device's logical (or interpreted) resolution by the formula:
For example:
Apple iPhone 6s
Actual Resolution: 750 x 1334
CSS Pixel Ratio: 2
Logical Resolution:
When viewing a web page, the CSS will think the device has a 375x667 resolution screen and Media Queries will respond as if the screen is 375x667. But the rendered elements on the screen will be twice as sharp as an actual 375x667 screen because there are twice as many physical pixels in the physical screen.
Some other examples:
Samsung Galaxy S4
Actual Resolution: 1080 x 1920
CSS Pixel Ratio: 3
Logical Resolution:
iPhone 5s
Actual Resolution: 640 x 1136
CSS Pixel Ratio: 2
Logical Resolution:
Why does the Device Pixel Ratio exist?
The reason that CSS pixel ratio was created is because as phones screens get higher resolutions, if every device still had a CSS pixel ratio of 1 then webpages would render too small to see.
A typical full screen desktop monitor is a roughly 24" at 1920x1080 resolution. Imagine if that monitor was shrunk down to about 5" but had the same resolution. Viewing things on the screen would be impossible because they would be so small. But manufactures are coming out with 1920x1080 resolution phone screens consistently now.
So the device pixel ratio was invented by phone makers so that they could continue to push the resolution, sharpness and quality of phone screens, without making elements on the screen too small to see or read.
Here is a tool that also tells you your current device's pixel density:
http://bjango.com/articles/min-device-pixel-ratio/
Boris Smus's article High DPI Images for Variable Pixel Densities has a more accurate definition of device pixel ratio: the number of device pixels per CSS pixel is a good approximation, but not the whole story.
Note that you can get the DPR used by a device with window.devicePixelRatio.
https://developer.mozilla.org/en/CSS/Media_queries#-moz-device-pixel-ratio
-moz-device-pixel-ratio
Gives the number of device pixels per CSS pixel.
this is almost self-explaining. the number describes the ratio of how much "real" pixels (physical pixerls of the screen) are used to display one "virtual" pixel (size set in CSS).
Device Pixel Ratio has direct correlation with Pixel density of the device.
Best concise description I could find:
Purpose of DPR is to keep consistent size of CSS pixels and therefore
consistent size of letters, symbols, images and everything else on
screen, across a variety of devices with different physical pixel
densities.
Source: screenresolutiontest

Resources