CSS orientation Media Feature

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

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.

CSS Syntax

syntax.css
@media (orientation: portrait | landscape) { … }

One keyword per query. You can combine with other features, for example @media (orientation: landscape) and (min-width: 768px).

Feature Values

ValueDescriptionTry it
portraitHeight is greater than or equal to widthTry it
landscapeWidth is greater than heightTry it
Both queriesSwitch layout for tall vs wide viewportsTry it
With widthCombine orientation and a width queryTry it
Flex switchColumn in portrait, row in landscapeTry it
Label textShow which orientation currently matchesTry it

How to Use orientation

Write base styles, then override inside portrait and/or landscape queries.

  1. Choose a default layout that works in the common case (often portrait on phones).
  2. Add @media (orientation: landscape) { … } (and portrait if needed).
  3. Change only what must adapt — direction, spacing, or image size.
  4. Resize the window or rotate a device to verify both matches.

Live Preview

This page checks your viewport with matchMedia. Resize the window to flip the match. Sample boxes use real @media (orientation) rules.

Viewport check

Portrait means height ≥ width. Landscape means width > height.

Results
  • portrait: …
  • landscape: …
Checking…

Checking…

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).

CSS
<style>
  .box {
    padding: 1rem;
    background: #e2e8f0;
  }

  @media (orientation: portrait) {
    .box { background: #dbeafe; }
  }
</style>

<div class="box">Portrait styles when tall.</div>
Try It Yourself

How It Works

  1. Set a base. Gray background works in any orientation.
  2. Query portrait. Matches when height ≥ width.
  3. Override inside the block. Soft blue applies only while portrait matches.
  4. Resize taller. Make the window taller than wide to see the change.

Example 2 — Landscape

Target wide viewports with a separate query.

CSS
<style>
  .box {
    padding: 1rem;
    background: #e2e8f0;
  }

  @media (orientation: landscape) {
    .box { background: #dcfce7; }
  }
</style>

<div class="box">Landscape styles when wide.</div>
Try It Yourself

How It Works

  1. Same base idea. Gray until a query matches.
  2. Query landscape. Matches when width > height.
  3. Green accent. Applies only in wide viewports.
  4. Resize wider. Stretch the window horizontally to match.

Example 3 — Flex Direction Switch

Stack items in portrait; place them in a row in landscape.

CSS
<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>
Try It Yourself

How It Works

  1. Default to column. Tall screens stack the boxes.
  2. Landscape overrides. flex-direction: row when width wins.
  3. Items share space. flex: 1 keeps them even in a row.
  4. 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.

viewport

Viewport, not device sensor

orientation compares viewport width and height. It does not read the accelerometer.

keyboard

Soft keyboard can flip the match

On many phones, a tall keyboard shrinks height so landscape styles apply while the device is still upright.

square

Equal sides count as portrait

When height equals width, portrait matches (height ≥ width).

vs-ratio

Prefer aspect-ratio for fine control

Need 16/9 or other ratios? Use aspect-ratio. Use orientation for simple tall vs wide.

combine

Combine with width when needed

Desktop windows can be landscape and narrow. Pair with width if phone-only rules matter.

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.

98% Browser support
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 support
orientation Widely supported

Bottom line: Safe for responsive layouts. Remember it tracks the viewport. It is not a runtime API.

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.

Next: overflow-block

Detect how the block axis overflows on the output device.

overflow-block tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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