orientation CSS media feature can be used to test the orientation of the viewport (or the page box, for paged media ).

注意: 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.

句法

orientation feature is specified as a keyword value chosen from the list below.

Keyword values

portrait

The viewport is in a portrait orientation, i.e.,  the height is greater than or equal to the width.

landscape

The viewport is in a landscape orientation, i.e., the width is greater than the height.

范例

HTML

<div>Box 1</div>
<div>Box 2</div>
<div>Box 3</div>
					

CSS

body {
  display: flex;
}
div {
  background: yellow;
}
@media (orientation: landscape) {
  body {
    flex-direction: row;
  }
}
@media (orientation: portrait) {
  body {
    flex-direction: column;
  }
}
					

结果

规范

规范 状态 注释
Media Queries Level 4
The definition of 'orientation' in that specification.
候选推荐 无变化。
Media Queries
The definition of 'orientation' in that specification.
推荐 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
orientation media feature Chrome 完整支持 3 Edge 完整支持 12 Firefox 完整支持 2 IE 完整支持 9 Opera 完整支持 10.6 Safari 完整支持 5 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 11 Safari iOS 完整支持 4.2 Samsung Internet Android 完整支持 1.0

图例

完整支持

完整支持

元数据

  • 最后修改: