Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS @media orientation Property

Posted in CSS Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 17 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
CSS @media orientation Property

Photo Credit to CodeToFun

πŸ™‹ Introduction

The @media orientation property in CSS is used to apply specific styles based on the orientation of the device's screen. It helps developers create responsive designs that adapt depending on whether the device is in portrait or landscape mode.

This is particularly useful for enhancing the user experience on mobile devices and tablets, where the screen orientation can frequently change.

πŸ’‘ Syntax

The syntax for the @media orientation property follows the basic structure of a media query. You can define styles for portrait or landscape orientations.

Syntax
Copied
Copy To Clipboard
@media (orientation: portrait) {
  /* Styles for portrait orientation */
}

@media (orientation: landscape) {
  /* Styles for landscape orientation */
}

🏠 Property Values

  • portrait: Applies styles when the height of the viewport is greater than its width.
  • landscape: Applies styles when the width of the viewport is greater than its height.

πŸŽ›οΈ Default Value

The @media orientation property does not have a default value, as it’s based on the actual screen orientation of the device. If no media queries are defined, the default styles in your CSS will apply.

πŸ“ Example Usage

πŸ“œ Basic Usage

In this example, different background colors are applied depending on the screen orientation.

example.scss
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS @media orientation Example</title>
  <style>
    /* Styles for portrait orientation */
    @media (orientation: portrait) {
      body {
        background-color: lightblue;
      }
    }

    /* Styles for landscape orientation */
    @media (orientation: landscape) {
      body {
        background-color: lightgreen;
      }
    }
  </style>
</head>
<body>
  <h1>Resize your device or browser to see the effect!</h1>
</body>
</html>

In this example, the background color changes to light blue in portrait mode and light green in landscape mode. Try resizing the browser window or rotating your device to see the effect.

πŸ–₯️ Browser Compatibility

The @media orientation property is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It works seamlessly on most mobile and tablet devices. As always, it’s recommended to test your styles across different browsers and devices to ensure consistent behavior.

πŸŽ‰ Conclusion

The @media orientation property is a powerful tool for creating responsive and adaptable web designs. By targeting different screen orientations, you can ensure that your content remains visually appealing and easy to use, whether viewed in portrait or landscape mode. This enhances the user experience and helps your website work well across a wide range of devices.

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy