大网数据广告活动
Web 标准与前端开发

Viewport Segments API

· 52CE 我爱测

The **Viewport Segments API** allows developers to access the position and dimensions of logically separate viewport segments using CSS and JavaScript. _Viewport segments_ are created when the viewport is split by one or more hardware features such as a fold or a hinge between se

Viewport Segments API

The Viewport Segments API allows developers to access the position and dimensions of logically separate viewport segments using CSS and JavaScript. Viewport segments are created when the viewport is split by one or more hardware features such as a fold or a hinge between separate displays. With the Viewport Segments API, developers can create responsive designs optimized for different viewport segment sizes and arrangements.

Concepts and usage

Devices with multiple displays that are intended to act as different segments of the same display surface (think foldable or hinged screen smartphones) present some unique design challenges to developers. You can optimize your layout for the display as a single entity, but how can you ensure that design elements fit snugly on the different segments and are not cut into two pieces? And how can you prevent content from being hidden by the physical fold/join?

It may be important to know whether a user's device screen has a fold or join, what size the different segments are, whether they are the same size, and the orientation of the segments (whether they are side-by-side or top-to-bottom). The Viewport Segments API enables accessing the user's segmented device information with CSS and JavaScript features that provide access to the position and dimensions of each viewport segment within a display, including @media features to detect different horizontal and vertical region layouts.

For an explanation of how the API works, see Using the Viewport Segments API.

CSS features

  • @media/horizontal-viewport-segments @media feature
    • : Detects whether the device has a specified number of viewport segments laid out horizontally.
  • @media/vertical-viewport-segments @media feature
    • : Detects whether the device has a specified number of viewport segments laid out vertically.
  • Viewport segment environment variables
    • : These environment variables provide access to the edge coordinates and dimensions of each viewport segment.

Interfaces

  • Viewport
    • : Represents the device's viewport. Provides access to the Viewport.segments property, which returns an array of DOMRect objects representing the position and dimensions of each viewport segment within a segmented display.

Extensions to other interfaces

  • Window.viewport
    • : Returns a Viewport object instance, which provides information about the current state of the device's viewport.

Examples

You can find a complete example demonstrating usage of the above features in the Viewport segment API demo.

If possible, you should view the demo on a foldable device. Current browser developer tools enable emulating foldable devices, but don't include emulation of the different physical segments.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。