explainers

Per-edge viewport-fit

Authors

Participate

Status

Being implemented in WebKit, for iOS, behind the PerEdgeViewportFitEnabled preference, which is off by default.

Table of contents

tl;dr

viewport-fit=cover lets a page extend underneath the parts of the display that may be obscured, such as the status bar, the sensor housing and the home indicator. It has two problems:

We propose four keys for the viewport <meta> element, viewport-fit-top, viewport-fit-right, viewport-fit-bottom and viewport-fit-left, that each set viewport-fit for one edge. Using these keys, a page can cover unsafe area edges that it’s designed for, and have the browser be responsible for ensuring other edges are clear of the unsafe area. A page that names an edge has said it’s designed for that edge, so browsers honor cover there, and can keep treating viewport-fit=cover on its own as a hint.

<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit-top=cover">

Introduction

Many devices have displays that web content can’t use entirely. The corners of the display may be rounded, a sensor housing may cut into it, and the system may draw its own UI, such as a status bar or a home indicator, on top of the page. The part of the display that is clear of all of these is the safe area.

By default, browsers keep pages within the safe area. With the viewport-fit key of the viewport <meta> element, a page can instead ask to fill the whole display. As defined in CSS Round Display, viewport-fit has three values:

A page that covers the display then uses the safe area insets, env(safe-area-inset-top), env(safe-area-inset-right), env(safe-area-inset-bottom) and env(safe-area-inset-left), to keep its text and controls within the safe area, while its backgrounds extend underneath the unsafe area:

header {
    padding-top: env(safe-area-inset-top);
    background: navy;
}

However, viewport-fit applies to every edge of the viewport at once. A page that only wants its header to run underneath the status bar has to cover the unsafe area at all four edges, and take over the job of keeping its content clear of it at the three edges it didn’t want to cover:

In practice, many pages either don’t use cover, or use it and leave content underneath the unsafe area at the edges their authors didn’t consider. For example, when the device rotates to landscape, the sensor housing moves to the left or right edge of the viewport, so a page whose author only kept its content clear of the unsafe area at the top has obscured content at the sides.

Because so many pages use viewport-fit=cover this way, browsers can’t always honor it without obscuring content. For compatibility, Safari already doesn’t fully support viewport-fit=cover. The proposal lets a page reliably extend underneath the unsafe area at the edges it names, and lets browsers keep treating viewport-fit=cover on its own as a hint.

Use cases

Goals

Non-goals

Proposal

Syntax

We propose four keys for the viewport <meta> element:

Each accepts the same values as viewport-fit, auto, contain and cover, and is parsed the same way, so keys and values are ASCII case-insensitive.

The browser applies the keys in the order they appear in content. viewport-fit sets the viewport-fit of each edge, and each edge’s key sets the viewport-fit of that edge alone, so an edge’s viewport-fit is the value of the last of these keys to appear. An edge that no key sets is auto. A page that sets both viewport-fit and an edge-specific key should put viewport-fit first.

A browser ignores an invalid value for an edge, so the edge keeps the viewport-fit set by any earlier key. A browser treats an invalid value for viewport-fit as auto, as it does today, so the key sets every edge to auto.

content Top Right Bottom Left
viewport-fit=cover cover (hint) cover (hint) cover (hint) cover (hint)
viewport-fit-top=cover cover auto auto auto
viewport-fit-bottom=cover auto auto cover auto
viewport-fit-left=cover, viewport-fit-right=cover auto cover auto cover
viewport-fit=cover, viewport-fit-bottom=contain cover (hint) cover (hint) contain cover (hint)
viewport-fit=cover, viewport-fit-top=auto auto cover (hint) cover (hint) cover (hint)
viewport-fit-top=contain, viewport-fit=cover cover (hint) cover (hint) cover (hint) cover (hint)
viewport-fit=cover, viewport-fit-bottom=invalid cover (hint) cover (hint) cover (hint) cover (hint)

Note: cover (hint) means that the browser may still keep the page from extending underneath the unsafe area at that edge, for compatibility, as described in Layout.

As with viewport-fit, only the viewport <meta> element of the top-level document has an effect.

Layout

The browser sets the initial layout viewport and the visual viewport edge by edge, according to each edge’s viewport-fit:

This relaxes CSS Round Display, which defines cover as setting the viewport to the rectangle circumscribing the display.

Safe area insets

The safe area insets are insets from the edges of the viewport, so they follow from where each edge of the viewport is:

As a result, a page can pad its content with the safe area inset at every edge, and its content stays clear of the unsafe area whichever edges cover it.

Changing the viewport

A page can change the new keys at any time, by changing the content attribute of its viewport <meta> element, as it can with any other key. The browser then updates the viewport and the safe area insets, as it does when viewport-fit changes.

Example

A web app with a tab bar fixed to the bottom of the viewport covers the unsafe area at the bottom edge only:

<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit-bottom=cover">
<style>
nav.tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding-bottom: env(safe-area-inset-bottom);
    background: white;
}
</style>

The tab bar’s background reaches the bottom of the display, behind the home indicator, and its padding keeps its buttons above the home indicator. The browser keeps the rest of the page clear of the status bar at the top, and of the sensor housing at the left or right in landscape, where the safe area insets are 0.

Progressive enhancement

Browsers ignore keys in the viewport <meta> element that they don’t recognize. In a browser that doesn’t support the new keys:

Considered alternatives

Covering every edge and padding with the safe area insets

Pages can already cover the unsafe area at every edge, and pad their content with the safe area insets at the edges they don’t want to cover. As described in the introduction, this forces the page to take responsibility for every edge in order to extend underneath one, and to recreate what the browser already does to keep a page within the safe area. In a browser that treats viewport-fit=cover as a hint, the page may not extend underneath the unsafe area at all.

A value for viewport-fit that browsers always honor

viewport-fit could accept a new value that browsers always honor, unlike cover, such as viewport-fit=cover-always. This would address the compatibility problem, but the page would still cover all four edges at once. It would also fall back poorly: a browser that doesn’t recognize the new value treats it as invalid, and so as auto, and the page loses the extension underneath the unsafe area at every edge. A browser that doesn’t recognize the per-edge keys ignores them, and keeps honoring viewport-fit=cover as it does today.

Multiple values for viewport-fit

viewport-fit could accept up to four values, one for each edge, like the margin shorthand in CSS, such as viewport-fit=cover contain contain contain. However, the syntax of the viewport <meta> element uses whitespace to separate keys from one another, so existing browsers read this as viewport-fit=cover followed by a key named contain, covering every edge. Using a different separator within the value would be new to the viewport <meta> element. Separate keys keep to its existing pattern of one value for each key, and let a page specify the edges independently.

Values that name edges

viewport-fit could accept new values that name the edges to cover, such as viewport-fit=cover-top. Covering any combination of edges this way needs either fifteen values or a list of values, which has the problem described above.

Logical edges

The keys could name logical edges, such as viewport-fit-block-start, rather than physical ones. However, the unsafe area comes from the physical display and from how the device is held, not from the page’s writing mode: the status bar is at the top of a page written vertically, or right to left, too. Logical keys would make the edge that covers the unsafe area depend on the writing mode, and wouldn’t line up with the safe area insets, which are physical.

Edge keys that take precedence over viewport-fit

An edge’s key could take precedence over viewport-fit wherever each appears in content, so viewport-fit-top=contain, viewport-fit=cover would make the top edge contain and the other edges cover. This would make the result independent of the order of the keys. However, it would be unlike how CSS applies a shorthand property and its longhands, and unlike how the viewport <meta> element already handles a key that appears more than once, where a later key overrides an earlier one.

A CSS property

The viewport-fit of each edge could be set in CSS, such as with a property on the root element. However, CSS Round Display originally defined viewport-fit as a descriptor of the @viewport rule, which browsers no longer support, and pages set it in the viewport <meta> element today. Keeping the new keys next to viewport-fit lets pages set the viewport-fit of the whole viewport and of each edge in one place, which the browser knows about before it lays out the page.

Privacy and security considerations

The proposal doesn’t expose any new information. A page can already read the safe area insets at every edge with viewport-fit=cover, and with the new keys, it reads them at the same edges or at fewer of them.

The proposal doesn’t let a page draw anywhere that viewport-fit=cover doesn’t already allow.

Open questions

References