CSS orientation Media Feature

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Media Queries

What You’ll Learn

The orientation media feature detects whether the viewport is portrait (tall) or landscape (wide). Adapt layouts when users rotate phones or resize browser windows.

01

@media

Media query.

02

portrait

Tall viewport.

03

landscape

Wide viewport.

04

Mobile

Rotate device.

05

vs ratio

aspect-ratio.

06

+ width

Combine queries.

Introduction

When you hold a phone upright, the screen is in portrait orientation — height is greater than width. Rotate it sideways and you get landscape — width is greater than height.

The @media (orientation) feature lets CSS respond to that change instantly. It is one of the most practical media queries for mobile and tablet design.

Definition and Usage

Write @media (orientation: portrait) for tall viewports or @media (orientation: landscape) for wide viewports. Combine with min-width when you only want landscape rules on larger screens.

💡
Beginner Tip

Resize your browser window taller than it is wide to trigger portrait, or wider than tall for landscape. On a phone, simply rotate the device to test.

📝 Syntax

Use portrait or landscape inside an @media rule:

syntax.css
/* Viewport is taller than wide */
@media (orientation: portrait) {
  /* Portrait styles */
}

/* Viewport is wider than tall */
@media (orientation: landscape) {
  /* Landscape styles */
}

Accepted Values

  • portrait — Viewport height is greater than width (phone held upright, tall browser window).
  • landscape — Viewport width is greater than height (phone rotated sideways, wide browser window).

Portrait vs Landscape

portrait
height > width
landscape — width > height

orientation vs aspect-ratio

(orientation: landscape) Simple wide-vs-tall check. Matches any viewport where width exceeds height.
(min-aspect-ratio: 16/9) Matches specific ratios. Finer control for widescreen or square layouts.

Basic Example

orientation-basic.css
@media (orientation: portrait) {
  body {
    background-color: #dbeafe;
  }
}

@media (orientation: landscape) {
  body {
    background-color: #dcfce7;
  }
}

Default Value

There is no CSS default. Orientation is determined by the current viewport dimensions. Your base styles apply until a matching query overrides them.

Syntax Rules

  • Only two values: portrait and landscape.
  • Combine with width: @media (orientation: landscape) and (min-width: 48rem).
  • Square viewports (equal width and height) match neither portrait nor landscape per spec.
  • Prefer min-width breakpoints for general responsive design; use orientation for rotation-specific tweaks.
  • Test by resizing the browser or rotating a real device.

Related Topics

@media (orientation: portrait) @media (orientation: landscape) and (min-width: 48rem)

⚡ Quick Reference

QuestionAnswer
Feature nameorientation
Valuesportrait, landscape
Portrait conditionViewport height > width
Landscape conditionViewport width > height
Primary use caseMobile/tablet rotation layouts
Finer ratio controlUse aspect-ratio media feature
Browser supportAll modern browsers

When to Use orientation

Reach for orientation when viewport shape drives the layout:

  • Mobile rotation — Switch between stacked and side-by-side layouts.
  • Video players — Expand to full width in landscape mode.
  • Navigation bars — Vertical nav in portrait, horizontal in landscape.
  • Hero sections — Tall image in portrait, wide banner in landscape.
  • Keyboard open on phones — Landscape often means less vertical space despite wide width.

👀 Live Preview

Resize your browser or rotate your device. This block is blue in portrait and green in landscape:

Orientation Demo @media (orientation)

Examples Gallery

Practice orientation with background themes, flexible layouts, navigation changes, and combined width queries.

📜 Core Patterns

Adapt styles when the viewport is tall or wide.

Example 1 — Background by orientation

Apply different page backgrounds for portrait and landscape:

orientation-background.css
@media (orientation: portrait) {
  body {
    background-color: #dbeafe;
  }
}

@media (orientation: landscape) {
  body {
    background-color: #dcfce7;
  }
}
Try It Yourself

How It Works

This is the classic beginner demo from the reference tutorial — instant visual feedback when orientation changes.

Example 2 — Stack in portrait, columns in landscape

Single column on tall screens; two columns when the viewport is wide:

orientation-layout.css
.layout {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (orientation: landscape) {
  .layout {
    flex-direction: row;
  }
  .layout > * {
    flex: 1;
  }
}
Try It Yourself

How It Works

Portrait stacks content vertically; landscape uses horizontal space for side-by-side panels.

Example 3 — Vertical nav in portrait, horizontal in landscape

Adapt navigation direction based on orientation:

orientation-nav.css
.nav {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem;
  background: #1e293b;
}

.nav a {
  color: #f8fafc;
  text-decoration: none;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
}

@media (orientation: landscape) {
  .nav {
    flex-direction: row;
    justify-content: center;
  }
}
Try It Yourself

How It Works

Portrait gives a vertical menu; landscape spreads links in a horizontal bar across the top.

Example 4 — Landscape layout on wide screens only

Combine orientation with min-width so small landscape phones keep a stacked layout:

orientation-combined.css
.gallery {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

@media (orientation: landscape) and (min-width: 40rem) {
  .gallery {
    grid-template-columns: repeat(3, 1fr);
  }
}
Try It Yourself

How It Works

A phone rotated to landscape may still be narrow. Adding min-width avoids cramming three columns on small screens.

💬 Usage Tips

  • Combine with min-width — Landscape alone is not enough for desktop-sized layouts.
  • Test real devices — Rotate phones and tablets to verify navigation and spacing.
  • Consider keyboard height — Mobile landscape often has very little vertical space.
  • Use aspect-ratio for ratios — When you need 16:9 specifically, not just “wide.”
  • Mobile-first defaults — Portrait styles often match your base mobile layout.

⚠️ Common Pitfalls

  • Landscape ≠ desktop — A rotated phone is landscape but still small.
  • Replacing min-width entirely — Use width breakpoints for general responsive design.
  • Square viewports — Equal width and height match neither value.
  • Over-rotating layouts — Avoid jarring layout shifts; keep content consistent.
  • Ignoring portrait keyboard — Virtual keyboard reduces height in portrait too.

♿ Accessibility

  • Content in both orientations — Never hide essential content in one orientation.
  • Touch targets — Keep buttons at least 44×44px in all orientations.
  • Reflow — Support zoom and text scaling without horizontal scroll.
  • Focus order — Tab order should make sense in portrait and landscape.
  • Reduced motion — Avoid disorienting layout animations on rotation.

🧠 How orientation Works

1

Viewport dimensions change

User rotates device or resizes the browser window.

Resize
2

Browser compares width vs height

Determines portrait or landscape.

Compare
3

@media rules apply

Matching orientation blocks update layout, colors, and navigation.

Apply
=

Rotation-ready UI

Layout adapts smoothly when the viewport shape changes.

🖥 Browser Compatibility

The orientation media feature is universally supported in modern browsers on mobile, tablet, and desktop.

Baseline · Universal support

Portrait & landscape queries

Works in Chrome, Firefox, Safari, Edge, and Opera.

99% Global support
orientation media feature 99% supported

Bottom line: orientation is production-ready. Combine with min-width for robust responsive layouts across devices.

🎉 Conclusion

The orientation media feature is one of the most practical tools for responsive design. With portrait and landscape, you can adapt layouts when users rotate phones or resize browser windows.

Use it for navigation, galleries, and hero sections that should look different tall vs wide. Pair with width breakpoints and keep essential content accessible in every orientation.

💡 Best Practices

✅ Do

  • Combine with min-width
  • Test device rotation
  • Keep content in both modes
  • Use for nav and galleries
  • Mobile-first base styles

❌ Don’t

  • Assume landscape = desktop
  • Replace width breakpoints
  • Hide essential content
  • Over-animate on rotate
  • Ignore keyboard space

Key Takeaways

Knowledge Unlocked

Five things to remember about orientation

Use these points when building rotation-aware layouts.

5
Core concepts
L 02

landscape

Wide viewport.

Value
📱 03

Rotate

Mobile UX.

Use case
+W 04

min-width

Combine.

Pattern
🌐 05

99% support

Universal.

Compat

❓ Frequently Asked Questions

The orientation media feature matches when the viewport is in portrait mode (taller than wide) or landscape mode (wider than tall). Use @media (orientation: portrait) or @media (orientation: landscape) to adapt layouts when users rotate their phone or resize the browser.
Two values: portrait (viewport height is greater than width) and landscape (viewport width is greater than height).
orientation uses simple portrait/landscape keywords based on whether height or width is larger. aspect-ratio matches specific width-to-height ratios like 16/9 or 4/3 with min/max variants for finer control.
Use it for mobile layouts that change when the device rotates, video players that go full-width in landscape, navigation that switches between stacked and horizontal, and any UI that should adapt to tall vs wide viewports.
Yes. The orientation media feature is supported in all modern browsers including Chrome, Firefox, Safari, Edge, and Opera on phones, tablets, and desktops.

Practice in the Live Editor

Open the HTML editor and experiment with @media (orientation: portrait) and landscape layouts.

HTML Editor →

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.

5 people found this page helpful