orientation is determined purely by comparing the viewport's current width against its current height, portrait meaning height is greater than or equal to width, and landscape meaning width is greater than height, regardless of what specific device is being used or how that device's own physical orientation sensor might report its rotation — a desktop browser window can trigger orientation: portrait simply by being resized taller than it is wide, entirely independent of any actual physical device rotation. This makes it useful for adjusting layouts specifically suited to a wider-than-tall or taller-than-wide viewport shape, like switching a video player's controls layout or a grid's column count based on the available aspect ratio.
1Understanding Orientation
orientation is determined purely by comparing the viewport's current width against its current height, portrait meaning height is greater than or equal to width, and landscape meaning width is greater than height, regardless of what specific device is being used or how that device's own physical orientation sensor might report its rotation — a desktop browser window can trigger orientation: portrait simply by being resized taller than it is wide, entirely independent of any actual physical device rotation. This makes it useful for adjusting layouts specifically suited to a wider-than-tall or taller-than-wide viewport shape, like switching a video player's controls layout or a grid's column count based on the available aspect ratio.
orientation is based purely on the current viewport's width-versus-height comparison, not a physical device's actual rotation sensor — resizing a plain desktop browser window to be taller than it is wide triggers orientation: portrait, even on a desktop computer with no rotation capability at all.
@media (orientation: landscape) {
.video-controls {
flex-direction: row;
}
}
@media (orientation: portrait) {
.video-controls {
flex-direction: column;
}
}2Practical Example
Here is a real-world application of Orientation showing how it is used in production CSS code.
@media (orientation: portrait) and (max-width: 500px) {
.gallery {
grid-template-columns: 1fr;
}
}3Best Practices
Follow these guidelines when working with Orientation:
1. Use orientation queries specifically for layout adjustments that genuinely depend on the viewport's relative width-versus-height shape, like a video player's control layout
2. Remember orientation applies equally to a resized desktop browser window as to an actual rotated mobile device, since it's purely a width/height comparison
3. Combine orientation with width-based queries when a specific adjustment should only apply within a particular combination of both screen size and aspect ratio
Tip: orientation is based purely on the current viewport's width-versus-height comparison, not a physical device's actual rotation sensor — resizing a plain desktop browser window to be taller than it is wide triggers orientation: portrait, even on a desktop computer with no rotation capability at all.
@media (orientation: landscape) {
.video-controls {
flex-direction: row;
}
}
@media (orientation: portrait) {
.video-controls {
flex-direction: column;
}
}