Languages
[Edit]
EN

CSS - style elements only for portrait orientation

0 points
Created by:
Wayne
475

In this article, we would like to show you how to style elements only for portrait orientation in CSS.

Quick solution:

@media (orientation: portrait) {
    .element {
        /* put portrait styles here */
    }
}

Where:

  • portrait styles are applied to the element when the viewport width <= height.

 

Practical example

In this example, div element background will be orange on portrait orientation (vertical) only.

// ONLINE-RUNNER:browser;

<!DOCTYPE html>
<html>
<head>
  <style>

    div {
        width: 100%;
        height: 500px;
    }

    @media (orientation: portrait) {
        div {
            background: orange;
        }
    }

  </style>
</head>
<body>
  <div>Web page content here...</div>
</body>
</html>

 

See also

  1. CSS - viewport explanation

References

  1. orientation - CSS: Cascading Style Sheets | MDN
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join