弃用
This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the 兼容性表格 at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

注意 : See https://github.com/w3c/csswg-drafts/issues/4766 for discussion around @viewport's removal from the standards track.

@viewport CSS at-rule lets you configure the viewport through which the document is viewed. It's primarily used for mobile devices, but is also used by desktop browsers that support features like "snap to edge" (such as Microsoft Edge).

Lengths specified as percentages are calculated relative to the initial viewport , which is the viewport before any user agent or authored styles have had an opportunity to adjust the viewport. This is typically based on the size of the window on desktop browsers that aren't in full screen mode.

On mobile devices (or desktop devices that are in full screen mode), the initial viewport is usually the portion of a device's screen that is available for application use. This may be either the full screen or the full screen area minus areas controlled by the operating system (such as a taskbar) or the application-available screen area (either the full screen or the screen minus any areas owned by the operating system or other applications).

@viewport {
  width: 100vw; /*Sets the width of the actual viewport to the device width*/
}
					

注意 : The use of <meta name="viewport"> tag overrides @viewport

句法

The at-rule contains a set of nested descriptor s in a CSS block that is delimited by curly braces.

A zoom factor of 1.0 or 100% corresponds to no zooming. Larger values zoom in. Smaller values zoom out.

Descriptors

Browser support for @viewport is weak at this time, with support being largely available in Internet Explorer and Edge. Even in those browsers, only a small number of descriptors are available. Browsers will ignore @viewport if they don't support it, and will ignore any descriptors that they don't recognize.

min-width

Used in the determination of the width of the viewport when the document is first displayed.

max-width

Used in the determination of the width of the viewport when the document is first displayed.

width
A shorthand descriptor for setting both min-width and max-width .
min-height

Used in the determination of the height of the viewport when the document is first displayed.

max-height

Used in the determination of the height of the viewport when the document is first displayed.

height
A shorthand descriptor for setting both min-height and max-height .
zoom

Sets the initial zoom factor.

min-zoom

Sets the minimum zoom factor.

max-zoom

Sets the maximum zoom factor.

user-zoom

Controls whether or not the user should be able to change the zoom factor.

orientation

Controls the document's orientation.

viewport-fit

Controls the display of the document on non-rectangular displays.

形式句法

@viewport {
  <group-rule-body>
}
					

范例

Setting viewport size, zoom, and orientation

@viewport {
  min-width: 640px;
  max-width: 800px;
}
@viewport {
  zoom: 0.75;
  min-zoom: 0.5;
  max-zoom: 0.9;
}
@viewport {
  orientation: landscape;
}
					

规范

规范 状态 注释
CSS Round Display Level 1
The definition of '@viewport' in that specification.
工作草案 Defined the viewport-fit descriptor.
CSS Device Adaptation
The definition of '@viewport' 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
@viewport Chrome 不支持 29 — 84 注意事项 Disabled
不支持 29 — 84 注意事项 Disabled
注意事项 See Chromium bug 235457 .
Disabled From version 29 until version 84 (exclusive): this feature is behind the --enable-experimental-webkit-features runtime flag and the --enable-viewport runtime flag.
Edge 完整支持 12 Prefixed Disabled
完整支持 12 Prefixed Disabled
Prefixed Implemented with the vendor prefix: -ms-
Disabled From version 12: this feature is behind the Enable @-ms-viewport rules preference.
Firefox 不支持 No 注意事项
不支持 No 注意事项
注意事项 See Firefox bug 747754 .
IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16 Disabled
完整支持 16 Disabled
Disabled From version 16: this feature is behind the --enable-experimental-webkit-features runtime flag and the --enable-viewport runtime flag.
不支持 11.1 — 15 Prefixed
Prefixed Implemented with the vendor prefix: -o-
Safari 不支持 No 注意事项
不支持 No 注意事项
注意事项 See WebKit bug 95959 .
WebView Android 不支持 4.4 — 37 Chrome Android 不支持 29 — 84 注意事项 Disabled
不支持 29 — 84 注意事项 Disabled
注意事项 See Chromium bug 235457 .
Disabled From version 29 until version 84 (exclusive): this feature is behind the --enable-experimental-webkit-features runtime flag and the --enable-viewport runtime flag.
Firefox Android 不支持 No 注意事项
不支持 No 注意事项
注意事项 See Firefox bug 747754 .
Opera Android 完整支持 16 Disabled
完整支持 16 Disabled
Disabled From version 16: this feature is behind the --enable-experimental-webkit-features runtime flag and the --enable-viewport runtime flag.
不支持 11.1 — 14 Prefixed
Prefixed Implemented with the vendor prefix: -o-
Safari iOS 不支持 No 注意事项
不支持 No 注意事项
注意事项 See WebKit bug 95959 .
Samsung Internet Android 完整支持 2.0
height descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16
完整支持 16
不支持 11.1 — 15
Safari 不支持 No WebView Android 不支持 4.4 — 37 Chrome Android 不支持 29 — 84 Firefox Android 不支持 No Opera Android 完整支持 16
完整支持 16
不支持 11.1 — 14
Safari iOS 不支持 No Samsung Internet Android 完整支持 2.0
max-height descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16
完整支持 16
不支持 11.1 — 15
Safari 不支持 No WebView Android 不支持 4.4 — 37 Chrome Android 不支持 29 — 84 Firefox Android 不支持 No Opera Android 完整支持 16
完整支持 16
不支持 11.1 — 14
Safari iOS 不支持 No Samsung Internet Android 完整支持 2.0
max-width descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16
完整支持 16
不支持 11.1 — 15
Safari 不支持 No WebView Android 不支持 4.4 — 37 Chrome Android 不支持 29 — 84 Firefox Android 不支持 No Opera Android 完整支持 16
完整支持 16
不支持 11.1 — 14
Safari iOS 不支持 No Samsung Internet Android 完整支持 2.0
max-zoom descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16 Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 29 — 84 Firefox Android 不支持 No Opera Android 完整支持 16 Safari iOS 不支持 No Samsung Internet Android 完整支持 2.0
min-height descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16
完整支持 16
不支持 11.1 — 15
Safari 不支持 No WebView Android 不支持 4.4 — 37 Chrome Android 不支持 29 — 84 Firefox Android 不支持 No Opera Android 完整支持 16
完整支持 16
不支持 11.1 — 14
Safari iOS 不支持 No Samsung Internet Android 完整支持 2.0
min-width descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16
完整支持 16
不支持 11.1 — 15
Safari 不支持 No WebView Android 不支持 4.4 — 37 Chrome Android 不支持 29 — 84 Firefox Android 不支持 No Opera Android 完整支持 16
完整支持 16
不支持 11.1 — 14
Safari iOS 不支持 No Samsung Internet Android 完整支持 2.0
min-zoom descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16 Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 29 — 84 Firefox Android 不支持 No Opera Android 完整支持 16 Safari iOS 不支持 No Samsung Internet Android 完整支持 2.0
orientation descriptor Chrome 不支持 No Edge 不支持 12 — 79 Prefixed
不支持 12 — 79 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No Opera Android 不支持 11.1 — 14 Prefixed
不支持 11.1 — 14 Prefixed
Prefixed Implemented with the vendor prefix: -o-
Safari iOS 不支持 No Samsung Internet Android 不支持 No
user-zoom descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16 Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 29 — 84 Firefox Android 不支持 No Opera Android 完整支持 16 Safari iOS 不支持 No Samsung Internet Android 完整支持 2.0
viewport-fit descriptor Chrome 不支持 No Edge 不支持 No Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
width descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16
完整支持 16
不支持 11.1 — 15
Safari 不支持 No WebView Android 不支持 4.4 — 37 Chrome Android 不支持 29 — 84 Firefox Android 不支持 No Opera Android 完整支持 16
完整支持 16
不支持 11.1 — 14
Safari iOS 不支持 No Samsung Internet Android 完整支持 2.0
zoom descriptor Chrome 不支持 29 — 84 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 不支持 No IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 16 Safari 不支持 No WebView Android 不支持 4.4 — 37 Chrome Android 不支持 61 — 84 Firefox Android 不支持 No Opera Android 完整支持 16 Safari iOS 不支持 No Samsung Internet Android 完整支持 8.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考
  3. CSS Device Adaptation
  4. At-Rules
    1. @viewport

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1