DZone
Web Dev Zone
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
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Detecting Screen Orientation in JavaScript

Detecting Screen Orientation in JavaScript

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

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Apr. 28, 22 · Web Dev Zone · Code Snippet
Like (3)
Save
Tweet
4.71K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top Soft Skills to Identify a Great Software Engineer
  • Choosing Between GraphQL Vs REST
  • Synchronization Methods for Many-To-Many Associations
  • Java Hashtable, HashMap, ConcurrentHashMap: Performance Impact

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo