DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • PWA on iOS, Part 5: Service Worker Updates, Cache, and Push
  • PWA on iOS, Part 4: What Add to Home Screen Really Does
  • PWA on iOS, Part 3: Chrome on iPhone Is WebKit Too. How One Engine Sets the Rules for PWAs on iOS
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker

Trending

  • Designing a Data Pipeline for Multi-Location Marketing Attribution (Before You Actually Need One)
  • How We Cut DynamoDB Ingestion Reads With an S3 Hash Pre-Filter
  • Solving the Stateful Exception: Database Branching and the Unification of CI/CD
  • Hosted Code Interpreter in Spring AI: Letting OpenAI Write and Run Python via the Responses API

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. JavaScript
  4. Detecting Screen Orientation in JavaScript

Detecting Screen Orientation in JavaScript

Let's look at how to detect screen orientation in JavaScript.

By 
Johnny Simpson
·
Apr. 28, 22 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Sometimes, you want to know if a screen is in portrait or landscape mode. There are two primary places you would like to do this: JavaScript and CSS. So let's look at how to detect the screen's orientation in both.

Detecting Orientation in CSS

In CSS, use the following media queries to match any portrait or landscape device:

 
/* Portrait orientation */
@media screen and (orientation: portrait) {

}
/* Landscape orientation */
@media screen and (orientation: landscape) {

}


Detecting Orientation in JavaScript

Since screen.orientation has patchy support; you can use the same media query in JavaScript like so:

 
let portrait = window.matchMedia("(orientation: portrait)");

portrait.addEventListener("change", function(e) {
    if(e.matches) {
        // Portrait mode
    } else {
        // Landscape
    }
})


Detecting Orientation Changes in JavaScript

Should you need to detect when a user changes orientation, you can use the following event listener:

 
screen.orientation.addEventListener("change", function(e) {
    // Do something on change
});


Currently, this is not supported in Safari, so your mileage may vary on this one. However, you can use the matchMedia query from above to achieve similar functionality if you need to.

Conclusion

Detecting screen orientation is easy - and in the future, we'll be able to use screen.orientation to do this reliably. For now, it's best to stick with CSS media queries and window.matchMedia.

JavaScript

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • PWA on iOS, Part 5: Service Worker Updates, Cache, and Push
  • PWA on iOS, Part 4: What Add to Home Screen Really Does
  • PWA on iOS, Part 3: Chrome on iPhone Is WebKit Too. How One Engine Sets the Rules for PWAs on iOS
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook