Size container queries can detect landscape / portrait orientation (#tilPost)

I’m still wrapping my head around container size queries. The concept is clear (I think), but now that they’re there, I’m finally catching up.

MDN Compat Data (source)


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I'm still wrapping my head around container size queries. The concept is clear (I think), but now that they're there, I'm finally catching up.

MDN Compat Data (source)
Browser support info for @container
chrome chrome_android edge firefox firefox_android safari safari_ios samsunginternet_android webview_android
105 105 105 110 110 16 16 20.0 105

Manuel Matuzović discovered you can detect a container's portrait or landscape mode with Container Size Queries.

Orientation queries might be the solution to a niche problem, but I think it's exciting and makes a nice demo.

[Interactive component: visit the article to see it...]

The CSS to make this work is as follows:

.container {
  container-type: size;
  height: 12rem;
}

@container (orientation: portrait) {
  .portrait {
    display: grid;
  }

  .landscape {
    display: none;
  }
}

Building this component took me surprisingly long because orientation queries have a big gotcha. If you want to evaluate a container's orientation, you must consider the inline and(!) block size and define container-type: size. And this is where it becomes tricky for browser makers. How would you implement a container query that depends on X and Y axis with the dynamic nature of CSS? I've no idea.

So currently, you have to set an explicit height in all major browsers (Chromium, Safari, Firefox), or your container has zero height if you want to consider both axes in your queries. Badumpts... Container queries and browsers are complicated.

In summary, this functionality makes a fancy demo, but it's hard for me to find a use case for a orienation: portrait when I have to define the component height myself. But hey – today I learned. 😅


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2023-03-16T23:00:00+00:00) Size container queries can detect landscape / portrait orientation (#tilPost). Retrieved from https://www.scien.cx/2023/03/16/size-container-queries-can-detect-landscape-portrait-orientation-tilpost/

MLA
" » Size container queries can detect landscape / portrait orientation (#tilPost)." Stefan Judis | Sciencx - Thursday March 16, 2023, https://www.scien.cx/2023/03/16/size-container-queries-can-detect-landscape-portrait-orientation-tilpost/
HARVARD
Stefan Judis | Sciencx Thursday March 16, 2023 » Size container queries can detect landscape / portrait orientation (#tilPost)., viewed ,<https://www.scien.cx/2023/03/16/size-container-queries-can-detect-landscape-portrait-orientation-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Size container queries can detect landscape / portrait orientation (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/16/size-container-queries-can-detect-landscape-portrait-orientation-tilpost/
CHICAGO
" » Size container queries can detect landscape / portrait orientation (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2023/03/16/size-container-queries-can-detect-landscape-portrait-orientation-tilpost/
IEEE
" » Size container queries can detect landscape / portrait orientation (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2023/03/16/size-container-queries-can-detect-landscape-portrait-orientation-tilpost/. [Accessed: ]
rf:citation
» Size container queries can detect landscape / portrait orientation (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2023/03/16/size-container-queries-can-detect-landscape-portrait-orientation-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.