orientation
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The orientation
CSS media feature can be used to test the orientation of the viewport (or the page box, for paged media).
Note: This feature does not correspond to device orientation. Opening the soft keyboard on many devices in portrait orientation will cause the viewport to become wider than it is tall, thereby causing the browser to use landscape styles instead of portrait.
Syntax
The orientation
feature is specified as a keyword value chosen from the list below.
Keyword values
Examples
Portrait orientation
In this example we have three boxes in the HTML, and use the orientation
media feature to switch between a row layout (in landscape) and a column layout (in portrait).
The example output is embedded in an <iframe>
whose height is greater than its width, so the boxes get a column layout.
HTML
<div>Box 1</div>
<div>Box 2</div>
<div>Box 3</div>
CSS
body {
display: flex;
}
div {
background: yellow;
width: 200px;
height: 200px;
margin: 0.5rem;
padding: 0.5rem;
}
@media (orientation: landscape) {
body {
flex-direction: row;
}
}
@media (orientation: portrait) {
body {
flex-direction: column;
}
}
Result
Landscape orientation
This example has exactly the same code as the previous example: it has three boxes in the HTML, and uses the orientation
media feature to switch between a row layout (in landscape) and a column layout (in portrait).
However, in this example, the example output is embedded in an <iframe>
whose height is less than its width, so the boxes get a row layout.
HTML
<div>Box 1</div>
<div>Box 2</div>
<div>Box 3</div>
CSS
body {
display: flex;
}
div {
background: yellow;
width: 200px;
height: 200px;
margin: 0.5rem;
padding: 0.5rem;
}
@media (orientation: landscape) {
body {
flex-direction: row;
}
}
@media (orientation: portrait) {
body {
flex-direction: column;
}
}
Specifications
Specification |
---|
Media Queries Level 4 # orientation |
Browser compatibility
BCD tables only load in the browser