Aspect-ratio Media Queries
By now most of us are using media queries. Most of these are testing the width of the display. What if we want to target widescreen displays, or even when a user resizes their window to mimic a widescreen ratio? Enter aspect-ratio media queries.
Aspect Ratio?
Aspect ratio is the ratio between a shape’s sizes in different dimensions. In this case, that shape is either our screen, or window. We’ll get into the difference in a moment.
Aspect ratios are represented by two numbers delineated by a colon, such as 4:3. The first number represents the horizontal ratio, the second the vertical ratio.
For example, a 15″ Retina Macbook Pro has a resolution of 2880×1800. The lowest common divisor of these two numbers is 360, and dividing by that we get our 8:5 aspect ratio.
Media Queries
We have two options when it comes to querying based on aspect ratio. I’ll explain the differences in each below.
Note: Although aspect ratios are usually delineated with a colon, css uses a forward slash (/).
aspect-ratio
This media query describes the aspect ratio of the targeted display area of the output device. This means the window you are working in. If a user resizes their screen to be within the bounds of your denoted ratio, the query will go into effect.
@media screen and (min-aspect-ratio: 8/5) { ... }
In the above example, we are telling the browser to call this css is if the window aspect is 8:5 or higher. This means 8:4, 8:3, etc. will work. This will work for our Macbook example, whether the user has their browser full screen or shrinks it vertically, they will get the css.
The benefit here is we aren’t married to the aspect-ratio of the device. Since this query only takes the viewable area into account, we can get it to fire off when our user has 18 toolbars open, as it doesn’t care about the space the browser chrome is taking up.
device-aspect-ratio
This media query describes the physical aspect ration of the screen itself.
@media screen and (min-device-aspect-ratio: 8/5) { ... }
The above example will display our css when the screen aspect ratio is 8:5 or higher, regardless of how we resize our browser.
How are you using aspect-ratio media queries?
Code icon designed by Mayene de La Cruz from The Noun Project
Comments (10)
Leave a Reply
Have any visuals or a jsfiddle? I would think aspect ratio only applies to large images or containers that you would want to be responsive and display all in one view. I use media queries for responsive stacking, but never used ratios before. We usually allow the browser’s vertical scroll to handle it.
Link to commentAspect ratio applies to a few different scenarios. Personally, my experience has been apps that need to display the content in a single view, like you said.
No fiddle, but you could always make one!
Link to commentThank you for this article. It was described in clear manner.
Link to commentHi Rob! Thanks for the article! Can you tell us about browser support?
Link to commentAspect ratio media queries fall in line with standard media queries when it comes to browser support. Basically, if you don’t need to support the dreaded ie8, you are good to go!
Link to commentGreat article, it helped clear up how aspect ratio works. You made it a little confusing though when you were giving the min-aspect-ratio: 8/5 example.
@media screen and (min-aspect-ratio: 8/5) { … } “we are telling the browser to call this css if the window aspect is 8:5 or higher. This means 8:4, 8:3, etc. will work.”
That’s really misleading and confusing because browsers more read it like:
Link to commentmin-aspect-ratio: 8/5 = <8:5
As in 7:5, 6:5, 4:5 will also work. Saying that it is equivalent to 8:4, 8:3 etc. may technically be true, but it is very misleading for readers and it's going to confuse a lot of people.
Amazing article! Thank you so much!! This helped me a lot!
I have one doubt. What is the best way to discover the device-ratio of devices? Do you have a website? Mathematical formula? Guide?
Thanks again!
Link to commentJust to contribute to Raphael’s question –
you can easily calculate the ratio using this online tool (I’m sure there are others).
http://andrew.hedges.name/experiments/aspect_ratio/
Just enter the dimensions and it will tell you the ratio below.
Small note: the tool will provide the ratio in %num1% : %num2% format –
Link to commentyou can convert it to %num1% / %num2% for the media query and it will work.
Nice! This is the only resource I found that went into detail about how to use aspect ratio media queries. Thank you!
Here’s a codepen I did to illustrate one use-case in combination with background-image:cover
https://codepen.io/squarecandy/pen/YYGpgX
Link to commentThis is awesome Peter! Let me know if there’s anything I could add to the article to better explain things.
Link to comment