Orientation Media Feature
You'll learn how to detect whether a device is in portrait or landscape mode and adjust your CSS accordingly.
What Is Orientation?
The orientation media feature lets you apply different styles based on whether the viewport is taller than it is wide (portrait) or wider than it is tall (landscape). This is especially useful for phones and tablets that users rotate frequently.
Think of it like a book: when you hold it upright, you're reading in "portrait" mode. Tilt it sideways, and you're in "landscape" mode. Your CSS can respond to this rotation automatically.
How It Works
Inside a media query, you test for orientation like this:
/* Styles for portrait orientation (height > width) */
@media (orientation: portrait) {
.container {
flex-direction: column;
}
}
/* Styles for landscape orientation (width > height) */
@media (orientation: landscape) {
.container {
flex-direction: row;
}
}
When a user rotates their phone from vertical to horizontal, the landscape styles kick in. Rotate back, and portrait styles apply again.
Practical Example
/* Default mobile styles */
.gallery {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
/* When device rotates to landscape */
@media (orientation: landscape) {
.gallery {
grid-template-columns: 1fr 1fr;
}
}
This gallery shows one column when upright, but switches to two columns when the device is sideways—giving you more horizontal space to work with.
Key Takeaway: The orientation media feature detects portrait (height > width) or landscape (width > height) mode, letting you optimize layouts for how users physically hold their devices.