Being implemented in WebKit, for iOS, behind the PerEdgeViewportFitEnabled preference, which is off by default.
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:
viewport-fit=cover without keeping their content clear of the unsafe area at every edge with env(safe-area-inset-*), so browsers that honor it at every edge obscure their content. For compatibility, Safari doesn’t always honor it.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">
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:
auto, the default, doesn’t affect the initial layout viewport.contain sets the initial layout viewport to the largest rectangle inscribed in the display.cover sets the initial layout viewport to the rectangle circumscribing the display.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.
viewport-fit=cover for compatibility to extend it underneath the unsafe area.viewport-fit=cover wants to adopt it one edge at a time, rather than auditing every edge of every page at once.viewport-fit.viewport-fit=cover on its own as a hint, as they already do in practice for compatibility, while honoring cover at an edge the page names.viewport-fit=cover.We propose four keys for the viewport <meta> element:
viewport-fit-topviewport-fit-rightviewport-fit-bottomviewport-fit-leftEach 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.
The browser sets the initial layout viewport and the visual viewport edge by edge, according to each edge’s viewport-fit:
contain, the edge of the viewport is the corresponding edge of the largest rectangle inscribed in the display.cover, the edge of the viewport is the corresponding edge of the rectangle circumscribing the display.auto, the edge of the viewport is unaffected, as it is with viewport-fit=auto.cover only because of viewport-fit=cover, the browser may instead treat the edge as auto, for compatibility with pages that use viewport-fit=cover without keeping their content clear of the unsafe area at that edge.This relaxes CSS Round Display, which defines cover as setting the viewport to the rectangle circumscribing the display.
The safe area insets are insets from the edges of the viewport, so they follow from where each edge of the viewport is:
cover, the safe area inset is the distance from that edge of the viewport to the safe area, as it is today with viewport-fit=cover.auto despite viewport-fit=cover, the edge of the viewport is already within the safe area, so the safe area inset is 0.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.
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.
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.
Browsers ignore keys in the viewport <meta> element that they don’t recognize. In a browser that doesn’t support the new keys:
viewport-fit-top=cover, stays within the safe area at every edge, as it would without the key. It loses the extension underneath the unsafe area, but none of its content is obscured.viewport-fit=cover, viewport-fit-bottom=contain, is treated as using viewport-fit=cover, so it extends underneath the unsafe area wherever the browser honors viewport-fit=cover. If it pads its content with the safe area inset at the edges it keeps within the safe area, too, it works in both kinds of browsers, since the safe area inset is 0 at those edges in a browser that supports the new keys.viewport-fit, as in viewport-fit=cover, viewport-fit-top=cover, viewport-fit-right=cover, viewport-fit-bottom=cover, viewport-fit-left=cover. Browsers that support the new keys honor cover at every edge, and other browsers treat the page as using viewport-fit=cover.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.
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.
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.
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.
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.
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.
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.
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.
auto at an edge. We propose that auto at an edge means the browser’s default for that edge, as viewport-fit=auto does for the whole viewport, so that a page can return one edge to the default while the others cover the unsafe area. An edge without a key already follows viewport-fit.cover at every edge has to name all four edges. Should there be a shorter way to do that?viewport-fit<meta> elementviewport-fit and the safe area insets