
Definition
Definition and Usage
The orientation media feature tests whether the viewport is taller than wide (portrait) or wider than tall (landscape).
Use it inside @media to change layout when users rotate a phone or resize the browser. It follows viewport size — not the physical device sensor.
Baseline · Widely available.Not deprecated. Cross-browser since about July 2015.
Viewport tip.Opening a soft keyboard can make height < width and flip you into landscape styles even if the phone is still upright.
Syntax
CSS Syntax
@media (orientation: portrait | landscape) { … }
One keyword per query. You can combine with other features, for example @media (orientation: landscape) and (min-width: 768px).
Values
Feature Values
| Value | Description | Try it |
|---|
portrait | Height is greater than or equal to width | Try it |
landscape | Width is greater than height | Try it |
| Both queries | Switch layout for tall vs wide viewports | Try it |
| With width | Combine orientation and a width query | Try it |
| Flex switch | Column in portrait, row in landscape | Try it |
| Label text | Show which orientation currently matches | Try it |
Approach
How to Use orientation
Write base styles, then override inside portrait and/or landscape queries.
- Choose a default layout that works in the common case (often portrait on phones).
- Add
@media (orientation: landscape) { … } (and portrait if needed). - Change only what must adapt — direction, spacing, or image size.
- Resize the window or rotate a device to verify both matches.
Try it
Live Preview
This page checks your viewport with matchMedia. Resize the window to flip the match. Sample boxes use real @media (orientation) rules.
Code
CSS Examples
Resize the browser (or the Try It pane) to see each query match. Use Try It Yourself for a live editor.
Example 1 — Portrait
Style content when the viewport is tall (or square).
<style>
.box {
padding: 1rem;
background: #e2e8f0;
}
@media (orientation: portrait) {
.box { background: #dbeafe; }
}
</style>
<div class="box">Portrait styles when tall.</div>
Portrait styles when tall.
How It Works
- Set a base. Gray background works in any orientation.
- Query portrait. Matches when height ≥ width.
- Override inside the block. Soft blue applies only while portrait matches.
- Resize taller. Make the window taller than wide to see the change.
Example 2 — Landscape
Target wide viewports with a separate query.
<style>
.box {
padding: 1rem;
background: #e2e8f0;
}
@media (orientation: landscape) {
.box { background: #dcfce7; }
}
</style>
<div class="box">Landscape styles when wide.</div>
Landscape styles when wide.
How It Works
- Same base idea. Gray until a query matches.
- Query landscape. Matches when width > height.
- Green accent. Applies only in wide viewports.
- Resize wider. Stretch the window horizontally to match.
Example 3 — Flex Direction Switch
Stack items in portrait; place them in a row in landscape.
<style>
.row {
display: flex;
gap: 0.5rem;
flex-direction: column;
}
@media (orientation: landscape) {
.row { flex-direction: row; }
}
.item {
flex: 1;
padding: 0.75rem;
background: #2563eb;
color: #fff;
text-align: center;
font-weight: 700;
}
</style>
<div class="row">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
How It Works
- Default to column. Tall screens stack the boxes.
- Landscape overrides.
flex-direction: row when width wins. - Items share space.
flex: 1 keeps them even in a row. - Rotate or resize. The axis flips with the viewport.
Edge Cases & Pitfalls
Most surprises come from thinking orientation means the physical device, not the viewport.
viewportViewport, not device sensor
orientation compares viewport width and height. It does not read the accelerometer.
keyboardSoft keyboard can flip the match
On many phones, a tall keyboard shrinks height so landscape styles apply while the device is still upright.
squareEqual sides count as portrait
When height equals width, portrait matches (height ≥ width).
vs-ratioPrefer aspect-ratio for fine control
Need 16/9 or other ratios? Use aspect-ratio. Use orientation for simple tall vs wide.
combineCombine with width when needed
Desktop windows can be landscape and narrow. Pair with width if phone-only rules matter.
Compatibility
Browser Support
orientation is a browser CSS media feature for viewport shape. Node.js, Deno, and Bun do not apply it.
✓ Baseline · Widely available
orientation
Chrome 3+, Firefox 3.5+, Safari 3.1+, Edge 12+, IE 9+. Widely available since July 2015.
Google Chrome 3+
Full support
Mozilla Firefox 3.5+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 10.6+
Full support
Samsung Internet 1+
Full support
Internet Explorer 9+
Full supportorientation Widely supported
Bottom line: Safe for responsive layouts. Remember it tracks the viewport. It is not a runtime API.
Remember
Key Takeaways
Role: match tall (portrait) or wide (landscape) viewports.
Portrait: height ≥ width. Landscape: width > height.
Not the sensor: keyboard and resize change the match.
Combine: add width/height queries when desktop windows need finer rules.
One line: use @media (orientation: …) to adapt layouts for tall vs wide viewports.
Frequently Asked Questions
It matches whether the viewport is portrait (height ≥ width) or landscape (width > height) so you can adapt layout with @media.
Two keywords: portrait and landscape.
No. It follows the viewport size. A soft keyboard that shortens height can make a phone match landscape styles while still held upright.
orientation is a simple tall-vs-wide test. aspect-ratio matches specific ratios such as 16/9 with finer min/max control.
When height equals width, the viewport matches portrait (height is greater than or equal to width).
It is Baseline and widely available since about July 2015. It is a browser CSS media feature, not a Node.js API.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful